我从https://bookdown.org/MathiasHarrer/Doing_Meta_Analysis_in_R/plotting-the-summary.html借用(并略微简化)了ggplot2绘图函数的代码
给定以下数据,函数plot_rob()将生成所示的图形。
我想按以下顺序显示栏(从上到下):
“随机序列生成”、“分配隐藏”、“参与者致盲”、“人员致盲”、“结果评估员致盲”、“结果数据不完整”、“意向治疗分析”、“基线群体相似性”、“共同干预”、“遵从性”、“结果评估的时间安排”。
data <- structure(list(name = c("Random sequence generation", "Allocation concealment",
"Blinding of participants", "Blinding of personnel", "Blinding of outcome assessor",
"Incomplete outcome data", "Intention to treat analysis", "Group similarity at baseline",
"Co-interventions", "Compliance", "Timing of outcome assessments",
"Random sequence generation", "Allocation concealment", "Blinding of participants",
"Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data",
"Intention to treat analysis", "Group similarity at baseline",
"Co-interventions", "Compliance", "Timing of outcome assessments",
"Random sequence generation", "Allocation concealment", "Blinding of participants",
"Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data",
"Intention to treat analysis", "Group similarity at baseline",
"Co-interventions", "Compliance", "Timing of outcome assessments"
), RoB = c("U", "H", "H", "H", "H", "H", "H", "L", "L", "L",
"L", "U", "L", "H", "H", "H", "L", "L", "H", "L", "L", "L", "L",
"L", "H", "H", "H", "H", "H", "L", "L", "L", "L")), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -33L))下面的函数plot_rob():
plot_rob <- function(rob.long) {
rob.long$RoB<-as.factor(rob.long$RoB)
rob.long$RoB<-factor(rob.long$RoB,levels(rob.long$RoB)[c(1,3,2)])
rob.plot <-ggplot(data=rob.long)+
geom_bar(mapping=aes(x=name,fill=RoB),
width=0.7,
position = "fill",
color="black")+
coord_flip(ylim = c(0,1))+
guides(fill = guide_legend(reverse = TRUE))+
scale_fill_manual("Risk of Bias",
labels = c(" High risk of bias ",
" Unclear risk of bias ",
" Low risk of bias "),
values = c("U" = "#E2DF07",
"H" = "#BF0000",
"L" = "#02C100"))+
scale_y_continuous(labels = scales::percent)+
theme(axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y = element_text(size=18, color = "black"),
axis.line.x = element_line(colour = "black",
size = 0.5, linetype = "solid"),
legend.position = "bottom",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.background = element_rect(linetype="solid",
colour ="black"),
legend.title = element_blank(),
legend.key.size = unit(0.75,"cm"),
legend.text=element_text(size=12))
return(rob.plot)
}

发布于 2019-03-22 21:24:57
正如@DS_UNI已经提到的,您必须将name列转换为一个因子,然后指定顺序。在这种情况下,您需要它们出现的(相反)顺序,以便您可以使用以下行:
library(tidyverse)
data %>%
mutate(name = fct_inorder(name) %>% fct_rev()) %>%
plot_rob()

发布于 2019-03-22 21:38:29
order <- c("Random sequence generation", "Allocation concealment", "Blinding of participants",
"Blinding of personnel", "Blinding of outcome assessor", "Incomplete outcome data",
"Intention to treat analysis", "Group similarity at baseline",
"Co-interventions", "Compliance", "Timing of outcome assessments"
)我按照@DS_UNI所暗示的那样修改了函数,以生成任意顺序的水平条形图。
plot_rob <- function(rob.long, order) {
rob.long$RoB<-as.factor(rob.long$RoB)
rob.long$RoB<-factor(rob.long$RoB,levels(rob.long$RoB)[c(1,3,2)])
rob.long$name<-factor(rob.long$name, levels=rev(order))
rob.plot <-ggplot(data=rob.long)+
geom_bar(mapping=aes(x=name,fill=RoB),
width=0.7,
position = "fill",
color="black")+
coord_flip(ylim = c(0,1))+
guides(fill = guide_legend(reverse = TRUE))+
scale_fill_manual("Risk of Bias",
labels = c(" High risk of bias ",
" Unclear risk of bias ",
" Low risk of bias "),
values = c("U" = "#E2DF07",
"H" = "#BF0000",
"L" = "#02C100"))+
scale_y_continuous(labels = scales::percent)+
theme(axis.title.x=element_blank(),
axis.title.y=element_blank(),
axis.ticks.y=element_blank(),
axis.text.y = element_text(size=18, color = "black"),
axis.line.x = element_line(colour = "black",
size = 0.5, linetype = "solid"),
legend.position = "bottom",
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
legend.background = element_rect(linetype="solid",
colour ="black"),
legend.title = element_blank(),
legend.key.size = unit(0.75,"cm"),
legend.text=element_text(size=12))
return(rob.plot)
}https://stackoverflow.com/questions/55300120
复制相似问题