[英]combining multiple boxplots to one figure using ggplot r
使用以下代码,我创建了 4 个箱线图:
bxp1<-ggplot(setTxy,aes(x=VolumCat2, y =perc_DVHT_99, fill=displ),position=position_dodge(width=5)) +
geom_boxplot() +
scale_x_discrete(labels = My.labels)+
scale_fill_manual(values=c("#999999","#E69F00","#56B4E9"))+
labs(title="Translation x-,y-direction",y="Ratio",ymin=0.5, ymax=1.2)
bxp3<-ggplot(setTz,aes(x=VolumCat2, y =perc_DVHT_99, fill=displ),position=position_dodge(width=5)) +
geom_boxplot() +
scale_x_discrete(labels = My.labels)+
scale_fill_manual(values=c("#999999","#E69F00","#56B4E9"))+
labs(title="Translation z-direction", x="Volume category",y="Ratio",ymin=0.5, ymax=1.2)
bxp2<- ggplot(setTxy,aes(x=VolumCat2, y =perc_DVHR_99, fill=displ),position=position_dodge(width=5)) +
geom_boxplot() +
scale_x_discrete(labels = My.labels)+
scale_fill_manual(values=c("#999999","#E69F00","#56B4E9"),name="Displacement")+
labs(title="Rotation x-,y-direction", y="Ratio",ymin=0.5, ymax=1.2)
bxp4<- ggplot(setTz,aes(x=VolumCat2, y =perc_DVHR_99, fill=displ),position=position_dodge(width=5)) +
geom_boxplot() +
scale_x_discrete(labels = My.labels)+
scale_fill_manual(values=c("#999999","#E69F00","#56B4E9"),name="Displacement")+
labs(title="Rotation z-direction", x="Volume category",y="Ratio",ymin=0.5, ymax=1.2)
figure<- ggarrange(bxp1,bxp2,bxp3,bxp4,
labels=c("a","b","c","d"),
ncol=2,nrow=2)
annotate_figure(figure,
top=text_grob("Ratio D99",color="black",face="bold",size=12),
fig.lab="Figure 1" )
但是我怎样才能进行以下更改:1)对于前两个箱线图,我想松开 x 标签 2)对于所有 plot,我希望有相同的 y 轴间距 3)对于左边的两个箱线图,我会喜欢放松对名为 displ 的“盒子”的解释如果可能的话,我想为所有图放开这个,并在 4 个箱形图的顶部添加灰色代表 0.5 毫米,橙色代表 1.0 毫米,蓝色代表 1.5 毫米位移
欢迎提出其他使该图看起来更好的建议
My.labels<-c("0-1cc\nn=39","1-5cc\nn=25","5-10cc\nn= 15","10-15cc\nn=6","15-20cc\nn=4",">20cc\nn=42")
dput()
给出: setTxy 是一个长结构,如下所示:
structure(list(displ = c("0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm",
"0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm", "0,5 mm"),
perc_DVHT_99 = c(0.99169574073565, 0.983998642978761,
0.993452539098338, 0.983301531618343, 0.978633859305831,
0.97572227542085, 0.993287258697977, 0.993033293087417, 0.95287598273786,
0.970386976259169),
perc_DVHR_99 = c(0.998593284244034, 0.999335925776935,
0.996706003069954, 1.00115102497025, 0.96614985358813, 1.01255533813509,
1.00579263638508, 0.989133602564636, 0.955562258557279, 0.973325763729247),
perc_covT = c(0.996966632962588, 0.993933265925177, 0.997977755308392,
0.993933265925177, 0.99388379204893, 0.988786952089704, 0.99592252803262,
0.99592252803262, 0.992957746478873, 0.994969818913481),
perc_covR = c(0.998988877654196, 0.998988877654196, 0.998988877654196,
1, 0.986748216106014, 1.00509683995923, 1.00203873598369,
0.994903160040775, 0.992957746478873, 0.995975855130785),
VolumCat2 = c("e", "e", "e", "e", "b1", "b1", "b1", "b1",
"b1", "b1")), row.names = c(NA, -10L), class = c("tbl_df",
"tbl", "data.frame"))
在前十行中,仅显示“0.5mm”,但“1.0 mm”和“1.5 mm”的显示格式相同,但行号不同
setTz 具有相同的结构
有几种方法可以在这里实现您的目标。 因为您希望所有 y 轴“比率”具有相同的比例并且 x 轴始终相同,所以最简单的方法是 plot 使用facet_wrap()
和ggplot
。
library(data.table) # to use rbindlist()
library(dplyr) # to use mutate() and the pipe operator (optional)
library(tidyr) # to use gather()
library(ggplot2)
# first, combine setTxy and setTz into a single data frame and create a single column for all your "Ratio" values and one for the panel groups
dat <- rbindlist(list("x-,y-direction" = setTxy, "z-direction" = setTz), idcol = "source") %>%
gather(perc, Ratio, perc_DVHR_99, perc_DVHT_99) %>%
mutate(
perc = gsub("perc_DVHR_99", "Rotation", perc),
perc = gsub("perc_DVHT_99", "Translation", perc),
panel_name = factor(
paste(perc, source),
levels = c("Translation x-,y-direction", "Rotation x-,y-direction", "Translation z-direction", "Rotation z-direction")
)
)
# create a multi-panel plot with facet_wrap()
ggplot(data=dat, aes(VolumCat2, Ratio, fill = displ)) +
facet_wrap(~panel_name) +
geom_boxplot() +
scale_x_discrete("Volume Category", labels = My.labels) +
scale_fill_manual("Displacement", values = c("#999999", "#E69F00", "#56B4E9")) +
theme(
legend.position = "top", # legend is positioned at the top
strip.background = element_blank(), # "strip" refers to the part with the panel names; here, I turned its background to blank
strip.text = element_text(hjust = 0) # the panel titles become left-aligned
)
您可以使用theme()
进一步修改美学。 运行?theme
以获取更多信息。
PS - 注意dat
,为简单起见,我通过在创建此数据框时重命名变量来根据您的四个 plot 标题命名面板组。 当然,您可以直接在 ggplot 中进行重命名。
如果您想使用其他多plot替代品,您可以查看cowplot 的plot_grid()
或gridExtra 的grid.arrange()
选项。
编辑
要更改由facet_wrap(~panel_name)
创建的面板的排列,在panel_name
中应用factor(...)
以将级别或顺序分配给panel_name
中的唯一值。 facet_wrap(~panel_name)
创建的面板排列使用与级别相同的顺序(按照提供的顺序)。
否则,如果panel_name
保留为paste(...)
或字符 class,则面板将按字母顺序排列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.