Home > Software engineering >  How to create legend by line type and colour in stat_function
How to create legend by line type and colour in stat_function

Time:11-30

Here I have tried to plot a PDF of 11 variables using ggplot2 and stat_function.I have managed to add legend by colour but I want also to differentiate the lines by line type also and incorporate it into the legend. If anybody could help me with matter I would be thankful? Here is the code:

library(gamlss)
library(ggplot2)
library(cowplot)

xlower= 50
xupper= 183


    plot1<-ggplot(data.frame(x = c(xlower , xupper)), aes(x = x))   
        xlim(c(xlower , xupper))   stat_function(fun = dNO, args =list(mu= 85.433,sigma=2.208) ,linetype="solid", aes(colour = "Observed")) 
        stat_function(fun = dSN2, args =list(mu= 97.847,sigma=2.896,nu=5.882,log=FALSE) , linetype="solid",aes(colour = "CMCC.ESM2")) 
        stat_function(fun = dIGAMMA, args =list(mu= 4.520,sigma=2.336,log=FALSE) , linetype="solid",aes(colour = "ECEARTH3")) 
        stat_function(fun = dRG, args =list(mu= 93.062,sigma=2.233,log=FALSE) ,linetype="solid" ,aes(colour = "ECEARTH3.CC")) 
        stat_function(fun = dRG, args =list(mu= 94.296,sigma=2.237,log=FALSE) ,linetype="solid" ,aes(colour = "ECEARTH3.Veg")) 
        stat_function(fun = dSHASH, args =list(mu= 112.241,sigma=12.133,nu=9.419,tau=9.743,log=FALSE) ,linetype="solid", aes(colour = "GFDL.CM4")) 
        stat_function(fun = dRG, args =list(mu= 101.101,sigma=2.372,log=FALSE) ,linetype="solid" ,aes(colour = "GFDL.ESM4")) 
        stat_function(fun = dLO, args =list(mu= 73.086,sigma=1.485,log=FALSE) , linetype="solid",aes(colour = "MPI.ESM1.2.HR")) 
        stat_function(fun = dNO, args =list(mu= 139.373,sigma=2.895) ,linetype="solid",aes(colour = "MRI.ESM2")) 
        stat_function(fun = dLO, args =list(mu= 134.221,sigma=2.158,log=FALSE) ,linetype="solid", aes(colour = "NorESM2.MM")) 
        stat_function(fun = dNO, args =list(mu= 107.372,sigma=2.232) ,linetype="solid" ,aes(colour = "TaiESM1")) 
        scale_color_manual("Data Types",breaks = c("Observed", "CMCC.ESM2","ECEARTH3","ECEARTH3.CC","ECEARTH3.Veg","GFDL.CM4","GFDL.ESM4","MPI.ESM1.2.HR","MRI.ESM2","NorESM2.MM","TaiESM1"),values = c("Black","green", "blue","brown","darkgreen","red","cyan","yellow4","aquamarine4","slateblue4","magenta4")) 
        labs(x = "Monthly average Precipitation (mm)", y = "PDF")   
        theme(plot.title = element_text(hjust = 0.5), 
              axis.title.x = element_text(face="plain", colour="black", size = 12),
              axis.title.y = element_text(face="plain", colour="black", size = 12),
              legend.title = element_text(face="plain", size = 10),
              legend.position = "none")
    
    plot2<- ggplot(data.frame(x = c(xlower , xupper)), aes(x = x))   
        xlim(c(xlower , xupper))   stat_function(fun = pNO, args =list(mu= 85.433,sigma=2.208) ,linetype="solid", aes(colour = "Observed")) 
        stat_function(fun = pSN2, args =list(mu= 97.847,sigma=2.896,nu=5.882,log=FALSE) , linetype="solid",aes(colour = "CMCC.ESM2")) 
        stat_function(fun = pIGAMMA, args =list(mu= 4.520,sigma=2.336,log=FALSE) , linetype="solid",aes(colour = "ECEARTH3")) 
        stat_function(fun = pRG, args =list(mu= 93.062,sigma=2.233,log=FALSE) ,linetype="solid" ,aes(colour = "ECEARTH3.CC")) 
        stat_function(fun = pRG, args =list(mu= 94.296,sigma=2.237,log=FALSE) ,linetype="solid" ,aes(colour = "ECEARTH3.Veg")) 
        stat_function(fun = pSHASH, args =list(mu= 112.241,sigma=12.133,nu=9.419,tau=9.743,log=FALSE) ,linetype="solid", aes(colour = "GFDL.CM4")) 
        stat_function(fun = pRG, args =list(mu= 101.101,sigma=2.372,log=FALSE) ,linetype="solid" ,aes(colour = "GFDL.ESM4")) 
        stat_function(fun = pLO, args =list(mu= 73.086,sigma=1.485,log=FALSE) , linetype="solid",aes(colour = "MPI.ESM1.2.HR")) 
        stat_function(fun = pNO, args =list(mu= 139.373,sigma=2.895) ,linetype="solid",aes(colour = "MRI.ESM2")) 
        stat_function(fun = pLO, args =list(mu= 134.221,sigma=2.158,log=FALSE) ,linetype="solid", aes(colour = "NorESM2.MM")) 
        stat_function(fun = pNO, args =list(mu= 107.372,sigma=2.232) ,linetype="solid" ,aes(colour = "TaiESM1")) 
        scale_color_manual("Data Types",breaks = c("Observed", "CMCC.ESM2","ECEARTH3","ECEARTH3.CC","ECEARTH3.Veg","GFDL.CM4","GFDL.ESM4","MPI.ESM1.2.HR","MRI.ESM2","NorESM2.MM","TaiESM1"),values = c("Black","green", "blue","brown","darkgreen","red","cyan","yellow4","aquamarine4","slateblue4","magenta4")) 
        labs(x = "Monthly average Precipitation (mm)", y = "CDF")   
        theme(plot.title = element_text(hjust = 0.5), 
              axis.title.x = element_text(face="plain", colour="black", size = 12),
              axis.title.y = element_text(face="plain", colour="black", size = 12),
              legend.title = element_text(face="plain", size = 10),
              legend.position = "right")
    
    p<- plot_grid(plot1, plot2, labels = "")

