我如何绘制以下数据的月度增长图:
A
2008-07-01 0
2008-08-01 87
2008-09-01 257
2008-10-01 294
2008-11-01 325
2008-12-01 299
(以 dput 格式,在约书亚追捕我并在我睡梦中谋杀我之前):
structure(c(0L, 87L, 257L, 294L, 325L, 299L), .indexCLASS = c("POSIXt",
"POSIXct"), .indexTZ = "", index = structure(c(1214884800, 1217563200,
1220241600, 1222833600, 1225512000, 1228107600), tzone = "", tclass = c("POSIXt",
"POSIXct")), .Dim = c(6L, 1L), .Dimnames = list(NULL, "A"), class = c("xts",
"zoo"))
最佳答案
定义增长:第一个区别?百分比?在任何一种情况下,只需计算然后绘制:
R> index(KB) <- as.Date(index(KB)) ## what you have are dates, not datetimes
R> barplot(diff(KB), ylab="Change in value", main="Growth")
您还可以使用标准线图:
R> plot(diff(KB), type='b', ylab="Change in value", main="Growth")
并更改
type=
该图的参数以显示条形等 pp。通常会绘制百分比变化,但鉴于您的第一个数据点,这在此处是 Not Acceptable (如 Gavin 所指出的),因此对于此插图而言存在差异。
关于R 在 XTS 对象上逐月增长的百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6876928/