Home > Enterprise >  Modifying Legend Labels and Color Palette for Improved Visibility in ggplot2 Plot geom_path?
Modifying Legend Labels and Color Palette for Improved Visibility in ggplot2 Plot geom_path?

Time:11-05

I have used the following code to create the attached graph:

# Load required library
library(ggplot2)
library(ggmulti)

# Simulate data
set.seed(123)  # Set a seed for reproducibility
n_points <- 100  # Number of data points

# Simulate values for Loading variables l1, l2, l3, l4, l5, and l6
l1 <- rnorm(n_points)
l2 <- rnorm(n_points)
l3 <- rnorm(n_points)
l4 <- rnorm(n_points)
l5 <- rnorm(n_points)
l6 <- rnorm(n_points)

# Create a labelling variable with categories
labelling <- factor(sample(1:3, n_points, replace = TRUE))

# Create a data frame with the simulated data
dataplot <- data.frame(l1, l2, l3, l4, l5, l6, labelling)

# Create the plot using ggplot
p <- ggplot(dataplot,
            aes(
              Loading1 = l1,
              Loading2 = l2,
              Loading3 = l3,
              Loading4 = l4,
              Loading5 = l5,
              Loading6 = l6,
              color = labelling
            ))  
  geom_path(alpha = 0.2)  
  theme_bw()  
  scale_shape_manual(values = 7)  
  coord_serialaxes()

# Display the plot
print(p)

My questions: How can I change the labels used in the legend to make them more visible e.g. using a filled square to indicate the color? And how to change the colors used with the goal to use more distinctive colors?

enter image description here

CodePudding user response:

Solution 1

Using this question as reference enter image description here

Solution 2

@MrFlick's suggestion was considered enter image description here

  • Related