简体   繁体   中英

Changing legend title and labels in R?

I am new to R, having just recently taken it up.

I have generated a plot, shown with two separate lines of data which is linked here:

1

My current code for the graph is:

ggplot(Stats, aes(x=Well))+
  geom_line(aes(y=Historic.Mean, colour="red", group="red"))+ 
  geom_line(aes(y=Recent.Mean, colour="blue", group="blue"))+ 
  labs(x="Well", y="Water Level (m)", title="Historic vs Recent Well Levels")+
  scale_fill_discrete(name= "Means", labels= c("Historic", "Recent"))+
  theme(axis.text.x=element_text(angle=90))

Running this code, generated the plot previously shown, but has not updated the legend title and labels appropriately for some reason.

I have also tried including fill= "legend title" in the labs section instead of the "scale_fill_discrete" which has not worked.

Any help?

You accidentally selected fill when you are actually using colour . The plot therefore provides labels for a fill element, however since such does not exist, nothing appears to happen. Change scale_fill_discrete to scale_colour_discrete .

Its hard to see where you've gone wrong without a reprex, see here: https://reprex.tidyverse.org/ . Its generally best practise when posting here so that people can just stick your code in their R and work out whats causing the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM