ggboxplot 忽略因子水平

问题描述 投票:0回答:1

我制作了一个

ggboxplot
,我对此非常满意,除了我的 x 值的顺序已更改。 x 值是我为其定义水平的因素。然而,在剧情中这个顺序是不同的。我想该顺序可能与设置的其他因子级别或嵌套方面冲突,但我似乎无法修复它。

绘图代码:

library(ggplot2)
library(ggpubr)
library(ggh4x)

#setting levels:
counts_fe_d7_long$day <- factor(counts_fe_d7_long$day, 
                                levels = c("WT", "vanA", "inoculum"))
counts_fe_d7_long$inoculum <- factor(counts_fe_d7_long$inoculum,
                                     levels = c("WT", "vanA", "vanA narAB"))
counts_fe_d7_long$fe_group <- factor(counts_fe_d7_long$fe_group,
                                     levels = ("1","2","3","4","5","6","7","8")) 

#plotting:
p_d7_log <- ggboxplot(data = counts_fe_d7_long,  #generate plot
                      x = "fe_group", 
                      y = "CFU_g_log",
                      color = "day",      #coloring of boxes
                      palette = "jco",    #nature of colors used
                      add = "jitter",)+   #dispersing points so they don't overlap 
  theme(panel.border = element_rect(linetype="dotted",color="grey", fill=NA), 
        panel.spacing = unit(0, 'pt'), legend.position="right",
        strip.text = element_text(size = 7,margin = margin()))+
  # "margin" for space in facet boxes, 
  # "stri.text" for text size in box and "panel.spacing" for space between boxes.  
  labs(x = "group", y = "log(CFU/g+1)",color = NULL)+
    # "free" argument removes empty space in the plot. 
    # In contrary to facet_grid, facet_nested allows for merger of facet boxes. 
  facet_nested( ~ inoculum + treatment,scales = "free_x", space = "free_x" ) 

p_d7_log 

数据:

