midnightgazer
midnightgazer

Reputation: 3

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?

Upvotes: 0

Views: 911

Answers (2)

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.

Upvotes: 2

Robbie Still
Robbie Still

Reputation: 11

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.

Upvotes: 0

Related Questions