在這里,我嘗試使用 ggplot2 和 stat_function 繪制包含 11 個變數的 PDF。我設法按顏色添加圖例,但我還想按線型區分線條并將其合并到圖例中。如果有人可以幫助我解決問題,我會很感激嗎?這是代碼:
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 = "")
uj5u.com熱心網友回復:
我減少了繪制的統計資料數量,以便更容易了解發生了什么。
由于兩個圖的圖例相同,因此您只需要一組scale_x_manual.
線型和顏色都需要在對 aes 的呼叫中才能出現在圖例中。
要合并兩個圖例,請在呼叫labs.
您可能會發現需要定義其他線型,因為標準集包含 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

由reprex 包(v2.0.1)于 2021 年 11 月 30 日創建
uj5u.com熱心網友回復:
不確定,但命令應該是
geom_line(aes(colour=Observed, linetype=Observed), size=1)
所以對于plot 1和plot 2
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")
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/371948.html
