我就废话不多说了,大家还是直接看代码吧~
1
2
3
4
5
|
ts1<-ts(test_data$tot_num,frequency = 365,start=c(2017,11,21)) plot(ts1,col= 'blue' ,lty= 'dotted' ,ylim=c(50,550)) par(new=TRUE) ts2<-ts(test_data$pre_result,frequency = 365,start=c(2017,11,21)) plot(ts2,col= 'red' ,ylim=c(50,550)) |
好用,需要注意控制相同的xlim和ylim
补充:用R语言在同一副图里面画两个折线图的注意事项
如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
setwd( "C:/Users/11565/Desktop/合并二" ) shuju <- read .csv( "数据.csv" ,header = T) library(psych) head (shuju) attach(shuju) head (mtcars) aggregate(shuju,by= list(state),FUN = mean,na. rm = T) tapply(shuju[,3],shuju$year,mean) #每次只能求一列 aggregate(shuju[,3:4],list(shuju[,2]),mean) #每次可以求多列 mean_1 <- aggregate(shuju[,3:6],list(shuju[,2]),mean) detach(shuju) attach(mean_1) head (mean_1) mean_1$pcap <- mean_1$pcap /10 mean_1$Group.1<- as.numeric(mean_1$Group.1) plot(Group.1,pcap,lty= 3,pch = 21,ylim = c(1000,3000)) #画不出两幅图的原因很可能是两个变量数量级存在问题 lines(Group.1,hwy /10 ,lty = 2,pch = 22) #画折现图要先看一下x对应的y和y的数量级如何 str(mean_1) |
补充:在R中把多条曲线放置在一张图中
如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
library(xlsx) myield<- read .xlsx( "myield.xlsx" ,header=T,sheetIndex=1) head (myield) time X3m X6m X1y X2y X3y X4y 1 2002.010.019889 0.020353 0.021264 0.023014 0.024670 0.026232 2 2002.020.020781 0.021131 0.021819 0.023155 0.024437 0.025663 3 2002.030.018449 0.018829 0.019574 0.021015 0.022389 0.023695 4 2002.040.019096 0.019325 0.019780 0.020668 0.021529 0.022362 5 2002.050.018228 0.018425 0.018815 0.019578 0.020319 0.021037 6 2002.060.017224 0.017479 0.017980 0.018952 0.019882 0.020771 tm<-myield[,2] sm<-myield[,3] oy<-myield[,4] ty<-myield[,5] time <-myield[,1] thy<-myield[,6] fy<-myield[,7] plot(tm~ time , type = "l" ,lty=1) lines(sm~ time ,col= "red" ,lty=2) lines(oy~ time ,col= "blue" ,lty=3) lines(ty~ time ,col= "yellow" ,lty=4) lines(thy~ time ,col= "green" ,lty=5) lines(fy~ time ,col= "grey" ,lty=6) title( "YIELD(m)" ,lwd=3) legend( "topleft" ,cex=.6,c( "tm" , "sm" , "oy" , "ty" , "thy" , "fy" ),col=c( "black" , "red" , "blue" , "yellow" , "green" , "grey" ),lty=1:6) |
也可以应用matplot函数,但使用之前注意把数值型数据转换为数据框或者向量,根据本例,即把tm,sm,oy等数值型数据转化为数据框,
1
|
z<-data.frame(cbind(tm,sm,oy,ty,thy,fy)) |
然后
1
|
matplot( time ,z,col=1:6, type = "l" ,lwd=2,xlab= "" ,ylab= "" ,lty=1:5) |
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。
原文链接:https://blog.csdn.net/Jinpeijie217/article/details/88991235