R语言数据展现——线图与时间序列谱图

2018.11.15 17:22
2523 0 0

  上文为大家介绍的是R语言中的散点图,相信大家已经对R语言有一定的了解了,接下来小编就给大家说说关于线图与时间序列谱图。

  一、给线图增加legend(图例)

  legend(1,300,legend=c("Tokyo","Berlin","New York","London"),

  lty=1,lwd=2,pch=21,col=c("black","red","orange","purple"),

  horiz=TRUE,bty="n",bg="yellow",cex=1,

  text.col=c("black","red","orange","purple"))

给线图增加legend.png

  二、画底纹格子

  rain<-read.csv("cityrain.csv")

  plot(rain$Tokyo,type="b",lwd=2,

  xaxt="n",ylim=c(0,300),col="black",

  xlab="Month",ylab="Rainfall(mm)",

  main="Monthly Rainfall in Tokyo")

  axis(1,at=1:length(rain$Month),labels=rain$Month)

  grid()

画底纹格子.png

  三、时间序列图

  sales<-read.csv("dailysales.csv")

  plot(sales$units~as.Date(sales$date,"%d/%m/%y"),type="l",xlab="Date",ylab="Units Sold")

时间序列图.png

  四、时间刻度可读化

  plot(air$nox~as.Date(air$date,"%d/%m/%Y %H:%M"),type="l",xaxt="n",xlab="Time", ylab="Concentration (ppb)",main="Time trend of Oxides of Nitrogen")

  xlabels<-strptime(air$date, format = "%d/%m/%Y %H:%M")

  axis.Date(1, at=xlabels[xlabels$mday==1], format="%b-%Y")

时间刻度可读化.png

  五、求出均值后画时间序列

  air$date = as.POSIXct(strptime(air$date, format = "%d/%m/%Y %H:%M“,"GMT"))

  means <- aggregate(air["nox"], format(air["date"],"%Y-%U"),mean,na.rm = TRUE)

  means$date <- seq(air$date[1], air$date[nrow(air)],length = nrow(means))

  plot(means$date, means$nox, type = "l")

求出均值后画时间序列.png

  六、抓取股票数据并画出趋势图

  aapl<-get.hist.quote(instrument = "aapl", quote = c("Cl", "Vol"))

  goog <- get.hist.quote(instrument = "goog", quote = c("Cl", "Vol"))

  msft <- get.hist.quote(instrument = "msft", quote = c("Cl", "Vol"))

  plot(msft$Close,main = "Stock Price Comparison",ylim=c(0,800), col="red", type="l", lwd=0.5,

  pch=19,cex=0.6, xlab="Date" ,ylab="Stock Price (USD)")

  lines(goog$Close,col="blue",lwd=0.5)

  lines(aapl$Close,col="gray",lwd=0.5)

  legend("top",horiz=T,legend=c("Microsoft","Google","Apple"),

  col=c("red","blue","gray"),lty=1,bty="n")

抓取股票数据并画出趋势图.png

  关于R语言数据展现——线图与时间序列谱图的讲述就到这里了,上述的内容只是截取了资料中的一部分,如需获取完整的资料,可通过关注微信公众号后联系客服获取。

  想要了解更多的资料、信息,可持续关注我们,我们将为大家提供有价值、有需求的材料。为大家在整理数据时省去烦恼。

投诉文章 ©著作权归作者所有
喜欢  |  0
0/200字
没有更多评论了~
悬赏问题
给科研问题设置一定金额,将更容易获得关注与回答哦~
  • 1元
  • 3元
  • 5元
  • 8元
  • 18元
  • 自定义
选择支付方式
  • 微信支付
  • 支付宝支付
  • 余额支付
Copyright © 2014-2019 晟斯医学 All Rights Reserved. 备案号:苏ICP备11037034号-5 版权所有:南京孜文信息咨询有限公司