Home > front end >  Creating a legend with shapes using ggplot2
Creating a legend with shapes using ggplot2

Time:12-12

I have created the following code for a graph in which four fitted lines and corresponding points are plotted. I have problems with the legend. For some reason I cannot find a way to assign the different shapes of the points to a variable name. Also, the colours do not line up with the actual colours in the graph.

y1 <- c(1400,1200,1100,1000,900,800)
y2 <- c(1300,1130,1020,970,830,820)
y3 <- c(1340,1230,1120,1070,940,850)
y4 <- c(1290,1150,1040,920,810,800)

df <- data.frame(x,y1,y2,y3,y4)


g <- ggplot(df, aes(x=x), shape="shape")  
  geom_smooth(aes(y=y1), colour="red", method="auto", se=FALSE)   geom_point(aes(y=y1),shape=14)  
  geom_smooth(aes(y=y2), colour="blue", method="auto", se=FALSE)   geom_point(aes(y=y2),shape=8)  
  geom_smooth(aes(y=y3), colour="green", method="auto", se=FALSE)   geom_point(aes(y=y3),shape=6)  
  geom_smooth(aes(y=y4), colour="yellow", method="auto", se=FALSE)   geom_point(aes(y=y4),shape=2)  
  ylab("x")   xlab("y")   labs(title="overview")
  geom_line(aes(y=1000), linetype = "dashed")
  theme_light()  
  theme(plot.title = element_text(color="black", size=12, face="italic", hjust = 0.5))  
  scale_shape_binned(name="Value g", values=c(y1="14",y2="8",y3="6",y4="2"))

print(g)

I am wondering why the colours don't match up and how I can construct such a legend that it is clear which shape corresponds to which variable name.

CodePudding user response:

Freek19,

While you can add the legend manually via scale_shape_manual, perhaps the adequate solution would be to reshape your data (try using tidyr::pivot_longer() on y1:y4 variables), and then assigning the resulting variable to the shape aesthetic (you can then manually set the colors to your liking). You would then need to use a single geom_point() and geom_smooth() instead of four of each.

Also, you're missing a reproducible example (what are the values of x?) and your code emits some warnings while trying to perform loess smoothing (because there's fewer data points than need to perform it).

CodePudding user response:

I managed to get close to what I want, using:


library(ggplot2)


data <- data.frame(x = c(0,0.02,0.04,0.06,0.08,0.1),
                   y = c(1400,1200,1100,1000,910,850, #y1
                         1300,1130,1010,970,890,840,  #y2
                         1200,1080,980,950,880,820, #y3
                         1100,1050,960,930,830,810, #y4
                         1050,1000,950,920,810,800), #y5
                   
                   group = rep(c("5%","6%","7%","8%","9%"), each = 6))
data

Values <- ggplot(data, aes(x, y, shape = group, color = group))      # Create line plot with default colors
  geom_smooth(aes(color=group))   geom_point(aes(shape=group),size=3)  
  scale_shape_manual(values=c(1, 2, 3,4,5)) 
  geom_line(aes(y=1000), linetype = "dashed")  
  ylab("V(c)")   xlab("c")   labs(title="Valuation") 
  theme_light()  
  theme(plot.title = element_text(color="black", size=12, face="italic", hjust = 0.5)) 
  labs(group="Program Type")

Values

I am only stuck with 2 legends. I want to change both name, because otherwise they overlap. However I am not sure how to do this.

  • Related