How to modify the linetype of the hline in the legend while using geom_link2

Viewed 122

I'm trying to modify the legend of the hline.

Here is my data:

mydata=data.frame(month = c("Jan-1","Jan-10","Jan-15","Jan-20","Jan-25","Jan-30"),
                  rate = c(88.8,86,88.5,90,89,87))

And here is the code I used to create the figure:

library(ggplot2)
library(ggforce)
ggplot(mydata, aes(x = month, y = rate)) +
  geom_point(aes(group = 1,colour = after_stat(y < 88),
                 shape=after_stat(y < 88)),size=2) +
  geom_text(aes(label = paste(format(rate, digits = 4, format = "f"), "%")), 
            color="black",vjust = -0.5, size = 3.5) +
  geom_link2(aes(group = 1,colour = after_stat(y < 88)),size=1) +
  geom_hline(aes(yintercept=88,color="Target",linetype="Target"),size=0.7)+
  labs(y = NULL, x= NULL, color = NULL, linetype = NULL, shape = NULL, size = NULL) +
  scale_colour_manual(values = c("TRUE"="#F8766D","FALSE"="#00BFC4","Target"="#619CFF")) +
  scale_shape_manual(values = c("TRUE"= 16, "FALSE"=16, "Target" = NA)) + 
  scale_linetype_manual(values = c("TRUE"="solid","FALSE"="solid","Target" = "dashed")) 

enter image description here

As you can see, the legend of "Target" is still a solid line, I'd like to know how to change it into a dashed line.

In addition, I'd like to know why it gave me errors when I tried to modify the linetype in geom_link2():

ggplot(d1, aes(x = month, y = percent)) +
  geom_point(aes(group = 1,colour = after_stat(ifelse(y > 88,"qualified","unqualified")),
                 shape=after_stat(ifelse(y > 88,"qualified","unqualified"))), size=2) +
  geom_text(aes(label = paste(format(percent, digits = 4, format = "f"), "%")), 
            color="black",vjust = -0.5, size = 3.5) +
  geom_link2(aes(group=1, colour = after_stat(ifelse(y > 88,"qualified","unqualified")),
                 linetype = after_stat(ifelse(y > 88,"qualified","unqualified"))),size=1) +
  geom_hline(aes(yintercept=88,color="Target",linetype="Target"),size=0.7)+
  scale_y_continuous(breaks = sort(c(seq(80,100,10),88)), 
                     labels = paste0(sort(c(seq(80,100,10),88))," %")) +
  expand_limits(y = c(80, 100)) +
  labs(y = NULL, x= NULL, color = NULL, linetype = NULL, shape = NULL, size = NULL) +
  scale_colour_manual(values = c(qualified="#00BFC4",unqualified="#F8766D",Target="#F8766D")) +
  scale_shape_manual(values = c(qualified= 16, unqualified=16, Target = NA)) + 
  scale_linetype_manual(values = c(qualified="solid", unqualified="solid", Target="dashed")) 

Error: geom_path_interpolate: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

1 Answers

One option would be to override the aesthetics via guide_legend(override.aes = ...) like so:

library(ggplot2)
library(ggforce)

ggplot(mydata, aes(x = month, y = rate)) +
  geom_point(aes(group = 1,colour = after_stat(y < 88),
                 shape=after_stat(y < 88)),size=2) +
  geom_text(aes(label = paste(format(rate, digits = 4, format = "f"), "%")), 
            color="black",vjust = -0.5, size = 3.5) +
  geom_link2(aes(group = 1, colour = after_stat(y < 88)), size=1) +
  geom_hline(aes(yintercept=88, color="Target", linetype="Target"),size=0.7)+
  labs(y = NULL, x= NULL, color = NULL, linetype = NULL, shape = NULL, size = NULL) +
  scale_colour_manual(values = c("TRUE"="#F8766D","FALSE"="#00BFC4","Target"="#619CFF")) +
  scale_shape_manual(values = c("TRUE"= 16, "FALSE"=16, "Target" = NA)) + 
  scale_linetype_manual(values = c("TRUE"="solid","FALSE"="solid","Target" = "dashed")) +
  guides(color = guide_legend(override.aes = list(linetype = c("solid", "solid", "dashed"))))

Related