r - 使用 ggplot 绘制月度数据

标签 r ggplot2

我试图使用 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)

enter image description here

您可能会考虑在日期转换前后发布您的数据(使用 dput() )以帮助人们重现您的问题。

关于r - 使用 ggplot 绘制月度数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13096195/

相关文章:

string - 在 R 中实现 ROT-13

r - 无法编译 r markdown 代码

R:如何过滤/平滑二进制信号

r - 如何在 R 中绘制拷贝数变异曲线?

r - 使用 gridExtra 排列许多图

r - ggplot2 stat_密度_2d 分组时无法正常工作

r - qqplot + stat_smooth 错误

r - 将R数据作为csv直接写入s3

r - 在 R 脚本中执行批处理文件

r - ggplot中的堆栈点