How to easily change legend name inside a graph in R?

How to easily change legend name inside a graph in R?

I’ll generate one data.

Genotype<- rep(c("CV1","CV2","CV3"), each=6)
Fertilizer<- rep(c("N0","N1"), times=9)
Block<- rep(c("I","I","II","II","III","III"), times=3)
Yield<- c(100,110,105,113,103,112,120,130,125,133,123,132,90,100,95,103,93,102)
dataA<- data.frame(Genotype,Fertilizer,Block, Yield)

dataA

Then, I’ll make a bar graph about this data. To make a bar graph, data should be summarized.

library(plyr)

summary<- ddply (dataA, c("Genotype","Fertilizer"), summarise, mean=mean(na.rm=T,Yield), sd=sd(na.rm=T,Yield), n=length(Yield), se=sd/sqrt(n))

summary
library(ggplot2)

ggplot(data=summary, aes(x=Genotype, y=mean, fill=Fertilizer)) +
  geom_bar(stat="identity", position="dodge", width=0.5, size=1) +
  geom_errorbar(aes(ymin=mean-se,ymax=mean+se), width=0.2, 
  position=position_dodge(0.5), color='Black') +
  scale_fill_manual(values=c("grey75","grey25")) +
  scale_y_continuous(breaks=seq(0,150,30), limits=c(0,150)) +
  labs(x="Genotype", y="Yield") +
  theme_classic(base_size=18, base_family="serif")+
  theme(legend.position= c(0.90,0.90),
        legend.title= element_text(face="plain",family="serif", 
        size=18, color="Black"), 
        legend.key= element_rect(color=alpha("grey",.01),
        fill= alpha("grey",.01)), 
        legend.text= element_text(face="plain",family="serif", 
        size=17, color="Black"), 
        legend.background= element_rect(fill= alpha("grey",.05)),
        axis.line= element_line(size=0.5, colour="black")) +
windows(width=5.5, height=5)

Now, I want to change legend name from N0 to 0kg N/ha, and N1 to 200kg N/ha. Simply we can add more code like this; scale_fill_manual(label=c("0kg N/ha","200kg N/ha"), values=c("grey75","grey25"))

ggplot(data=summary, aes(x=Genotype, y=mean, fill=Fertilizer)) +
  geom_bar(stat="identity", position="dodge", width=0.5, size=1) +
  geom_errorbar(aes(ymin=mean-se,ymax=mean+se), width=0.2, 
  position=position_dodge(0.5), color='Black') +
  scale_fill_manual(label=c("0kg N/ha","200kg N/ha"), 
  values=c("grey75","grey25")) +
  scale_y_continuous(breaks=seq(0,150,30), limits=c(0,150)) +
  labs(x="Genotype", y="Yield") +
  theme_classic(base_size=18, base_family="serif")+
  theme(legend.position= c(0.90,0.90),
        legend.title= element_text(face="plain",family="serif", 
        size=18, color="Black"), 
        legend.key= element_rect(color=alpha("grey",.01),
        fill= alpha("grey",.01)), 
        legend.text= element_text(face="plain",family="serif", 
        size=17, color="Black"), 
        legend.background= element_rect(fill= alpha("grey",.05)),
        axis.line= element_line(size=0.5, colour="black")) +
windows(width=5.5, height=5)

What if we want to change the title of legend from Fertilizer to Treatment? Just add one code like this; labs(fill="Treatment",x="Genotype", y="Yield")

ggplot(data=summary, aes(x=Genotype, y=mean, fill=Fertilizer)) +
  geom_bar(stat="identity", position="dodge", width=0.5, size=1) +
  geom_errorbar(aes(ymin=mean-se,ymax=mean+se), width=0.2, 
  position=position_dodge(0.5), color='Black') +
  scale_fill_manual(label=c("0kg N/ha","200kg N/ha"), 
  values=c("grey75","grey25")) +
  scale_y_continuous(breaks=seq(0,150,30), limits=c(0,150)) +
  labs(fill="Treatment", x="Genotype", y="Yield") +
  theme_classic(base_size=18, base_family="serif")+
  theme(legend.position= c(0.90,0.90),
        legend.title= element_text(face="plain",family="serif", 
        size=18, color="Black"), 
        legend.key= element_rect(color=alpha("grey",.01),
        fill= alpha("grey",.01)), 
        legend.text= element_text(face="plain",family="serif", 
        size=17, color="Black"), 
        legend.background= element_rect(fill= alpha("grey",.05)),
        axis.line= element_line(size=0.5, colour="black")) +
windows(width=5.5, height=5)

Leave a Reply

If you include a website address in the comment section, I cannot see your comment as it will be automatically deleted and will not be posted. Please refrain from including website addresses.