格式化ggplot2中的X轴上的日期 [英] Formatting dates on X axis in ggplot2

查看:148
本文介绍了格式化ggplot2中的X轴上的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常非常艰难的时间让x轴看起来对我的图形来说是正确的。



以下是我的数据(通过 dput())生成:

  df<  -  structure(list(Month = structure(1:12,.Label = c(2011-07-31,2011-08-31 ,2011-09-30,2011-10-31,2011-11-30,2011-12-31,2012-01-31,2012-02-29, 2012-03-31,2012-04-30,2012-05-31,2012-06-30),class =factor),AvgVisits = c(6.98655104580674,7.66045407330464,7.69761337479304,7.54387561322994 ,7.24483848458728,6.32001400498928,6.66794871794872,7.207780853854,7.60281201431308,6.70113837397123,6.57634103019538,6.75321935568936)),.Names = c(Month,AvgVisits),row.names = c(NA,-12L),class =data。框架)

下面是我试图绘制的图表:

  ggplot(df,aes(x = Month,y = AvgVisits))+ 
geom_bar()+
theme_bw()+
labs(x =Month,y =每位用户的平均访问次数)

工作正常 - 但是,如果我想调整t的格式他约会,我相信我应该补充:
scale_x_date(labels = date_format(%m-%Y))



我试图让它的日期标签是'MMM-YYYY'

  ggplot( df,aes(x = Month,y = AvgVisits))+ 
geom_bar()+
theme_bw()+
labs(x =Month,y =每用户平均访问量 )+
scale_x_date(labels = date_format(%m-%Y))

当我绘制该图,我继续得到这个错误:

stat_bin:binwidth默认为范围/ 30。使用'binwidth = x'来调整它。



尽管花了几个小时对 geom_line geom_bar ,我无法修复它。任何人都可以解释我做错了什么?

编辑:作为后续想法:您可以使用date作为因子,还是应该使用 as.Date 在日期列上?

解决方案


您可以使用date作为因子吗?


是的,但您可能不应该。


...还是应该在日期列上使用 as.Date


是的。



这导致我们这样做:

 <$ c $ (df,aes(x = Month,y = AvgVisits))+ 
geom_bar(df,aes(x = Month,y = AvgVisits))
df $ Month < - as.Date(df $ Month)统计=身份)+
theme_bw()+
labs(x =Month,y =每个用户的平均访问量)+
scale_x_date(labels = date_format(%m - %Y))

stat =identity

c $ c>到您的 geom_bar 调用。

另外,关于binwidth的消息不是错误。一个错误实际上会在其中说出错误,并且类似地,警告总是会在其中说出警告。否则,这只是一条消息。


I'm having a very, very tough time getting the x-axis to look correct for my graphs.

Here is my data (generated via dput()):

df <- structure(list(Month = structure(1:12, .Label = c("2011-07-31", "2011-08-31", "2011-09-30", "2011-10-31", "2011-11-30", "2011-12-31", "2012-01-31", "2012-02-29", "2012-03-31", "2012-04-30", "2012-05-31", "2012-06-30"), class = "factor"), AvgVisits = c(6.98655104580674,7.66045407330464, 7.69761337479304, 7.54387561322994, 7.24483848458728, 6.32001400498928, 6.66794871794872, 7.207780853854, 7.60281201431308, 6.70113837397123, 6.57634103019538, 6.75321935568936)), .Names = c("Month","AvgVisits"), row.names = c(NA, -12L), class = "data.frame")

Here is the chart I am trying to graph:

ggplot(df, aes(x = Month, y = AvgVisits)) + 
  geom_bar() +
  theme_bw() +
  labs(x = "Month", y = "Average Visits per User")

That chart works fine - but, if I want to adjust the formatting of the date, I believe I should add this: scale_x_date(labels = date_format("%m-%Y"))

I'm trying to make it so the date labels are 'MMM-YYYY'

ggplot(df, aes(x = Month, y = AvgVisits)) + 
  geom_bar() +
  theme_bw() +
  labs(x = "Month", y = "Average Visits per User") +
  scale_x_date(labels = date_format("%m-%Y"))

When I plot that, I continue to get this error:

stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.

Despite hours of research on formatting of geom_line and geom_bar, I can't fix it. Can anyone explain what I'm doing wrong?

Edit: As a follow-up thought: Can you use date as a factor, or should you use as.Date on a date column?

解决方案

Can you use date as a factor?

Yes, but you probably shouldn't.

...or should you use as.Date on a date column?

Yes.

Which leads us to this:

library(scales)
df$Month <- as.Date(df$Month)
ggplot(df, aes(x = Month, y = AvgVisits)) + 
  geom_bar(stat = "identity") +
  theme_bw() +
  labs(x = "Month", y = "Average Visits per User") +
  scale_x_date(labels = date_format("%m-%Y"))

in which I've added stat = "identity" to your geom_bar call.

In addition, the message about the binwidth wasn't an error. An error will actually say "Error" in it, and similarly a warning will always say "Warning" in it. Otherwise it's just a message.

这篇关于格式化ggplot2中的X轴上的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