我想知道如何在 2 天后更改堆疊條的顏色(幾何垂直線)?
結果,我想得到用黃色標記的圖形。
這是我的資料框中的一個示例,如果主題在兩天后繼續,則帶有指示器(標記為黃色)。

ggplot(aes(subjectID, endTime))
scale_fill_manual(values = c("red", "blue"))
geom_bar(stat="identity", aes(fill=factor(stage)))
geom_point(data=df, aes(subjectID, endTime, colour=responseType, shape=responseType), size=6)
coord_flip()
scale_colour_manual(values=c('#20A387FF','#2D708EFF'))
scale_y_continuous(limits=c(-0.2, 20), breaks=0:20)
labs(fill="Treatment status", # Add labels
x="Subject ID ", y="Days since hospital admission",
title="Patient trajectories",
caption="Hypothetical example")
geom_hline(yintercept=2, linetype="dashed", size=1) # , col=c('DC0000B2')
theme(plot.title = element_text(hjust = 0.5), # Put title in the middle of plot
plot.caption = element_text(size=7, hjust=0)) # Make caption size smaller
theme_minimal()
uj5u.com熱心網友回復:
我想我會這樣做geom_rect
ggplot(df, aes(subjectID, endTime))
scale_fill_manual(values = c("red", "blue"))
geom_col(aes(fill = factor(stage)))
geom_rect(aes(ymin = 2, ymax = endTime, xmin = as.numeric(subjectID) - 0.45,
xmax = as.numeric(subjectID) 0.45), fill = '#fff200',
data = df[df$isContinued,])
geom_point(aes(colour = responseType, shape = responseType), size = 6)
coord_flip()
scale_colour_manual(values = c('#20A387FF','#2D708EFF'))
scale_y_continuous(limits = c(-0.2, 20), breaks = 0:20)
labs(fill = "Treatment status",
x = "Subject ID ",
y = "Days since hospital admission",
title = "Patient trajectories",
caption ="Hypothetical example")
geom_hline(yintercept = 2, linetype = "dashed", size = 1)
theme_minimal()
theme(plot.title = element_text(hjust = 0.5),
plot.caption = element_text(size = 7, hjust = 0))

可重現格式的資料
df <- data.frame(subjectID = factor(1:10, 10:1),
stage = rep(c("treated", "untreated"), times = c(6, 4)),
startTime = 0,
endTime = c(8, 12, 1.7, 1.8, 15,17, 8, 14, 1, 1.5),
isContinued = rep(c(!0, !1, !0, !1), times = c(2, 2, 4, 2)),
responseType = rep(c("death", "discharge"), 5))
df
#> subjectID stage startTime endTime isContinued responseType
#> 1 1 treated 0 8.0 TRUE death
#> 2 2 treated 0 12.0 TRUE discharge
#> 3 3 treated 0 1.7 FALSE death
#> 4 4 treated 0 1.8 FALSE discharge
#> 5 5 treated 0 15.0 TRUE death
#> 6 6 treated 0 17.0 TRUE discharge
#> 7 7 untreated 0 8.0 TRUE death
#> 8 8 untreated 0 14.0 TRUE discharge
#> 9 9 untreated 0 1.0 FALSE death
#> 10 10 untreated 0 1.5 FALSE discharge
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/466702.html
下一篇:在磁盤內繪制隨機樣本