CodePudding user response:

I've reduced the number of stats plotted to make it easier to see what's going on.

As the legends are the same for both graphs you only need one set of scale_x_manual.

Both linetype and colour need to be in the call to aes to appear in the legend.

To merge the two legends give both legends (colour and linetype) the same name in the call to labs.

You may find you need to define additional linetypes as the standard set comprises 6.

library(gamlss)
library(ggplot2)
library(cowplot)

xlower= 50
xupper= 183


plot1<-ggplot(data.frame(x = c(xlower , xupper)), aes(x = x))   
  xlim(c(xlower , xupper))   
  stat_function(fun = dNO, args =list(mu= 85.433,sigma=2.208), aes(colour = "Observed", linetype="Observed")) 
  stat_function(fun = dSN2, args =list(mu= 97.847,sigma=2.896,nu=5.882,log=FALSE), aes(colour = "CMCC.ESM2", linetype = "CMCC.ESM2")) 
  stat_function(fun = dNO, args =list(mu= 107.372,sigma=2.232), aes(colour = "TaiESM1", linetype = "TaiESM1")) 
  labs(x = "Monthly average Precipitation (mm)", y = "PDF")   
  theme(plot.title = element_text(hjust = 0.5), 
        axis.title.x = element_text(face="plain", colour="black", size = 12),
        axis.title.y = element_text(face="plain", colour="black", size = 12),
        legend.title = element_text(face="plain", size = 10),
        legend.position = "none")

