我有一個非常非常艱難的時間,讓x軸看起來對我的圖是正確的。格式化ggplot2中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")
這裏是我試圖繪製圖表:
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"))
我想使它的日期標籤是'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"))
當我繪製的是,我不斷地得到這個錯誤:
stat_bin: binwidth defaulted to range/30. Use 'binwidth = x' to adjust this.
儘管對geom_line
和geom_bar
格式研究小時,我無法修復它。任何人都可以解釋我做錯了什麼?
編輯:作爲後續的想法:您可以使用日期作爲因子,還是應該在日期列上使用as.Date
?
已加載'庫(秤)'? – smillig 2012-07-31 20:31:31
@smillig yes - 'scales'已加載。 – mikebmassey 2012-07-31 20:32:36