带日期的堆叠 geom_bar 图

Stacked geom_bar plot with dates

我正在尝试按组创建按日期(每月)累计会话数的堆叠 geom_bar 图。出于某种原因,即使我的 x 变量日期从 2016-11-01 开始并在 2019-02-01 结束,这两个组的情节是从 2015-12-01(2015 年 12 月)开始的,并且这些值都集中在1 月 16 日、1 月 17 日...等等

当我的约会对象是角色时,它可以正常工作,但后来我无法重新排序。所以我将它们更改为日期,但现在遇到了上述问题。

这是我从初始 csv 文件导入的数据的 dput()

recruitment_tally<-structure(list(dates = structure(c(16811, 16812, 17167, 17168, 
                                   17169, 17170, 17171, 17172, 17173, 17174, 17175, 17176, 17177, 
                                   17178, 17532, 17533, 17534, 17535, 17536, 17537, 17538, 17539, 
                                   17540, 17541, 17542, 17543, 17897, 17898, 17899, 16811, 16812, 
                                   17167, 17168, 17169, 17170, 17171, 17172, 17173, 17174, 17175, 
                                   17176, 17177, 17178, 17532, 17533, 17534, 17535, 17536, 17537, 
                                   17538, 17539, 17540, 17541, 17542, 17543, 17897, 17898, 17899
), class = "Date"), group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
                                        1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                        2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
                                        2L, 2L, 2L, 2L), .Label = c("control", "mtbi"), class = "factor"), 
total_sessions = c(4, 8, 11, 15, 19, 21, 27, 33, 35, 38, 
                   41, 44, 47, 48, 51, 53, 56, 58, 59, 62, 63, 63, 66, 67, 69, 
                   70, 71, 72, 73, 0, 0, 0, 2, 3, 5, 8, 10, 15, 18, 20, 27, 
                   28, 28, 32, 34, 36, 36, 39, 41, 41, 43, 49, 50, 53, 57, 58, 
                   60, 63)), row.names = c(NA, -58L), spec = structure(list(
                     cols = list(date = structure(list(), class = c("collector_character", 
                                                                    "collector")), group = structure(list(), class = c("collector_character", 
                                                                                                                       "collector")), culm_total = structure(list(), class = c("collector_double", 
                                                                                                                                                                               "collector"))), default = structure(list(), class = c("collector_guess", 
                                                                                                                                                                                                                                     "collector"))), class = "col_spec"), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                    "tbl", "data.frame"))

这是我的 ggplot 代码

library(ggplot2)

base<- recruitment_tally %>%
        ggplot()+
        geom_bar(aes(y = total_sessions, x= dates, fill = group), 
        stat="identity",position="dodge") +
        coord_flip()



base + scale_x_date(date_breaks = "month", date_labels = "%b%y")

非常感谢您的帮助!

我认为这里发生的情况是 CSV 导入后日期与预期不符。

示例数据中的日期似乎是每个月的前 12 天。我假设您想要的是一年中 12 个月中每个月的第一天。我怀疑在某个地方,年-日-月格式的日期变成了年-月-日。

您可以像这样使用您的数据解决此问题:

recruitment_tally %>% 
  mutate(dates = as.Date(as.character(dates), "%Y-%d-%m")) %>% 
  ggplot(aes(dates, total_sessions)) + 
    geom_col(aes(fill = group)) + 
    coord_flip() + 
    scale_x_date(date_labels = "%b %Y")

但更好的解决方法是在导入数据时正确设置日期格式。