带有日期的堆积geom_bar图

问题描述 投票:1回答:1

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

当我的约会是角色时,它正在工作,但后来我无法重新排序。所以我把它们改为日期,但现在有了上述问题。

here is the dput() of my data imported from an initial csv file

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"))

here is my ggplot code

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")

thanks very much for your help!

r ggplot2 geom-bar
1个回答
0
投票

我认为这里发生的事情是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")

enter image description here

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

© www.soinside.com 2019 - 2024. All rights reserved.