[英]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.