ggplot2画泳道图箭头如何显示
如何 显示 ggplot2 箭头
2023-06-13 09:15:10 时间
之前给大家分享了肿瘤领域常用的泳道图的画法:
图画出来了,基本符合要求,但是有一个小小的问题:箭头表示的信息没有展示出来。
众所周知,R语言里自带的形状是没有箭头的。
在这篇文章[1]中找到了答案,可以用Unicode符号。
我们还是用上次用过的df
数据继续演示。
suppressMessages(library(tidyverse))
set.seed(200)
df <- data.frame(Patient = 1:12,
Months = sample(6:30, 12, replace=TRUE),
Stage = sample(1:4, 12, replace=TRUE),
Continued = sample(0:1, 12, replace=TRUE))
df <- df |>
group_by(Patient) |>
mutate('Complete Response Start'=sample(c(6:(max(Months)-1),NA), 1,
prob=c(rep(1, length(6:(max(Months)-1))),5), replace=TRUE),
'Partial Response Start'=sample(c(6:(max(Months)-1),NA), 1,
prob=c(rep(1, length(6:(max(Months)-1))),5), replace=TRUE),
'Durable'=sample(c(-0.5,NA), 1, replace=TRUE),
'Response End'=sample(c(6:(max(Months)-1),NA), 1,
prob=c(rep(1, length(6:(max(Months)-1))),5), replace=TRUE)
)
# 数据大概这样
head(df)
## # A tibble: 6 × 8
## # Groups: Patient [6]
## Patient Months Stage Continued Complete Response Sta…¹ Parti…² Durable Respo…³
## <int> <int> <int> <int> <int> <int> <dbl> <int>
## 1 1 11 1 1 10 8 -0.5 NA
## 2 2 23 4 1 18 14 -0.5 17
## 3 3 20 2 0 NA NA -0.5 NA
## 4 4 13 3 0 6 NA -0.5 12
## 5 5 28 4 0 9 NA NA 23
## 6 6 17 4 0 NA 11 NA 9
## # … with abbreviated variable names ¹`Complete Response Start`,
## # ²`Partial Response Start`, ³`Response End`
变为长数据:
df_long <- df |>
mutate(Continued = case_when(Continued == 1 ~ Months+0.5)) |>
pivot_longer(4:8,names_to = "type",values_to = "value")
df_long$type <- factor(df_long$type,levels = c("Complete Response Start",
"Partial Response Start",
"Response End",
"Durable",
"Continued"))
head(df_long)
## # A tibble: 6 × 5
## # Groups: Patient [2]
## Patient Months Stage type value
## <int> <int> <int> <fct> <dbl>
## 1 1 11 1 Continued 11.5
## 2 1 11 1 Complete Response Start 10
## 3 1 11 1 Partial Response Start 8
## 4 1 11 1 Durable -0.5
## 5 1 11 1 Response End NA
## 6 2 23 4 Continued 23.5
p1 <- ggplot(df, aes(Months, fct_reorder(factor(Patient),Months)))+
geom_bar(stat = "identity", aes(fill=factor(Stage)), width = 0.7)+
geom_point(data = df_long,
aes(value, fct_reorder(factor(Patient),Months),color=type,shape=type),
size = 5
)
p1
plot of chunk unnamed-chunk-9
这个网站:https://unicode-table.com/en/blocks/, 可以挑选各种Unicode符号,解锁新形状,应有尽有,再也不怕找不到合适的符号了!
unicode = list(triangle=sprintf('\u25B2'),
circle=sprintf('\u25CF'),
square=sprintf('\u25A0'),
arrow=sprintf('\u2794'))
改变符号:
p2 <- p1 +
scale_shape_manual(values=c(rep(unicode[["triangle"]], 2),
unicode[["circle"]], unicode[["square"]], unicode[["arrow"]])) +
scale_colour_manual(values=c(RColorBrewer::brewer.pal(3, "Set1")[1:2],
rep("black", 3))) +
scale_x_continuous(limits = c(-1,32), breaks = -1:30,expand = c(0,0.6))+
labs(fill = "Disease Stage", shape = NULL, color = NULL,y="Patient")+
theme_bw()+
theme(panel.grid.minor=element_blank(),
panel.grid.major=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank()
)
p2
plot of chunk unnamed-chunk-11
搞定,但是还有问题,箭头不在正中间,大家有解决办法吗?
参考资料
[1]
unicode: https://rpubs.com/alexiswl/swimmer
相关文章
- 【说站】python如何对单个值测试多个变量?
- origin如何在柱状图上面显示数据_origin柱状图横坐标自定义
- SQL中如何将一列中的值显示出字符指定位置与指定长度。
- 【Linux编程】如何将gettimeofday获取的时间信息转换成可显示的字符串时间?
- 三体中歌者如何发现地球的位置的?
- 如何使用Ox4Shell反混淆Log4Shell的Payload
- 数据MySQL:如何快速删除多表数据(mysql删除多表)
- Oracle 视图 ALL_APPLY_DML_HANDLERS 官方解释,作用,如何使用详细说明
- Oracle 视图 HS_CLASS_DD 官方解释,作用,如何使用详细说明
- 如何更高效的删除MySQL中的索引(mysql索引删除)
- Linux网络连接灾难:如何解决?(linux网络连接失败)
- 远程桌面连接如何挂载显示本地磁盘
- 如何使用Linux复合命令简化工作(linux复合命令)
- 如何在Oracle中修改用户密码(oracle修改用户密码)
- 如何在MySQL中存储和显示Emoji表情符号?(mysqlemoji)
- 一步步教你如何安装Linux下的lftp(linux lftp安装)
- MySQL技术:如何正确显示时间(mysql 显示时间)
- 如何将Redis变为一项服务(怎么把redis做成服务)
- 如何设置自动启动Redis数据库(怎么自动启动redis库)
- Oracle如何修改中文显示格式(oracle修改中文格式)
- MySQL如何使用显示命令查看表结构(mysql下显示命令)
- MySQL服务未显示,如何解决(mysql不显示服务)
- 如何解决MySQL不显示列名的问题(mysql 不显示列名)
- Oracle中如何查看游标信息(oracle中显示游标)
- Redis集群如何关闭重启(redis集群关闭重启)
- 在Oracle中新建表的方法(oracle下如何新建表)