我试图使用 ggplot 来绘制看起来像这样的月度数据
count date 6 Oct 2010 23 Nov 2010 20 Dec 2010 16 Jan 2011 64 Oct 2011 ... 425 Jul 2012 436 Aug 2012 405 Sep 2012
where date is of yearmon class from the zoo package.
This was my call:
ggplot(data, aes(x=date, y=count))+geom_line()
出现了这个错误:错误:提供给连续刻度的离散值。
所以ggplot不支持yearmon类,这很好。
然后我尝试将 yearmon 转换为 Date。现在数据看起来像这样:
计数日期
6 2010-10-01
23 2010-11-01
20 2010-12-01
16 2011-01-01
64 2011-10-01
...
425 2012-07-01
436 2012-08-01
405 2012-09-01
我打了同样的电话,这是 the resulting plot (对不起href...新用户不允许发布图片)
绘图末尾有一个不应该出现的下降,因为 data$count 在最后几行具有相似的值。
有没有人对此有好的解决方案?
谢谢阅读,
账单
最佳答案
我无法重现该线意外掉落在图右侧的问题。这是我使用的代码和输出:
library(ggplot2)
dat = read.table(header=TRUE, colClasses=c("numeric", "Date"),
text=" count date
6 2010-10-01
23 2010-11-01
20 2010-12-01
16 2011-01-01
64 2011-10-01
425 2012-07-01
436 2012-08-01
405 2012-09-01")
plot_1 = ggplot(dat, aes(x=date, y=count)) + geom_line()
ggsave("plot_1.png", plot_1, height=4.5, width=4.5)
您可能会考虑在日期转换前后发布您的数据(使用
dput()
)以帮助人们重现您的问题。
关于r - 使用 ggplot 绘制月度数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13096195/