R 游泳运动员生存图GG图(按持续时间用颜色编码的事件)
关于游泳者瀑布图的时间轴的一个问题 我使用下面的代码生成了游泳者图> 但是,我希望根据数据集中的responseStartTime和responseEndTime持续时间,使用responseType(而不是“阶段”)为每个主题的条形图上色。请建议如何在响应持续时间内按responseType定义颜色 谢谢 资料来源:() 数据代码R 游泳运动员生存图GG图(按持续时间用颜色编码的事件),r,ggplot2,survival-analysis,geom-bar,aesthetics,R,Ggplot2,Survival Analysis,Geom Bar,Aesthetics,关于游泳者瀑布图的时间轴的一个问题 我使用下面的代码生成了游泳者图> 但是,我希望根据数据集中的responseStartTime和responseEndTime持续时间,使用responseType(而不是“阶段”)为每个主题的条形图上色。请建议如何在响应持续时间内按responseType定义颜色 谢谢 资料来源:() 数据代码 很抱歉回答得太晚,但我认为这是一个非常有趣的问题,所以即使你不再需要,我也会发布一个解决方案。我希望我明白你的意思 基本上,你必须有一个不同的方法和使用 如果你
很抱歉回答得太晚,但我认为这是一个非常有趣的问题,所以即使你不再需要,我也会发布一个解决方案。我希望我明白你的意思 基本上,你必须有一个不同的方法和使用 如果你这样做的话,解决方案是非常简单的。 唯一的问题是你没有明确你的目标:例如,如果
responseedTime
值为NA,您想做什么?或者您想保留或不保留条形图中显示的信息,因此我不得不做出任意选择,但您应该能够找出如何从该解决方案中获得所需信息:
df %>%
# Add a few variables to your df
dplyr::select(subjectID, stage, responseStartTime, responseEndTime,
endTime, responseType) %>%
# Remove duplicate rows
dplyr::distinct() %>%
# Order subject ID by numeric value
dplyr::mutate(
subjectID=forcats::fct_reorder(.f=subjectID,
.x=as.numeric(subjectID),
.desc = TRUE)) %>%
# Pipe into ggplot
ggplot(aes(subjectID, endTime)) + # Base axis
# substitute geom_bar by a geom_segment
geom_segment(aes(x = 0, xend = endTime, y=subjectID,
yend=subjectID, color = factor(stage)),
size = 12) +
# Substitue geom_point with another geom_segment for the responseTime part
geom_segment(aes(x = responseStartTime, xend = responseEndTime,
y=subjectID, yend=subjectID, color = responseType),
size =8) +
# don't need coord_flip anymore
# probably could improve this part but you got the idea
scale_colour_manual(values=c("#FFFFFF", "#000000",
"#F8766D", "#C49A00", "#53B400",
"#00C094")) +
# the y scale is now the x scale...
scale_x_continuous(limits=c(-0.5, 20), breaks=0:20) + # Set time limits
labs(fill="Disease Stage", colour="Symbol Key",
shape="Symbol Key", # Add labels
y="Subject ID ", x="Months since diagnosis",
title="Swimmer Plot",
caption="Durable defined as subject with six months or more of confirmed response") +
theme(plot.title = element_text(hjust = 0.5),
plot.caption = element_text(size=7, hjust=0))
代码在我的机器上运行良好。你确定你有所有软件包的最新更新版本并且没有其他错误吗?我在macOS v10.13.6上用R版本3.5.1和3.3.2试用过,它在两种情况下都有效…@Roman我认为这不是软件包的问题,因为数据代码的第一部分失败了,但没有库引用…@godot,我怀疑OP错过了他发布的错误信息。rk123:请重新启动R/Rstudio,按原样运行代码,并发布控制台的完整输出。非常感谢您的帮助。看来包装已经过时了。它可以与更新的软件包配合使用。
df %>%
# Get just the variables we need for the base of the plot
dplyr::select(subjectID, endTime, stage) %>%
# Remove duplicate rows
dplyr::distinct() %>%
# Order subject ID by numeric value
dplyr::mutate(subjectID=forcats::fct_reorder(.f=subjectID, .x=as.numeric(subjectID), .desc = TRUE)) %>%
# Pipe into ggplot
ggplot(aes(subjectID, endTime)) + # Base axis
geom_bar(stat="identity", aes(fill=factor(stage))) + # Bar plot. Colour by stage
geom_point(data=df.shapes, # Use df.shapes to add reponse points
aes(subjectID, time, colour=responseType, shape=responseType), size=5) +
coord_flip() + # Flip to horizonal bar plot.
scale_colour_manual(values=c(RColorBrewer::brewer.pal(3, "Set1")[1:2], # Add colours
rep("black", 3))) + # min of brewerpal is three but we only need 2.
scale_shape_manual(values=c(rep(unicode[["triangle"]], 2), # Add shapes
unicode[["circle"]], unicode[["square"]], unicode[["arrow"]])) +
scale_y_continuous(limits=c(-0.5, 20), breaks=0:20) + # Set time limits
labs(fill="Disease Stage", colour="Symbol Key", shape="Symbol Key", # Add labels
x="Subject ID ", y="Months since diagnosis",
title="Swimmer Plot",
caption="Durable defined as subject with six months or more of confirmed response") +
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
df %>%
# Add a few variables to your df
dplyr::select(subjectID, stage, responseStartTime, responseEndTime,
endTime, responseType) %>%
# Remove duplicate rows
dplyr::distinct() %>%
# Order subject ID by numeric value
dplyr::mutate(
subjectID=forcats::fct_reorder(.f=subjectID,
.x=as.numeric(subjectID),
.desc = TRUE)) %>%
# Pipe into ggplot
ggplot(aes(subjectID, endTime)) + # Base axis
# substitute geom_bar by a geom_segment
geom_segment(aes(x = 0, xend = endTime, y=subjectID,
yend=subjectID, color = factor(stage)),
size = 12) +
# Substitue geom_point with another geom_segment for the responseTime part
geom_segment(aes(x = responseStartTime, xend = responseEndTime,
y=subjectID, yend=subjectID, color = responseType),
size =8) +
# don't need coord_flip anymore
# probably could improve this part but you got the idea
scale_colour_manual(values=c("#FFFFFF", "#000000",
"#F8766D", "#C49A00", "#53B400",
"#00C094")) +
# the y scale is now the x scale...
scale_x_continuous(limits=c(-0.5, 20), breaks=0:20) + # Set time limits
labs(fill="Disease Stage", colour="Symbol Key",
shape="Symbol Key", # Add labels
y="Subject ID ", x="Months since diagnosis",
title="Swimmer Plot",
caption="Durable defined as subject with six months or more of confirmed response") +
theme(plot.title = element_text(hjust = 0.5),
plot.caption = element_text(size=7, hjust=0))