Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
713 views
in Technique[技术] by (71.8m points)

r - Modifying Plot in ggplot2 using as.yearmon from zoo

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...