plot2<- ggplot(data.frame(x = c(xlower , xupper)), aes(x = x))   
  xlim(c(xlower , xupper))   
  stat_function(fun = pNO, args =list(mu= 85.433,sigma=2.208), aes(colour = "Observed", linetype="Observed")) 
  stat_function(fun = pSN2, args =list(mu= 97.847,sigma=2.896,nu=5.882,log=FALSE), aes(colour = "CMCC.ESM2", linetype = "CMCC.ESM2")) 
  stat_function(fun = dNO, args =list(mu= 107.372,sigma=2.232), aes(colour = "TaiESM1", linetype = "TaiESM1")) 
  scale_color_manual(breaks = c("Observed", "CMCC.ESM2", "TaiESM1"),
                       values = c("Black","green", "magenta4")) 
  scale_linetype_manual(breaks = c("Observed", "CMCC.ESM2", "TaiESM1"),
                       values = c("solid", "dashed", "longdash")) 
  labs(x = "Monthly average Precipitation (mm)",
       y = "CDF",
       colour = "Data types",
       linetype =  "Data types")   
  theme(plot.title = element_text(hjust = 0.5), 
        axis.title.x = element_text(face="plain", colour="black", size = 12),
        axis.title.y = element_text(face="plain", colour="black", size = 12),
        legend.title = element_text(face="plain", size = 10),
        legend.position = "right")

p<- plot_grid(plot1, plot2, labels = "")
p

Created on 2021-11-30 by the reprex package (v2.0.1)

CodePudding user response:

Not sure but the comman should be

 geom_line(aes(colour=Observed, linetype=Observed), size=1)

So for plot 1 I assume

plot1<-ggplot(data.frame(x = c(xlower , xupper)), aes(x = x))   
   xlim(c(xlower , xupper))   
   stat_function(fun = dNO, args =list(mu= 85.433,sigma=2.208), aes(colour = "Observed", linetype="Observed")) 
        stat_function(fun = dSN2, args =list(mu= 97.847,sigma=2.896,nu=5.882,log=FALSE) ,aes(colour = "CMCC.ESM2", , linetype="CMCC.ESM2")) 
        stat_function(fun = dIGAMMA, args =list(mu= 4.520,sigma=2.336,log=FALSE) , linetype=ECEARTH3,aes(colour = "ECEARTH3")) 
        stat_function(fun = dRG, args =list(mu= 93.062,sigma=2.233,log=FALSE) ,linetype=ECEARTH3.CC ,aes(colour = "ECEARTH3.CC")) 
        stat_function(fun = dRG, args =list(mu= 94.296,sigma=2.237,log=FALSE) ,aes(linetype="ECEARTH3.Veg", colour = "ECEARTH3.Veg")) 
        stat_function(fun = dSHASH, args =list(mu= 112.241,sigma=12.133,nu=9.419,tau=9.743,log=FALSE) , aes(linetype="GFDL.CM4",colour = "GFDL.CM4")) 
        stat_function(fun = dRG, args =list(mu= 101.101,sigma=2.372,log=FALSE)  ,aes(,linetype="GFDL.ESM4", colour = "GFDL.ESM4")) 
        stat_function(fun = dLO, args =list(mu= 73.086,sigma=1.485,log=FALSE) ,aes(, linetype="MPI.ESM1.2.HR", colour = "MPI.ESM1.2.HR")) 
        stat_function(fun = dNO, args =list(mu= 139.373,sigma=2.895) ,aes(,linetype="MRI.ESM2", colour = "MRI.ESM2")) 
        stat_function(fun = dLO, args =list(mu= 134.221,sigma=2.158,log=FALSE) , aes(linetype="NorESM2.MM", colour = "NorESM2.MM")) 
        stat_function(fun = dNO, args =list(mu= 107.372,sigma=2.232)  ,aes(,linetype="TaiESM1", colour = "TaiESM1")) 
        scale_color_manual("Data Types",breaks = c("Observed", "CMCC.ESM2","ECEARTH3","ECEARTH3.CC","ECEARTH3.Veg","GFDL.CM4","GFDL.ESM4","MPI.ESM1.2.HR","MRI.ESM2","NorESM2.MM","TaiESM1"),values = c("Black","green", "blue","brown","darkgreen","red","cyan","yellow4","aquamarine4","slateblue4","magenta4")) 
        labs(x = "Monthly average Precipitation (mm)", y = "PDF")   
        theme(plot.title = element_text(hjust = 0.5), 
              axis.title.x = element_text(face="plain", colour="black", size = 12),
              axis.title.y = element_text(face="plain", colour="black", size = 12),
              legend.title = element_text(face="plain", size = 10),
              legend.position = "none")
  • Related