yearmon修改ggplot2中的剧情

yearmon修改ggplot2中的剧情

本文介绍了使用动物园中的as.yearmon修改ggplot2中的剧情的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在ggplot2中使用动物园创建了一个图表来创建月份箱。但是,我想要修改图形,所以它看起来像一个标准的ggplot图。这意味着没有使用的垃圾箱被丢弃,并且填充整个垃圾箱空间的垃圾箱。这是我的代码:

I have created a graph in ggplot2 using zoo to create month bins. However, I want to be able to modify the graph so it looks like a standard ggplot graph. This means that the bins that aren't used are dropped and the bins that are populate the entire bin space. Here is my code:

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调用是:

The ggplot call is:

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

我不知道我失踪了,但我想知道!感谢提前!

I'm not sure what I'm missing, but I'd like to find out! Thanks in advance!

推荐答案

要获得标准图,将数据转换为标准数据类型,这是一个因素:

To get a "standard-looking" plot, convert the data to a "standard" data type, which is a factor:

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

这篇关于使用动物园中的as.yearmon修改ggplot2中的剧情的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:35