【问题标题】:How to plot certain columns in R [xts]?如何在 R [xts] 中绘制某些列?
【发布时间】:2019-01-08 22:53:38
【问题描述】:

我有一些看起来像这样的数据: https://imgur.com/a/UK64GCp

我正在使用:

plot(fifty_twoweekmovavg)
pdf("52_week_moving_average_chartNSW.pdf",onefile=TRUE)
addLegend("topleft",lty = 1,cex=1.2)
dev.off()

如何绘制它以便只包含几个变量? 例如。绘制新南威尔士州价格和煤炭价格与时间的关系,而不是绘制每个变量与时间的关系?

谢谢

可重现的例子:

NSW1.Price Black.Coal Gas Hydro Liquid.Fuel 2011-01-01 30.89336 32.33668 41.63653 69.82661 108.06855 2011-01-08 30.98103 32.24805 41.33295 69.44308 104.36587 2011-01-15 30.73076 32.11497 40.76273 69.59129 97.30812 2011-01-22 30.76028 30.50381 36.56215 62.50329 61.78828 2011-01-29 29.76733 34.65090 43.94289 93.20954 113.42410

Edit2,我如何创建数据:

mydata=read.csv(file="nem_tech_dataTAS.csv") library(xts) library(zoo) date <- seq(from=as.POSIXct("2010-01-01 00:30", format = "%Y-%m-%d %H:%M"), length.out = nrow(mydata), by = "30 min") mydata_dated <- xts(mydata, date) fifty_twoweekmovavg=rollapply(mydata_dated,17520,mean,by = 336,na.pad = FALSE)

Edit3,图例格式:

当前图例:

想要的图例:

【问题讨论】:

  • 尝试使用ggplot 进行绘图
  • plot(fifty_twoweekmovavg$Gas,fifty_twoweekmovavg$Hydro)
  • 当我这样做时,它只绘制天然气数据而不是水电数据?
  • 你看过linesmatplot函数吗?
  • 我尝试过线条,但我无法让它与我的图例一起使用?你能帮帮我吗

标签: r xts zoo graphing


【解决方案1】:

如果包xts 已加载,则可以使用 plot 命令绘制 xts 对象。有关如何绘制 xts 对象的更多详细信息,请使用?plot.xts

要仅选择 2 列,您可以在 xts 对象内使用grep

library(xts)

plot(fifty_twoweekmovavg[, grep("NSW1|Coal", names(fifty_twoweekmovavg))],
     legend(grep("NSW1|Coal", names(fifty_twoweekmovavg))),
     main = "52_week_moving_average",
     legend.loc = "topleft")

编辑: 操作图例,这会更容易,并且会产生相同的图,但图例使用线条而不是正方形:

plot(fifty_twoweekmovavg[, grep("NSW1|Coal", names(fifty_twoweekmovavg))],
     main = "52_week_moving_average")

# on = 1 is for main plot. lty is for showing a line in the legend.
# see ?addLegend and ?legend
addLegend("topleft", on = 1, lty=1)

【讨论】:

  • 哦,太好了,我试试这个
  • 工作完美,谢谢,虽然我不完全理解“grep”的工作原理
  • 一件小事,但如果可以的话,我想修复它是图例的格式。使用您的代码,我的图例有一个指示每种颜色的框,而最初我有一条指示每种颜色的线。我在上面编辑了我的问题来说明这一点。如何使用线条格式而不是您的框格式来格式化图例?谢谢!
【解决方案2】:

如果您想绘制两条线来显示一段时间内的 Gas 和 Hydro 值,您首先需要创建一个时间序列。创建一个获取日期并使用as.Date 将其转换为日期格式的列。在你上面的例子中,你会写:

fifty_twoweekmovavg$date=as.Date(rownames(fifty_twoweekmovavg))

这会让你得到你的 x 轴值。

现在,为了获得您的 Gas 和 Hydro 值,您必须确保 y 轴适合它们,因为 Gas 和 Hydro 的值不相交。

这样做的一种方法是:

extents=range(c(fifty_twoweekmovavg$Gas,fifty_twoweekmovavg$Hydro))

一旦你设置了日期和范围,你最终可以继续绘制你的线条:

plot(fifty_twoweekmovavg$date,fifty_twoweekmovavg$Gas,type='l',ylim=extents)
lines(fifty_twoweekmovavg$date,fifty_twoweekmovavg$Hydro,col='red')

【讨论】:

  • 谢谢,但是当我做你的第一行时,我得到了这个:&gt; fifty_twoweekmovavg$date=as.Date(rownames(fifty_twoweekmovavg)) Error in as.Date.default(x, ...) : do not know how to convert 'x' to class “Date”
  • 你能试试str(fifty_twoweekmovavg) 复制粘贴你得到的东西吗?
  • An ‘xts’ object on 2011-01-01/2018-12-29 containing: Data: num [1:418, 1:6] 30.9 30.9 30.7 30.6 30.6 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:6] "TAS1.Price" "Gas" "Hydro" "Liquid.Fuel" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL
  • 啊,你能展示一下你用来创建 XTS 对象的代码吗? This question 可能对你有用。
  • 是的,现在在原来的问题中
【解决方案3】:

下面的代码可以用图例渲染多条线的图。

df2 = data.frame(matrix(data=c(
30.89336,   32.33668, 41.63653,  69.82661,   108.06855,
30.98103,   32.24805, 41.33295,  69.44308,   104.36587,
30.73076,   32.11497, 40.76273,  69.59129,    97.30812,
30.76028,   30.50381, 36.56215,  62.50329,    61.78828,
29.76733,   34.65090, 43.94289,  93.20954,   113.42410
), ncol = 5, byrow = TRUE ))
colnames(df2) = c("NSW1.Price", "Black.Coal", "Gas", "Hydro", "Liquid.Fuel")
df1 = data.frame("time" = as.Date(
      c("2011-01-01",   
        "2011-01-08",   
        "2011-01-15",   
        "2011-01-22",   
        "2011-01-29"),"%Y-%m-%d"))
df = cbind(df1, df2)
plot(0, cex=0, xlim=range(df$time), 
     ylim=c(0,max(c(df$NSW1.Price, df$Black.Coal))))
lines(df$time, df$NSW1.Price, col="cyan", lty = 1)
lines(df$time, df$Black.Coal, col="black", lty=2)
legend("bottomleft", legend = c("NSW1.Price", "Black.Coal"), 
       col = c("cyan", "black"), lty = c(1,2))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 2016-05-14
    • 2018-04-08
    • 2021-06-20
    • 1970-01-01
    • 1970-01-01
    • 2017-03-27
    相关资源
    最近更新 更多