1

First post so hopefully I've included everything.

I'm trying to do two things on the below charts:

  1. Change the legend title from z,z1,z2 to POS, ROD, INS
  2. Change the legend labels all to AOK and TUR

I've been trying to find the answer all day and so far nothing has worked.

Plot

My data is:

Year Area   Percentage
  <dbl> <fct>       <dbl>
1  2008 AOKPOS      0.571
2  2008 AOKPOS      0.6  
3  2008 AOKPOS      0.5  
4  2008 AOKPOS      0.846
5  2008 AOKPOS      0.2  
6  2008 AOKPOS      0.625

My code is:

plot1 <- ggplot() +
    # blue plot
    scale_x_continuous(name="", limits = c(2008, 2019),breaks = 0:2100)+ #No X axis title
    scale_y_continuous(name = "",labels = scales::percent)+ 
    theme(text = element_text(size=10))+
    coord_cartesian(ylim = c(0,1))+
    geom_smooth(data=Possum, aes(x=x, y=y, group=z, colour=z), size=1)

plot1

plot2 <- ggplot() +
    # blue plot
    scale_x_continuous(name="", limits = c(2008, 2019), breaks = 0:2100)+ #No X axis title
    scale_y_continuous(name = "",labels = scales::percent)+ 
    theme(text = element_text(size=10))+
    coord_cartesian(ylim = c(0,1))+
    geom_smooth(data=Rodent, aes(x=x1, y=y1, group=z1, colour=z1), size=1)

plot2

plot3 <- ggplot() +
    # blue plot
    scale_x_continuous(name="", limits = c(2008, 2019), breaks = 0:2100)+ #No X axis title
    scale_y_continuous(name = "",labels = scales::percent)+ 
    theme(text = element_text(size=10))+
    coord_cartesian(ylim = c(0,1))+
    geom_smooth(data=Insect, aes(x=x2, y=y2, group=z2, colour=z2), size=1)

plot3

grid.arrange(plot1, plot2, plot3, nrow=3)
Z.Lin
  • 28,055
  • 6
  • 54
  • 94
Andy McKay
  • 11
  • 1

2 Answers2

1

I don't know how you want to change it but you can use labs() function to do that like so

#Default plot
print(p)

#Modify legend titles
p + labs(fill = "Dose (mg)")

enter image description here

after using the code the title is change like this enter image description here

AbdulRahim Khan
  • 121
  • 1
  • 3
  • Welcome to SO, the provided data doesn't seem to reflect the plot in the linked image. For the legend title try ```+ labs(fill="POS")``` to plot1 and so forth. For the legend labels you could change the Area factor levels. It will be good to have a minimal reprex (See [here][1]). You seem to have a 4th variable (possum, rodent and insect) not available in the data. [1]: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610 – sactyr Nov 26 '19 at 03:27
0

Ok, so shortly after posting I managed to figure it out:

    plot1<-ggplot() +
    scale_x_continuous(name="", limits = c(2008, 2019),breaks = 0:2100)+ #No X axis title
    scale_y_continuous(name = "",labels = scales::percent)+ theme(text = element_text(size=10))+
    coord_cartesian(ylim = c(0,1))+
    geom_smooth(data=Possum, aes(x=x, y=y, group=z, colour=z), name = "Possum",size=1)

plot1<- plot1 + scale_color_discrete(name="Pos", label=c("Aok","Tur"))

plot1

plot2<-ggplot() +
    scale_x_continuous(name="", limits = c(2008, 2019), breaks = 0:2100)+ #No X axis title
    scale_y_continuous(name = "",labels = scales::percent)+ theme(text = element_text(size=10))+
    coord_cartesian(ylim = c(0,1))+
    geom_smooth(data=Rodent, aes(x=x1, y=y1, group=z1, colour=z1), size=1)

plot2<- plot2 + scale_color_discrete(name="Rod", label=c("Aok","Tur"))

plot2

plot3<-ggplot() +
    scale_x_continuous(name="", limits = c(2008, 2019), breaks = 0:2100)+ #No X axis title
    scale_y_continuous(name = "",labels = scales::percent)+ theme(text = element_text(size=10))+
    coord_cartesian(ylim = c(0,1))+
    geom_smooth(data=Insect, aes(x=x2, y=y2, group=z2, colour=z2), size=1)

plot3<- plot3 + scale_color_discrete(name="Ins",label=c("Aok","Tur"))

plot3

grid.arrange(plot1,plot2,plot3, nrow=3)
Andy McKay
  • 11
  • 1