R語言-在一張圖上顯示多條線的實現

查詢百度之後,發現在R上一次顯示多張圖的函數很多,比如layout()或者分屏函數,但是這些都不是我想要的結果。

之後,發現瞭line()函數可以保留原來圖片繼續作圖,在括號中填入所需畫圖的部分即可(我是將一個矩陣作圖)。

先運行plot()函數,再註釋掉plot()函數運行line()函數即可。

效果如下:

20

補充:R語言:在同一張圖作不同曲線

R語言將兩條曲線作在同一張圖的方法是

library(ggplot2)
year<-c(1993,1998,2003,2008)
Res<-c(0.0227,0.0198,0.0155,0.0147)
COPD<-c(0.0138,0.0125,0.0075,0.0069)

#R語言基礎包作圖
plot(year,Res,col=2,type="b",ylim=c(0.005,0.035),xlab = "Year",ylab ="Morbidity")
lines(year,COPD,col=3,type="b")
legend("topright",pch=c(15,15),legend=c("Respiratory","COPD"),col=c(2,3),bty="n")

#ggplot包作圖
dd <- data.frame(year,Res,COPD)
ggplot()+geom_line(data = dd,aes(x = year,y = Res,colour = "Respiratory"),size=1)+
  geom_point(data = dd,aes(x = year,y = Res,colour = "Respiratory"),size=3)+
  ylim(0.005,0.025)+
  geom_line(data = dd,aes(x = year,y = COPD,colour ="COPD"),size=1) + 
  geom_point(data = dd,aes(x = year,y = COPD,colour = "COPD"),size=3)+
  scale_colour_manual("",values = c("Respiratory" = "red","COPD" = "green"))+
  xlab("Year")+ylab("Morbidity")+
  theme(text=element_text(size=13, family="Comic Sans MS"))

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀:

    None Found