r - 如何使用不同颜色的颜色、形状和图例标签
问题描述
这是我的数据框:
> dput(x)
structure(list(cell_type = c("Macrophage_M2", "Monocyte", "Mast_cell",
"T_cell_CD4+_effector_memory", "T_cell_CD4+_memory", "T_cell_CD8+_naive",
"B_cell_plasma", "T_cell_CD4+_(non-regulatory)", "T_cell_CD4+_Th2",
"Endothelial_cell", "T_cell_NK", "T_cell_regulatory_(Tregs)",
"Cancer_associated_fibroblast", "T_cell_CD4+_central_memory",
"Hematopoietic_stem_cell", "T_cell_regulatory_(Tregs)", "Hematopoietic_stem_cell",
"Common_myeloid_progenitor", "Endothelial_cell", "Macrophage_M2"
), cluster = c("1", "1", "1", "1", "1", "2", "2", "2", "2", "2",
"3", "3", "3", "3", "3", "4", "4", "4", "4", "4"), vt_score = c(0.620725205988837,
0.565049819839983, 0.555015252291001, 0.529856563905404, 0.467454576797864,
0.434671831438467, 0.367378221155799, 0.357576774722313, 0.305798026454146,
0.222533326094146, 0.457134190027398, 0.447470659870985, 0.447033729409117,
0.374612139728217, 0.347672664499527, 0.319135092044291, 0.172082683316721,
0.116551044539405, 0.0763883445051204, 0.0268403599993589), id_col = c(1L,
2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L, 3L, 4L, 5L, 1L, 2L,
3L, 4L, 5L), colors = c("#0D0887FF", "#7E03A8FF", "#CC4678FF",
"#F89441FF", "#F0F921FF", "#0D0887FF", "#7E03A8FF", "#CC4678FF",
"#F89441FF", "#F0F921FF", "#0D0887FF", "#7E03A8FF", "#CC4678FF",
"#F89441FF", "#F0F921FF", "#0D0887FF", "#7E03A8FF", "#CC4678FF",
"#F89441FF", "#F0F921FF"), shapes = c(16, 16, 16, 16, 16, 17,
17, 17, 17, 17, 18, 18, 18, 18, 18, 19, 19, 19, 19, 19)), class = "data.frame", row.names = c(NA,
-20L))
为了清楚起见,前 10 行:
> head(x, n =10)
cell_type cluster vt_score id_col colors shapes
1 Macrophage_M2 1 0.6207252 1 #0D0887FF 16
2 Monocyte 1 0.5650498 2 #7E03A8FF 16
3 Mast_cell 1 0.5550153 3 #CC4678FF 16
4 T_cell_CD4+_effector_memory 1 0.5298566 4 #F89441FF 16
5 T_cell_CD4+_memory 1 0.4674546 5 #F0F921FF 16
6 T_cell_CD8+_naive 2 0.4346718 1 #0D0887FF 17
7 B_cell_plasma 2 0.3673782 2 #7E03A8FF 17
8 T_cell_CD4+_(non-regulatory) 2 0.3575768 3 #CC4678FF 17
9 T_cell_CD4+_Th2 2 0.3057980 4 #F89441FF 17
10 Endothelial_cell 2 0.2225333 5 #F0F921FF 17
所以这里有 4 个簇,每个簇都有一个形状(即形状列)。对于每个集群,有 5 个cell_types
值和一个与每个 cell_type 值相关联的颜色(即颜色列)。我想使用colors
和shapes
列着色和形状,但想cell_type
在图例中将实际显示为标签。我尝试了以下方法,但似乎不起作用:
ggplot(data = x, aes(x = cluster, y = vt_score,
color = cell_type, shape = cell_type)) +
geom_point(size = 5, alpha = 0.8) +
scale_color_manual(labels = x$cell_type,
values = x$colors) +
scale_shape_manual(labels = x$cell_type,
values = x$shapes)
我得到的是这是不正确的(颜色、形状和 cell_types 似乎不匹配)
更新:
我根据以下@aosmith 的建议使用了限制,它起作用了:
# add cell_type and cluster combination for duplicates
x$cell_type_lab <- paste0(x$cell_type,'_',x$cluster)
# use limits, labels (for legend) and values (colors/shapes)
ggplot(data = x, aes(x = cluster, y = vt_score,
color = cell_type_lab, shape = cell_type_lab)) +
theme_bw() + theme_Publication2() +
geom_point(size = 10, alpha = 0.7, position=position_dodge(width=0.05)) +
scale_color_manual(limits = x$cell_type_lab,
labels = x$cell_type,
values = x$colors) +
scale_shape_manual(limits = x$cell_type_lab,
labels = x$cell_type,
values = x$shapes)
解决方案
推荐阅读
- json - 无法使用嵌入对象发布 JSON - 它读取并存储除对象列表 (OneToMany) 之外的所有内容 - Spring Boot API (+ hibernate)
- reactjs - WordPress 作为带有 Next.js 的无头 CMS - 使用代码突出显示片段呈现帖子内容的问题
- c++ - 如何为 clang 的 C++20 功能正确设置 ccls?
- flutter - 如何去除脚手架中的晶圆厂底部填充物
- nginx - 动态位置块变量到字符串
- python - 如何使用python从动态网页中收集表格数据
- spring-boot - 如何在 Spring Boot 项目的某些模块上配置自定义 Pitest Gradle 任务?
- ios - 为什么 ViewController 后退按钮在 Xcode 上不起作用?
- python - 在python中获取下一个12个月的最大值
- bash - AWS localstack 和 dotnetcore3.1 lambda 函数 - 无法创建函数