counts_fe_d7_long <- 
structure(list(inoculum = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), levels = c("WT", "vanA", 
"vanA narAB"), class = "factor"), treatment = c("-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "-", "narasin", "narasin", 
"narasin", "narasin", "narasin", "narasin", "narasin", "narasin", 
"narasin", "narasin", "narasin", "narasin", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "narasin", "narasin", 
"narasin", "narasin", "narasin", "narasin", "narasin", "narasin", 
"narasin", "narasin", "narasin", "narasin", "-", "-", "-", "-", 
"-", "-", "-", "-", "-", "-", "-", "-", "narasin", "narasin", 
"narasin", "narasin", "narasin", "narasin", "narasin", "narasin", 
"narasin", "narasin", "narasin", "narasin", "monensin", "monensin", 
"monensin", "monensin", "monensin", "monensin", "monensin", "monensin", 
"monensin", "monensin", "monensin", "monensin", "lasalocid", 
"lasalocid", "lasalocid", "lasalocid", "lasalocid", "lasalocid", 
"lasalocid", "lasalocid", "lasalocid", "lasalocid", "lasalocid", 
"lasalocid"), ID_1 = c("1x", "1x", "1x", "A", "A", "A", "B", 
"B", "B", "C", "C", "C", "2x", "2x", "2x", "D", "D", "D", "E", 
"E", "E", "F", "F", "F", "3X", "3X", "3X", "G", "G", "G", "H", 
"H", "H", "I", "I", "I", "4X", "4X", "4X", "J", "J", "J", "K", 
"K", "K", "L", "L", "L", "5X", "5X", "5X", "M", "M", "M", "N", 
"N", "N", "O", "O", "O", "6X", "6X", "6X", "P", "P", "P", "Q", 
"Q", "Q", "R", "R", "R", "7X", "7X", "7X", "S", "S", "S", "T", 
"T", "T", "U", "U", "U", "8X", "8X", "8X", "V", "V", "V", "W", 
"W", "W", "X", "X", "X"), fe_group = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 
6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 
7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L), levels = c("1", 
"2", "3", "4", "5", "6", "7", "8"), class = "factor"), day = structure(c(3L, 
2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 
3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 
2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 
1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 
3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L, 3L, 2L, 1L), levels = c("WT", 
"vanA", "inoculum"), class = "factor"), CFU_g = c(0, 173000, 
27699827000, 0, 94700, 30799905300, 0, 55600, 9439944400, 0, 
66200, 3709933800, 0, 7.5e+08, 6.75e+09, 0, 1610, 5140, 0, 6670000, 
60030000, 0, 33200000, 108800000, 0, 6210000, 30993790000, 878000, 
110000, 21999012000, 309000, 140691000, 5.629e+09, 1060000, 13040000, 
3075900000, 4500000, 159500000, 1.026e+09, 3090000, 208910000, 
0, 25700, 4864300, 1405110000, 19600, 12680400, 1457300000, 131000, 
1179000, 11298690000, 66700, 83300, 3829850000, 32900, 241100, 
6589726000, 0, 60000, 0, 81800, 24418200, 16400000, 50000, 149950000, 
0, 0, 314000, 11986000, 0, 1.38e+08, 7.5e+07, 2e+07, 1.23e+09, 
8.75e+09, 726000, 5809274000, 2.9e+09, 132000, 608868000, 0, 
365000, 254635000, 6.1e+07, 0, 9960000, 9340000, 0, 1.37e+08, 
9.8e+07, 0, 56300000, 56700000, 0, 6210000, 0), CFU_g_log = c(0, 
5.23804861349427, 10.442477056691, 0, 4.97635456498204, 10.488549381198, 
0, 4.74508260256365, 9.97496943641565, 0, 4.82086454972976, 9.56936616025739, 
0, 8.87506126397076, 9.82930377289536, 0, 3.20709554041922, 3.71104760386703, 
0, 6.82412589902816, 7.7783683505905, 0, 7.5211380967852, 8.03662889935384, 
0, 6.79309167011127, 10.4912746861418, 5.94349501054646, 5.04139663327193, 
10.3424031766336, 5.48995988490632, 8.14826631960226, 9.75043124873736, 
6.02530627497635, 7.11527762470069, 9.48797221223478, 6.65321261028522, 
8.20276069011605, 9.01114736119909, 6.48995861997319, 8.31995923114904, 
0, 4.40995002162051, 6.68702044097571, 9.14771032492243, 4.29227822867297, 
7.10313298772028, 9.16354896519982, 5.11727461086816, 6.07151417345327, 
10.0530280931899, 4.82413234502939, 4.92065021499472, 9.58318176482667, 
4.51720909818954, 5.38219901167808, 9.81886735712396, 0, 4.77815848856469, 
0, 4.91275861286237, 7.38771366433642, 7.21484387452907, 4.6989786901388, 
8.17594647299181, 0, 0, 5.49693103117446, 7.07867430959394, 0, 
8.1398790895483, 7.87506126918229, 7.3010300173787, 9.08990511179248, 
9.94200805307195, 5.86093721890145, 9.76412186095135, 9.46239799804871, 
5.1205772213031, 8.78452315034899, 0, 5.56229405430274, 8.40591809961771, 
7.78532984213035, 0, 6.99825938202756, 6.97034692272843, 0, 8.13672057032644, 
7.99122608012407, 0, 7.75050840256528, 7.75358306655242, 0, 6.79309167011127, 
0)), row.names = c(NA, -96L), class = c("tbl_df", "tbl", "data.frame"
))
r ggplot2 facet-grid factors ggboxplot
1个回答
0
投票

您需要根据

treatment
;
fe_group

列重新排序
library(ggplot2)
library(ggpubr)
library(ggh4x)
library(dplyr)

counts_fe_d7_long %>% 
  mutate(day = factor(day,  c("WT", "vanA", "inoculum")),
         inoculum = factor(inoculum, c("WT", "vanA", "vanA narAB")),
         fe_group = factor(fe_group, c("1","2","3","4","5","6","7","8")),
         treatment = factor(treatment, unique(treatment[order(fe_group)]),
                                       ordered=TRUE)) %>% 
 ggboxplot(x = "fe_group", y = "CFU_g_log",
           color = "day", palette = "jco", add = "jitter") +   
  theme(panel.border = element_rect(linetype="dotted", color="grey", fill=NA),
        panel.spacing = unit(0, 'pt'),
        strip.text = element_text(size = 7, margin = margin()),
        legend.position = "right") +
  labs(x = "group", y = "log(CFU/g+1)", color = NULL) +
  facet_nested( ~ inoculum + treatment, scales = "free_x", space = "free_x") 

创建于 2024-03-24,使用 reprex v2.0.2

© www.soinside.com 2019 - 2024. All rights reserved.