如何在 R 中的 Swimmer ggplot 中的某个值后更改堆积条的颜色
How to change color of stacked bars after certain value in Swimmer ggplot in R
我想知道如何在 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()
我想我会用 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
我想知道如何在 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()
我想我会用 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