简体   繁体   English

带日期的堆叠geom_bar图

[英]Stacked geom_bar plot with dates

I'm trying to create a stacked geom_bar plot of the cumulative number of sessions by date (per month) by group. 我正在尝试按日期(每月),按组创建堆积的geom_bar图表,该图表按日期(每月)显示会话的累计数量。 For some reason even though my x variable dates starts at 2016-11-01 and ends at 2019-02-01 for both groups the plot is starting at 2015-12-01 (Dec-2015) and the values are all clumping together at Jan-16, Jan-17... etc. 由于某些原因,即使我的x变量日期都从2016-11-01开始并在2019-02-01结束,两组的绘图都从2015-12-01(Dec-2015)开始,并且所有值都聚集在1月16日,1月17日...等等

When my dates were characters it was working, but then I couldn't reorder. 当我的日期是字符时,它可以工作,但是后来我无法重新排序。 So I changed them to dates, but are now having the above issue. 因此,我将它们更改为日期,但现在遇到了上述问题。

here is the dput() of my data imported from an initial csv file 这是从初始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"))

here is my ggplot code 这是我的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")

thanks very much for your help! 非常感谢您的帮助!

I think what has happened here is that the dates are not as expected after CSV import. 我认为这里发生的是CSV导入后的日期与预期的不一样。

The dates in your example data seem to be the first 12 days of each month. 示例数据中的日期似乎是每个月的前12天。 I assume that what you want is the first day for each of the 12 months of the year. 我假设您想要的是一年中12个月中的每一天的第一天。 I suspect that somewhere along the way, dates in year-day-month format became year-month-day. 我怀疑在此过程中的某处,以年-日-月-月格式表示的日期变成了年-月-日。

You can fix this using your data like this: 您可以使用以下数据来解决此问题:

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

在此处输入图片说明

But the better fix is to get the date format correct when importing the data. 但是更好的解决方法是在导入数据时使日期格式正确。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM