使用ggplot r将多个箱形图组合为一个图形

时间:2020-07-05 15:14:40

标签: r ggplot2 ggpubr

使用以下代码,我创建了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" )

这将创建图像 enter image description here

但是如何进行以下更改: 1)对于前两个箱形图,我想松开x标签 2)对于所有图,我希望具有相同的y轴间距 3)对于左边的两个方框图,我想放松一下对“偏移”的解释。 如果可能的话,我想在所有绘图中都放开它,然后在4个箱形图的顶部添加,灰色表示0.5mm,橙色表示1.0mm,蓝色表示1.5mm

欢迎提出其他建议以使该图看起来更好

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具有相同的结构

1 个答案:

答案 0 :(得分:1)

这里有几种方法可以实现您的目标。因为您希望所有y轴的“比率”都具有相同的比例,并且x轴始终相同,所以最简单的方法是使用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了解更多信息。

P.S。 -请注意,dat中,为简单起见,我在创建此数据框时通过重命名变量来根据您的四个图表标题来命名面板组。您当然可以do the renaming directly in the ggplot instead

如果您想使用其他多个 plot 替代方案,则可以查看cowplot's plot_grid()gridExtra's grid.arrange()选项。

修改

要更改由facet_wrap(~panel_name)创建的面板的排列,请在factor(...)中应用panel_name,以向panel_name中的唯一值分配级别或顺序。由facet_wrap(~panel_name)创建的面板布置使用与级别相同的顺序(按提供的顺序)。
否则,如果将panel_name保留为paste(...)或字符类,则面板将按字母顺序排列。