R Ggplot2显示了错误的月份

R Ggplot2显示了错误的月份,r,ggplot2,R,Ggplot2,我有下表 df <- structure(list(Month = structure(1:12, .Label = c("2021-01-31", "2021-02-28", "2021-03-31", "2021-04-30", "2021-05-31", "2021-06-30", "2012-01-31", "2012-02-29&q

我有下表

df <- structure(list(Month = structure(1:12, .Label = c("2021-01-31", "2021-02-28", "2021-03-31", "2021-04-30", "2021-05-31", "2021-06-30", "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")

df$Month <- as.Date(df$Month)

可能从
润滑油中使用

library(lubridate)
library(ggplot2)
library(dplyr)
df %>% 
   filter(Month>as.Date("2021-01-01")) %>% 
   mutate(Month = floor_date(Month, 'month')) %>% 
   ggplot(aes(x=Month, y=AvgVisits))+ 
   geom_col()+ 
   scale_x_date(date_labels = "%b-%Y")
-输出

可能从
润滑油中使用

library(lubridate)
library(ggplot2)
library(dplyr)
df %>% 
   filter(Month>as.Date("2021-01-01")) %>% 
   mutate(Month = floor_date(Month, 'month')) %>% 
   ggplot(aes(x=Month, y=AvgVisits))+ 
   geom_col()+ 
   scale_x_date(date_labels = "%b-%Y")
-输出


您的日期是在每个月底,因此该图绘制的是“最近的”月份,即您的第一个栏位于1月31日的中心,但“最近的”标签是2月1日(这是人们通常想要的)。你可以很容易地改变你的绘图以适应,例如,@akrun在下面的答案你的日期是在每个月底,因此图表是绘制“最近的”月份,即你的第一个条形图集中在1月31日,但“最近的”标签是2月1日(这是人们通常想要的)。你可以很容易地改变你的情节来适应,例如下面@akrun的回答