在ggplot2中,在X轴上格式化日期

时间:2022-11-30 09:36:04

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

我很难让x轴看起来正确。

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

这是我的数据(通过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"))

这个图表工作得很好——但是,如果我想调整日期的格式,我认为我应该添加以下内容:scale_x_date(标签= date_format(“%m-%Y”))

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

我试着把它做成日期标签是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.

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

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?

尽管我花了数小时研究了geom_line和geom_bar的格式,但我无法修复它。有人能解释一下我做错了什么吗?

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

编辑:作为一个后续的想法:你可以用日期作为一个因素,或者你应该用As。日期栏上的日期?

2 个解决方案

#1


39  

Can you use date as a factor?

你能把日期作为一个因素吗?

Yes, but you probably shouldn't.

是的,但是你可能不应该。

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

…或者应该用as。日期栏上的日期?

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

在ggplot2中,在X轴上格式化日期

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

在其中,我添加了stat = "identity"到您的地_bar调用。

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.

此外,关于binwidth的消息不是错误的。一个错误会在里面写上“error”,同样的一个警告也会在里面写上“warning”。否则它只是一个信息。

#2


26  

To show months as Jan 2017 Feb 2017 etc:

2017年1月至2017年2月

scale_x_date(date_breaks = "1 month", date_labels =  "%b %Y") 

Angle the dates if they take up too much space:

如果日期占用了太多的空间,调整一下角度:

theme(axis.text.x=element_text(angle=60, hjust=1))

#1


39  

Can you use date as a factor?

你能把日期作为一个因素吗?

Yes, but you probably shouldn't.

是的,但是你可能不应该。

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

…或者应该用as。日期栏上的日期?

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

在ggplot2中,在X轴上格式化日期

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

在其中,我添加了stat = "identity"到您的地_bar调用。

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.

此外,关于binwidth的消息不是错误的。一个错误会在里面写上“error”,同样的一个警告也会在里面写上“warning”。否则它只是一个信息。

#2


26  

To show months as Jan 2017 Feb 2017 etc:

2017年1月至2017年2月

scale_x_date(date_breaks = "1 month", date_labels =  "%b %Y") 

Angle the dates if they take up too much space:

如果日期占用了太多的空间,调整一下角度:

theme(axis.text.x=element_text(angle=60, hjust=1))