我正在尝试创建一个堆叠的geom_bar图,该图按组(按日期(每月))显示会话的累计数量。由于某些原因,即使我的x变量日期都从2016-11-01开始并在2019-02-01结束,两组的绘图都从2015-12-01(Dec-2015)开始,并且所有值都聚集在1月16日,1月17日...等等。
当我的日期是字符时,它可以工作,但后来我无法重新排序。因此,我将它们更改为日期,但现在遇到了上述问题。
icon="icon.png"
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"))
答案 0 :(得分: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")
但是更好的解决方法是在导入数据时使日期格式正确。