如何对森林图中每个子组的级别进行分组
how to group levels of each subgroup in a forest plot
> dput(fig2b_data)
structure(list(subgroup = c("sex", "sex", "ai_comorbid_bool",
"ai_comorbid_bool", "non_ai_comorbid_bool", "non_ai_comorbid_bool",
"age_70_plus", "age_70_plus", "ecog_combined", "ecog_combined",
"indication_combined", "indication_combined", "site", "site",
"site", "site", "site", "site", "site", "site"), level = c("Female",
"Male", "No", "Yes", "No", "Yes", "No", "Yes", "0", "1+", "Adjuvant",
"Metastatic / Unresectable", "Cambridge", "Belfast", "Cardiff",
"Liverpool", "Norwich", "Preston", "Southampton", "Taunton"),
subgroup_level = c("sex_Female", "sex_Male", "ai_comorbid_bool_No",
"ai_comorbid_bool_Yes", "non_ai_comorbid_bool_No", "non_ai_comorbid_bool_Yes",
"age_70_plus_No", "age_70_plus_Yes", "ecog_combined_0", "ecog_combined_1+",
"indication_combined_Adjuvant", "indication_combined_Metastatic / Unresectable",
"site_Cambridge", "site_Belfast", "site_Cardiff", "site_Liverpool",
"site_Norwich", "site_Preston", "site_Southampton", "site_Taunton"
), ref = c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE,
TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE), adj_or = c(1, 1.92697788983048, 1,
0.309313271153888, 1, 1.60176654927755, 1, 0.581067651194834,
1, 0.606677244239784, 1, 0.757510322046024, 1, 0.0671548910659019,
1.24115412701041, 0.111740502056371, 0.296334401152569, 0.407313416513578,
0.100703132319318, 0.0580853387590806), ci_low = c(NA, 1.08574689964253,
NA, 0.0933004210866726, NA, 0.813446935851162, NA, 0.300096568750007,
NA, 0.301300997438692, NA, 0.395638695943013, NA, 0.0184879397812241,
0.316512222510664, 0.0310182213975059, 0.0774035454553755,
0.0834303368267395, 0.0228743220824828, 0.011193138928203
), ci_high = c(NA, 3.4667621174982, NA, 0.929482385449043,
NA, 3.1938659749789, NA, 1.11325241104074, NA, 1.21374279615277,
NA, 1.44670881667103, NA, 0.205952672316014, 4.59055508109202,
0.342443550375257, 1.00710088916867, 2.04034216674928, 0.387728614421501,
0.257636420370032), p = c(NA, 0.0263295963311719, NA, 0.0432646112707497,
NA, 0.175314541854903, NA, 0.103298047943536, NA, 0.158264479732785,
NA, 0.399589361570504, NA, 8.78601713425597e-06, 0.747238599523183,
0.000291277241946869, 0.0597081504970594, 0.260985385401162,
0.00132018341690714, 0.000328378914869459), sig = c(NA, TRUE,
NA, TRUE, NA, FALSE, NA, FALSE, NA, FALSE, NA, FALSE, NA,
TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE), col = c("REF",
"UP", "REF", "DOWN", "REF", "INSIG", "REF", "INSIG", "REF",
"INSIG", "REF", "INSIG", "REF", "DOWN", "INSIG", "DOWN",
"INSIG", "INSIG", "DOWN", "DOWN")), row.names = c(NA, -20L
), class = "data.frame")
我想画一个森林图,但每个级别都按子组分组。我该怎么做?
到目前为止我已经尝试过这个来获得我的情节,但是在对级别进行分组时遇到困难:
................................................ ..................................................... ..................................................... ..................................................... .....
# plot
ggplot(data = fig2b_data, aes(x = adj_or, y = subgroup_level)) +
geom_errorbarh(aes(xmax = ci_high, xmin = ci_low, color = col), size = .5, height = .2) +
geom_point(aes(color = col), size = 2) +
theme_bw()
我愿意:
_________________________
Sex
Male (ref) x
Female |----x-----|
_______________________________________________________________
AI comorbid
No (ref) x
Yes |----x-----|
_______________________________________________________________
Site
Cambridge(ref) x
Preston |----x-----|
Southampton |----x-----|
Belfast |----x-----|
__________________________________1____________________________
这是一个很好的解决方法。 facet_wrap()
高度的调整高度是从
借来的
代码
library(dplyr)
library(ggplot2)
fig2b_data_cleared <- fig2b_data %>%
mutate(subgroup = fct_recode(subgroup, "Age >= 70" = "age_70_plus",
"AI Comorbidities" = "ai_comorbid_bool",
"ECOG" = "ecog_combined",
"Indication" = "indication_combined",
"Non-AI Comorbidities" = "non_ai_comorbid_bool",
"Sex" = "sex",
"Site" = "site"),
subgroup_level = fct_relevel(subgroup_level, "age_70_plus_Yes", "age_70_plus_No",
"ai_comorbid_bool_Yes", "ai_comorbid_bool_No",
"ecog_combined_1+", "ecog_combined_0",
"indication_combined_Metastatic / Unresectable", "indication_combined_Adjuvant",
"non_ai_comorbid_bool_Yes", "non_ai_comorbid_bool_No",
"sex_Male", "sex_Female",
"site_Belfast",
"site_Cardiff", "site_Liverpool",
"site_Norwich", "site_Preston",
"site_Southampton", "site_Taunton",
"site_Cambridge"))
p <- ggplot(data = fig2b_data_cleared, aes(x = adj_or, y = subgroup_level)) +
geom_vline(xintercept = 1, linetype = 2, color = "red") +
geom_point(aes(color = col), size = 3) +
xlab("Adjusted Odds Ratio") +
ylab("") +
geom_errorbar(aes(xmax = ci_high, xmin = ci_low, color = col), size = 0.8, width = 0.5) +
theme(plot.title.x = element_text(size = 16, face = "bold"),
axis.text.y = element_blank(),
axis.text.x = element_text(face = "bold"),
axis.title.y = element_blank(),
strip.text.y = element_text(hjust = 0, vjust = 1, angle = 180, face = "bold"),
legend.title = element_blank()) +
theme_bw() +
scale_y_discrete(breaks=c("age_70_plus_No", "age_70_plus_Yes",
"ai_comorbid_bool_No", "ai_comorbid_bool_Yes",
"ecog_combined_0", "ecog_combined_1+",
"indication_combined_Adjuvant", "indication_combined_Metastatic / Unresectable",
"non_ai_comorbid_bool_No", "non_ai_comorbid_bool_Yes",
"sex_Female", "sex_Male",
"site_Cambridge", "site_Belfast",
"site_Cardiff", "site_Liverpool",
"site_Norwich", "site_Preston",
"site_Southampton", "site_Taunton"),
labels=c("No (Ref)", "Yes",
"No (Ref)", "Yes",
"No (Ref)", "Yes",
"No (Ref)", "Yes",
"No (Ref)", "Yes",
"Female (Ref)", "Male",
"Cambridge (Ref)", "Belfast",
"Cardiff", "Liverpool",
"Norwich", "Preston",
"Southampton", "Tanton")) +
scale_color_discrete(limits = c("REF", "INSIG", "DOWN", "UP"),
name = "")
p.grid <- p + facet_grid(subgroup ~ ., scales = "free_y", space = "free_y")
p.wrap <- p + facet_wrap(~ subgroup, ncol = 1, scales = "free_y")
gp.grid <- ggplotGrob(p.grid)
gp.wrap <- ggplotGrob(p.wrap)
gp.wrap$heights[gp.wrap$layout[grep("panel", gp.wrap$layout$name), "t"]] <-
gp.grid$heights[gp.grid$layout[grep("panel", gp.grid$layout$name), "t"]]
grid::grid.draw(gp.wrap)
输出
> dput(fig2b_data)
structure(list(subgroup = c("sex", "sex", "ai_comorbid_bool",
"ai_comorbid_bool", "non_ai_comorbid_bool", "non_ai_comorbid_bool",
"age_70_plus", "age_70_plus", "ecog_combined", "ecog_combined",
"indication_combined", "indication_combined", "site", "site",
"site", "site", "site", "site", "site", "site"), level = c("Female",
"Male", "No", "Yes", "No", "Yes", "No", "Yes", "0", "1+", "Adjuvant",
"Metastatic / Unresectable", "Cambridge", "Belfast", "Cardiff",
"Liverpool", "Norwich", "Preston", "Southampton", "Taunton"),
subgroup_level = c("sex_Female", "sex_Male", "ai_comorbid_bool_No",
"ai_comorbid_bool_Yes", "non_ai_comorbid_bool_No", "non_ai_comorbid_bool_Yes",
"age_70_plus_No", "age_70_plus_Yes", "ecog_combined_0", "ecog_combined_1+",
"indication_combined_Adjuvant", "indication_combined_Metastatic / Unresectable",
"site_Cambridge", "site_Belfast", "site_Cardiff", "site_Liverpool",
"site_Norwich", "site_Preston", "site_Southampton", "site_Taunton"
), ref = c(TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, TRUE, FALSE,
TRUE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE,
FALSE, FALSE, FALSE), adj_or = c(1, 1.92697788983048, 1,
0.309313271153888, 1, 1.60176654927755, 1, 0.581067651194834,
1, 0.606677244239784, 1, 0.757510322046024, 1, 0.0671548910659019,
1.24115412701041, 0.111740502056371, 0.296334401152569, 0.407313416513578,
0.100703132319318, 0.0580853387590806), ci_low = c(NA, 1.08574689964253,
NA, 0.0933004210866726, NA, 0.813446935851162, NA, 0.300096568750007,
NA, 0.301300997438692, NA, 0.395638695943013, NA, 0.0184879397812241,
0.316512222510664, 0.0310182213975059, 0.0774035454553755,
0.0834303368267395, 0.0228743220824828, 0.011193138928203
), ci_high = c(NA, 3.4667621174982, NA, 0.929482385449043,
NA, 3.1938659749789, NA, 1.11325241104074, NA, 1.21374279615277,
NA, 1.44670881667103, NA, 0.205952672316014, 4.59055508109202,
0.342443550375257, 1.00710088916867, 2.04034216674928, 0.387728614421501,
0.257636420370032), p = c(NA, 0.0263295963311719, NA, 0.0432646112707497,
NA, 0.175314541854903, NA, 0.103298047943536, NA, 0.158264479732785,
NA, 0.399589361570504, NA, 8.78601713425597e-06, 0.747238599523183,
0.000291277241946869, 0.0597081504970594, 0.260985385401162,
0.00132018341690714, 0.000328378914869459), sig = c(NA, TRUE,
NA, TRUE, NA, FALSE, NA, FALSE, NA, FALSE, NA, FALSE, NA,
TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, TRUE), col = c("REF",
"UP", "REF", "DOWN", "REF", "INSIG", "REF", "INSIG", "REF",
"INSIG", "REF", "INSIG", "REF", "DOWN", "INSIG", "DOWN",
"INSIG", "INSIG", "DOWN", "DOWN")), row.names = c(NA, -20L
), class = "data.frame")
我想画一个森林图,但每个级别都按子组分组。我该怎么做?
到目前为止我已经尝试过这个来获得我的情节,但是在对级别进行分组时遇到困难:
................................................ ..................................................... ..................................................... ..................................................... .....
# plot
ggplot(data = fig2b_data, aes(x = adj_or, y = subgroup_level)) +
geom_errorbarh(aes(xmax = ci_high, xmin = ci_low, color = col), size = .5, height = .2) +
geom_point(aes(color = col), size = 2) +
theme_bw()
我愿意:
_________________________
Sex
Male (ref) x
Female |----x-----|
_______________________________________________________________
AI comorbid
No (ref) x
Yes |----x-----|
_______________________________________________________________
Site
Cambridge(ref) x
Preston |----x-----|
Southampton |----x-----|
Belfast |----x-----|
__________________________________1____________________________
这是一个很好的解决方法。 facet_wrap()
高度的调整高度是从
代码
library(dplyr)
library(ggplot2)
fig2b_data_cleared <- fig2b_data %>%
mutate(subgroup = fct_recode(subgroup, "Age >= 70" = "age_70_plus",
"AI Comorbidities" = "ai_comorbid_bool",
"ECOG" = "ecog_combined",
"Indication" = "indication_combined",
"Non-AI Comorbidities" = "non_ai_comorbid_bool",
"Sex" = "sex",
"Site" = "site"),
subgroup_level = fct_relevel(subgroup_level, "age_70_plus_Yes", "age_70_plus_No",
"ai_comorbid_bool_Yes", "ai_comorbid_bool_No",
"ecog_combined_1+", "ecog_combined_0",
"indication_combined_Metastatic / Unresectable", "indication_combined_Adjuvant",
"non_ai_comorbid_bool_Yes", "non_ai_comorbid_bool_No",
"sex_Male", "sex_Female",
"site_Belfast",
"site_Cardiff", "site_Liverpool",
"site_Norwich", "site_Preston",
"site_Southampton", "site_Taunton",
"site_Cambridge"))
p <- ggplot(data = fig2b_data_cleared, aes(x = adj_or, y = subgroup_level)) +
geom_vline(xintercept = 1, linetype = 2, color = "red") +
geom_point(aes(color = col), size = 3) +
xlab("Adjusted Odds Ratio") +
ylab("") +
geom_errorbar(aes(xmax = ci_high, xmin = ci_low, color = col), size = 0.8, width = 0.5) +
theme(plot.title.x = element_text(size = 16, face = "bold"),
axis.text.y = element_blank(),
axis.text.x = element_text(face = "bold"),
axis.title.y = element_blank(),
strip.text.y = element_text(hjust = 0, vjust = 1, angle = 180, face = "bold"),
legend.title = element_blank()) +
theme_bw() +
scale_y_discrete(breaks=c("age_70_plus_No", "age_70_plus_Yes",
"ai_comorbid_bool_No", "ai_comorbid_bool_Yes",
"ecog_combined_0", "ecog_combined_1+",
"indication_combined_Adjuvant", "indication_combined_Metastatic / Unresectable",
"non_ai_comorbid_bool_No", "non_ai_comorbid_bool_Yes",
"sex_Female", "sex_Male",
"site_Cambridge", "site_Belfast",
"site_Cardiff", "site_Liverpool",
"site_Norwich", "site_Preston",
"site_Southampton", "site_Taunton"),
labels=c("No (Ref)", "Yes",
"No (Ref)", "Yes",
"No (Ref)", "Yes",
"No (Ref)", "Yes",
"No (Ref)", "Yes",
"Female (Ref)", "Male",
"Cambridge (Ref)", "Belfast",
"Cardiff", "Liverpool",
"Norwich", "Preston",
"Southampton", "Tanton")) +
scale_color_discrete(limits = c("REF", "INSIG", "DOWN", "UP"),
name = "")
p.grid <- p + facet_grid(subgroup ~ ., scales = "free_y", space = "free_y")
p.wrap <- p + facet_wrap(~ subgroup, ncol = 1, scales = "free_y")
gp.grid <- ggplotGrob(p.grid)
gp.wrap <- ggplotGrob(p.wrap)
gp.wrap$heights[gp.wrap$layout[grep("panel", gp.wrap$layout$name), "t"]] <-
gp.grid$heights[gp.grid$layout[grep("panel", gp.grid$layout$name), "t"]]
grid::grid.draw(gp.wrap)
输出