R:按月计算的平均每日计数的 ggplot

R: ggplot of average daily counts by month

我正在尝试按月绘制平均每日行程数。但是,我正在努力寻找如何在图中只包含每月每天的平均出行次数,而不是每月的总出行次数。

星期几和月份已经从数字类型转换为缩写,也已经排序(类型:)。

这是我尝试过的情节。

by_day <- df_temp %>%
  group_by(Start.Day)

ggplot(by_day, aes(x=Start.Month,
                    fill=Start.Month)) +
  geom_bar() +
  scale_fill_brewer(palette = "Paired") +
  labs(title="Number of Daily Trips by Month",
       x=" ",
       y="Number of Daily Trips")

这是我要复制的情节:

你快到了。由于您没有共享可重现的示例,因此我模拟了您的数据。您可能需要调整变量命名 and/or 更正我的假设。

{lubridate} 是一个强大的日期时间处理包。在处理日期和汇总日期等时,它会派上用场。

# simulating your data
## a series of dates from June through October
days <- seq(from = lubridate::ymd("2020-06-01")
            ,to  = lubridate::ymd("2020-10-30")
            ,by  = "1 day")
## random trips on each day
set.seed(666)
trips <- sample(2000:5000, length(days), replace = TRUE)

# putting things together in a data frame
df_temp <- data.frame(date = days, counts = trips) %>%
  # I assume the variable Start.Month is the monthly bin
  # let's use lubridate to "bin" the month from the date
  mutate(Start.Month = lubridate::floor_date(date, unit = "month"))

# aggregate trips for each month, calculate average daily trips
by_month <- df_temp %>%
  group_by(Start.Month) %>%            # group by the binning variable
  summarise(Avg.Trips = mean(counts))  # calculate the mean for each group

ggplot( data = by_month
      , aes(x = Start.Month, y = Avg.Trips
      , fill=as.factor(Start.Month))   # to work with a discrete palette, factorise
      ) +
# ------------ bar layer -----------------------------------------
## instead of geom_bar(... stat = "identity"), you can use geom_col()
## and define the fill colour
  geom_col() +  
  scale_fill_brewer(palette = "Paired") +

# ------------ if you like provide context with annotation -------
  geom_text(aes(label = Avg.Trips %>% round(2)), vjust = 1) +

# ------------ finalise plot with labels, theme, etc.

  labs(title="Number of Daily Trips by Month",
       x=NULL, # setting an unused lab to NULL is better than printing empty " "!
       y="Number of Daily Trips"
       ) + 
  theme_minimal() +
  theme(legend.position = "none")  # to suppress colour legend