Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change legend title ggplot2 [duplicate]

Tags:

r

legend

ggplot2

i got a plot in R based on ggplot2. Unfortunately, I have not enough reputation here to upload the plot. Now, I want to change my legend title and the names of the labels for two lines.

Thus, I added scale_fill_continuous(name = "New Legend Title", labels=c("Control", "Treatment")) to the code.

ggplot(data=descripintens, 
aes(x=syear, y=workingintensity, group= treatment, colour=factor(treatment))) + 
geom_line() +
xlab("Year") +
ylab("Working intensity") +
scale_fill_continuous(name = "New Legend Title", labels=c("Control", "Treatment"))

This is what is suggested in stackoverflow and the ggplot2 cheat sheet. Nothing changes. Not even an error comes up.

What is wrong with my code?

Thank you!

EDIT: The data I used for the plot is a table, which is based on this code:

descripintens<-intensity141516 %>% 
  group_by(syear, treatment) %>%
  summarise(workingintensity=mean(intensity))

The table descripintens looks like this:

   syear Treatment workingintensity
1  2014     0         96.2
2  2014     1         98.4
3  2015     0         101.00000
4  2015     1         102.00000
5  2016     0         105.9
6  2016     1         106.2
like image 435
MARIUS Avatar asked Oct 25 '18 09:10

MARIUS


Video Answer


1 Answers

You can try with this:

ggplot(data = descripintens
       ,aes(x = syear, y = workingintensity, group = treatment,colour = factor(treatment))) +
  geom_line() +
  xlab("Year") +
  ylab("Working intensity") + 
  labs(color='NEW LEGEND TITLE')  +
  # you should specify the color also
  scale_color_manual(labels = c("Control", "Treatment")
                     ,values = c("blue", "red"))

enter image description here


With data:

descripintens <- read.table(text ="   syear treatment workingintensity
1  2014     0         96.2
2  2014     1         98.4
3  2015     0         101.00000
4  2015     1         102.00000
5  2016     0         105.9
6  2016     1         106.2")
like image 182
s__ Avatar answered Sep 20 '22 07:09

s__