[R] legend order in ggplot2

John mi@ojpm @ending from gm@il@com
Tue May 22 05:16:48 CEST 2018


Hi,

   I'd like to graph three lines on ggplot2 and I intend the lines to be
"solid", "dashed", and "dotted". The legend names are "name_b", "name_a",
"name_c". I'd like to legend to present in the order: the "name_b" at the
top, and "name_c" at the bottom.
As a consequence, the legend is indeed in the order: name_b at the top and
name_c at the bottom. However, I'd like name_b to corresponds to "solid",
while it corresponds to "dashed", etc, which I don't want. How could I make
"solid" correspond to "name_b"? Thanks,

#########

library(ggplot2)
df<-data.frame(x1=c(1,2), y1=c(3,4),z1=c(5,6),w1=c(7,8))
p1<-ggplot(df, aes(x=1:2, y=x1))+
  geom_line(aes(linetype="name_b"))+
  geom_line(aes(x=1:2, y=y1, linetype="name_a"), df)+
  geom_line(aes(x=1:2, y=z1, linetype="name_c"), df)+
  scale_linetype_manual(name="", values=c("solid","dashed", "dotted"),
breaks=c("name_b","name_a","name_c"))
###########

	[[alternative HTML version deleted]]



More information about the R-help mailing list