我有一个非常非常艰难的时间让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.frame")
Run Code Online (Sandbox Code Playgroud)
这是我想要绘制的图表:
ggplot(df, aes(x = Month, y = AvgVisits)) +
geom_bar() +
theme_bw() +
labs(x = "Month", y = "Average Visits per User")
Run Code Online (Sandbox Code Playgroud)
该图表工作正常 - 但是,如果我想调整日期的格式,我相信我应该添加这个:
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 = "Average Visits per User") +
scale_x_date(labels = date_format("%m-%Y"))
Run Code Online (Sandbox Code Playgroud)
当我绘制它时,我继续得到这个错误:
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
尽管对格式研究小时geom_line和geom_bar,我不能修复它.谁能解释我做错了什么?
编辑:作为后续想法:您可以使用日期作为因素,还是应该使用as.Date日期列?
jor*_*ran 61
你能用日期作为因素吗?
是的,但你可能不应该.
......或者你应该
as.Date在日期栏上使用?
是.
这导致我们:
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"))
Run Code Online (Sandbox Code Playgroud)

我加入stat = "identity"了你的geom_bar电话.
此外,有关binwidth的消息不是错误.错误实际上会在其中显示"错误",同样警告将始终在其中显示"警告".否则它只是一条消息.
Ody*_*aca 57
显示2017年1月2017年2月的月份等:
scale_x_date(date_breaks = "1 month", date_labels = "%b %Y")
Run Code Online (Sandbox Code Playgroud)
如果日期占用太多空间,请对其进行调整:
theme(axis.text.x=element_text(angle=60, hjust=1))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
83801 次 |
| 最近记录: |