使用动物园中的as.yearmon在ggplot2中修改Plot

时间:2014-04-09 16:35:03

标签: r date plot ggplot2 zoo

我在ggplot2中使用zoo创建了一个图表来创建月份箱。但是,我希望能够修改图形,使其看起来像标准的ggplot图形。这意味着没有使用的垃圾箱被丢弃,并且垃圾箱将填充整个垃圾箱空间。这是我的代码:

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()

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

1 个答案:

答案 0 :(得分:6)

获得标准的&#34;&#34;绘图,将数据转换为&#34;标准&#34;数据类型,这是一个因素:

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

enter image description here