R语言-在一张图上显示多条线的实现
作者:张琪Andrea-E
这篇文章主要介绍了R语言-在一张图上显示多条线的实现,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
查询百度之后,发现在R上一次显示多张图的函数很多,比如layout()或者分屏函数,但是这些都不是我想要的结果。
之后,发现了line()函数可以保留原来图片继续作图,在括号中填入所需画图的部分即可(我是将一个矩阵作图)。
先运行plot()函数,再注释掉plot()函数运行line()函数即可。
效果如下:
补充: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"))
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。如有错误或未考虑完全的地方,望不吝赐教。