gpt4 book ai didi

r - 如何为由三个子组组成的两个独立组添加图例标题?

转载 作者:行者123 更新时间:2023-12-04 15:08:03 24 4
gpt4 key购买 nike

我有
enter image description here
蓝色表示一组,而红色表示另一组。如您所见,每组中有三个 WHO==1,2,3 等级。
我想简化图形。是否可以为“蓝色字符串”和“红色字符串”添加标题?
预期输出:
enter image description here
当前脚本

ggplot(b, 
aes(x=woTERT, y=wTERT, shape=tert_group, col=tert_group, fill=tert_group)) +
geom_abline(intercept=0, slope=1) +
geom_point(size = 6) +
scale_color_manual(values = c("#2C77BF","#E38072","#2C77BF","#E38072","#2C77BF","#E38072"),
name = "",
labels = c("WHO-I, TERTp-wt", "WHO-I, TERT-alt", "WHO-II, TERTp-wt", "WHO-II, TERT-alt", "WHO-III, TERTp-wt", "WHO-III, TERTp-alt")) +
scale_fill_manual(values = alpha(c("#2C77BF","#E38072","#2C77BF","#E38072","#2C77BF","#E38072"),.5),
name = "",
labels = c("WHO-I, TERTp-wt", "WHO-I, TERT-alt", "WHO-II, TERTp-wt", "WHO-II, TERT-alt", "WHO-III, TERTp-wt", "WHO-III, TERTp-alt")) +
scale_shape_manual(name="",
values = c(21,21,22,22,24,24),
labels = c("WHO-I, TERTp-wt", "WHO-I, TERT-alt", "WHO-II, TERTp-wt", "WHO-II, TERT-alt", "WHO-III, TERTp-wt", "WHO-III, TERTp-alt")) +
scale_x_continuous(name = "WHO w/o TERT: Predticed risk of recurrence",
labels = paste0(seq(0,100,10),"%"),
breaks = seq(0,1,.1),
limits = c(0,1)) +
scale_y_continuous(name = "WHO +TERT: Predticed risk of recurrence",
labels = paste0(seq(0,100,10),"%"),
breaks = seq(0,1,.1))
我尝试了 name = "TERTp-wt\nTERT-alt" 的不同方法,但这与图例文本不太吻合。
数据
b <- structure(list(wTERT = c(0.270595572606829, 0.520964343227331, 
0.272033993387013, 0.857817018869424, 0.277232269239759, 0.515411219723414,
0.530776201033681, 0.537978816391861, 0.580089397943256, 0.272329846882174,
0.281816598689814, 0.267523804265192, 0.586575955801609, 0.272561919226923,
0.551067731573839, 0.575040961457171, 0.853836648872606, 0.513865922413166,
0.275071745463873, 0.571893335762296, 0.555961759098334, 0.507670937474723,
0.273263149407663, 0.520179490081975, 0.275298952565891, 0.521199056010643,
0.275831552542037, 0.51268122313319, 0.26669990754491, 0.271720905718063,
0.271087579774399, 0.571600915004122, 0.272378890421031, 0.263465014921917,
0.518472001274984, 0.269721594792881, 0.520351505941105, 0.504346622358662,
0.27136029515132, 0.520046182006088, 0.855693574828987, 0.276472448986918,
0.539999936464913, 0.520401377593671, 0.507614268626494, 0.275450880251895,
0.577062832181107, 0.512247929637022, 0.240803122800178, 0.50763956995495
), woTERT = c(0.296489465444783, 0.596498864892734, 0.297108907238502,
0.602699790380155, 0.305069585544761, 0.587576335150488, 0.604521947458411,
0.608453027993258, 0.299884566783421, 0.299902221096003, 0.308462348235621,
0.294054710148313, 0.297251315754283, 0.298576329342399, 0.300810601918682,
0.298851968053745, 0.585048328214746, 0.589265392514601, 0.300302546149141,
0.297328944265885, 0.295424393598363, 0.579698692008963, 0.298375131334038,
0.590188017898546, 0.30140746476472, 0.59288008651446, 0.301020050566609,
0.583491137431191, 0.293372042721268, 0.295396218451921, 0.29655640187132,
0.298261453439166, 0.298801473982103, 0.290540925606159, 0.588873457535114,
0.295734684803261, 0.589402145362498, 0.575463683620961, 0.296105130790763,
0.590311321653519, 0.588488981258673, 0.302496354158585, 0.287223763371511,
0.59337243096153, 0.581761288026854, 0.300224806265764, 0.293940887350586,
0.585698286452565, 0.272261078946515, 0.58056413840941), tert_group = c("WHO-II0",
"WHO-III0", "WHO-II0", "WHO-III1", "WHO-II0", "WHO-III0", "WHO-III0",
"WHO-III0", "WHO-II1", "WHO-II0", "WHO-II0", "WHO-II0", "WHO-II1",
"WHO-II0", "WHO-II1", "WHO-II1", "WHO-III1", "WHO-III0", "WHO-II0",
"WHO-II1", "WHO-II1", "WHO-III0", "WHO-II0", "WHO-III0", "WHO-II0",
"WHO-II0", "WHO-III0", "WHO-III0", "WHO-II0", "WHO-II0", "WHO-II0",
"WHO-II1", "WHO-II0", "WHO-II0", "WHO-III0", "WHO-II0", "WHO-III0",
"WHO-II0", "WHO-III0", "WHO-III0", "WHO-III1", "WHO-II0", "WHO-I1",
"WHO-III0", "WHO-III0", "WHO-II0", "WHO-II1", "WHO-III0", "WHO-I0",
"WHO-III0")), row.names = c(NA, -50L), class = c("tbl_df", "tbl",
"data.frame"))

