【问题标题】:Modifying Plot in ggplot2 using as.yearmon from zoo使用来自动物园的 as.yearmon 修改 ggplot2 中的绘图
【发布时间】:2014-05-23 00:26:28
【问题描述】:

我在 ggplot2 中创建了一个图表,使用 zoo 来创建月份箱。但是,我希望能够修改该图,使其看起来像一个标准的 ggplot 图。这意味着未使用的 bin 将被删除,而填充的 bin 将填充整个 bin 空间。这是我的代码:

library(data.table)
library(ggplot2)
library(scales)
library(zoo)

testset <- data.table(Date=as.Date(c("2013-07-02","2013-08-03","2013-09-04","2013-10-05","2013-11-06","2013-07-03","2013-08-04","2013-09-05","2013-10-06","2013-11-07")), 
           Action = c("A","B","C","D","E","B","A","B","C","A","B","E","E","C","A"), 
           rating = runif(30))

ggplot 调用是:

ggplot(testset, aes(as.yearmon(Date), fill=Action)) + 
           geom_bar(position = "dodge") + 
           scale_x_yearmon()

我不确定我错过了什么,但我想知道!提前致谢!

【问题讨论】:

    标签: r date plot ggplot2 zoo


    【解决方案1】:

    要获得“标准外观”的图,请将数据转换为“标准”数据类型,这是一个因素:

    ggplot(testset, aes(as.factor(as.yearmon(Date)), fill=Action)) + 
        geom_bar(position='dodge') 
    

    【讨论】:

      猜你喜欢
      • 2013-11-16
      • 1970-01-01
      • 2018-01-07
      • 1970-01-01
      • 2015-04-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多