下面我模擬了一個資料集,其中在 5 個不同的日子(一個每天有 200 個新人的新組)分配給 5 組人。TrialStartDate表示分配給每個人的日期 ( ID), TrialEndDate 表示每個人完成分配的時間。
set.seed(123)
data <-
data.frame(
TrialStartDate = rep(c(sample(seq(as.Date('2019/02/01'), as.Date('2019/02/15'), by="day"), 5)), each = 200),
TrialFinishDate = sample(seq(as.Date('2019/02/01'), as.Date('2019/02/15'), by = "day"), 1000,replace = T),
ID = seq(1,1000, 1)
)
我有興趣比較個人完成試驗所需的時間取決于他們開始試驗的時間(即,假設TrialStartDate對完成試驗所需的時間有影響)。
為了可視化這一點,我想制作一個條形圖,顯示ID每個TrialFinishDate條形上的s計數TrialStartDate(因為每個條形都TrialStartDate充當分組變數)。到目前為止,我想出的最好的方法是這樣的刻面:
data%>%
group_by(TrialStartDate, TrialFinishDate)%>%
count()%>%
ggplot(aes(x = TrialFinishDate, y = n, col = factor(TrialStartDate), fill = factor(TrialStartDate)))
geom_bar(stat = "identity")
facet_wrap(~TrialStartDate, ncol = 1)

但是,我還想在每個方面添加一條垂直線,顯示TrialStartDate每個組的時間(最好與條形顏色相同)。嘗試添加垂直線時geom_vline,會將所有線添加到每個方面:
data%>%
group_by(TrialStartDate, TrialFinishDate)%>%
count()%>%
ggplot(aes(x = TrialFinishDate, y = n, col = factor(TrialStartDate), fill = factor(TrialStartDate)))
geom_bar(stat = "identity")
geom_vline(xintercept = unique(data$TrialStartDate))
facet_wrap(~TrialStartDate, ncol = 1)

我們如何才能使每個方面中各個組的垂直線獨一無二?
uj5u.com熱心網友回復:
您在 之外指定 xintercept aes,因此不尊重分面。
這應該可以解決問題:
data %>%
group_by(TrialStartDate, TrialFinishDate)%>%
count()%>%
ggplot(aes(x = TrialFinishDate, y = n, col = factor(TrialStartDate), fill = factor(TrialStartDate)))
geom_bar(stat = "identity")
geom_vline(aes(xintercept = TrialStartDate))
facet_wrap(~TrialStartDate, ncol = 1)
筆記 geom_vline(aes(xintercept = TrialStartDate))
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/394818.html
上一篇:在R中設定線性規劃調度問題
下一篇:在R中合并具有相同標題的幾列