最佳答案

这可以帮助:

library(ggplot2)
#Code
ggplot(b,
aes(x=woTERT, y=wTERT, shape=tert_group, col=tert_group, fill=tert_group)) +
geom_abline(intercept=0, slope=1) +
geom_point(size = 6) +
scale_color_manual(values = c("#2C77BF","#E38072","#2C77BF","#E38072","#2C77BF","#E38072"),
name = "TERTp-wt\n\nTERT-alt",
labels = c("WHO-I, TERTp-wt", "WHO-I, TERT-alt", "WHO-II, TERTp-wt", "WHO-II, TERT-alt", "WHO-III, TERTp-wt", "WHO-III, TERTp-alt")) +
scale_fill_manual(values = alpha(c("#2C77BF","#E38072","#2C77BF","#E38072","#2C77BF","#E38072"),.5),
name = "TERTp-wt\n\nTERT-alt",
labels = c("WHO-I, TERTp-wt", "WHO-I, TERT-alt", "WHO-II, TERTp-wt", "WHO-II, TERT-alt", "WHO-III, TERTp-wt", "WHO-III, TERTp-alt")) +
scale_shape_manual(name="TERTp-wt\n\nTERT-alt",
values = c(21,21,22,22,24,24),
labels = c("WHO-I, TERTp-wt", "WHO-I, TERT-alt", "WHO-II, TERTp-wt", "WHO-II, TERT-alt", "WHO-III, TERTp-wt", "WHO-III, TERTp-alt")) +
scale_x_continuous(name = "WHO w/o TERT: Predticed risk of recurrence",
labels = paste0(seq(0,100,10),"%"),
breaks = seq(0,1,.1),
limits = c(0,1)) +
scale_y_continuous(name = "WHO +TERT: Predticed risk of recurrence",
labels = paste0(seq(0,100,10),"%"),
breaks = seq(0,1,.1))+
theme(legend.position = 'top')+
theme(legend.title = element_text(face='bold',size=9))
输出:
enter image description here

关于r - 如何为由三个子组组成的两个独立组添加图例标题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65723211/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com