堆叠条形图的嵌套分组变量

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

我试图通过将 x 轴分成嵌套的样本子类别来改进该图的可视化。我无法使用 ggplot fill 来完成此操作,因为我已经使用了ivot_longer() 来包含 6 种类型的输出。该解决方案还必须允许使用facet_wrap()进行自由缩放视图,以检查各个蛋白质成分之间的趋势。

有: Current figure

想要: Desired figure

尝试: facet_wrap with free y-axis (pardon the x-axis labels, I've tried to conceal my actual data)

数据框:

>head(data)
>  ShortName  Mod Growth Day    Group        Conc
>1  CTRL_D02 CTRL     CC   2 Protein1 0.013072917
>2  CTRL_D02 CTRL     CC   2 Protein2 0.000000000
>3  CTRL_D02 CTRL     CC   2 Protein3 0.004661458
>4  CTRL_D02 CTRL     CC   2 Protein4 0.000000000
>5  CTRL_D02 CTRL     CC   2 Protein5 0.000000000
>6  CTRL_D02 CTRL     CC   2 Protein6 0.025885417

>CC <- data %>% slice(1:144)
>LG <- data %>% slice(145:210)

ggplot,堆叠条形图,用于查看所有蛋白质的广泛数量

p0 <- ggplot(CC) + 
  geom_col(aes(x = ShortName, y = Conc, fill = Group)) + 
  theme_clean() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) + 
  labs(y = "Concentration", x = "Sample", fill = "Composition")

ggplot,查看单个蛋白质成分的趋势

p0.1 <- p0 + 
  facet_wrap(. ~ Group, scales = "free_y") + 
  theme(legend.position="none")
r ggplot2 bar-chart axis-labels stacked-bar-chart
1个回答
0
投票

您可以使用 ggh4x 包来嵌套 x 轴刻度标签。

library(ggplot2); library(ggh4x)

ggplot(CC, aes(x=interaction(Day,Mod), Conc, fill = Group)) + 
  geom_col() + 
  scale_x_discrete(guide="axis_nested") +  # *** ggh4x *** #
  facet_grid(~Mod, scales = 'free') +
  labs(x="Sample", y="Concentration", fill="Composition", 
       title="Stacked bar graph of convoluted timecourse") +
  #theme_clean() +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
        panel.grid.major.x = element_blank(), 
        panel.grid.minor.x = element_blank(),
        strip.text = element_blank(),
        axis.line.x = element_line(),
        axis.line.y = element_line(),
        legend.box.background = element_rect())

enter image description here


数据:

data <- tibble(Day=sample(c('D02','D04','D07','D11','D02','D14'), 210, TRUE),
                   Mod=sample(c('CTRL','P2E8','P2G7','poly'), 210, TRUE),
                   Growth=sample(c('CC','LG'), 210, TRUE),
                   Group=sample(paste0("Protein", 1:6), 210, TRUE, prob=c(10,1,1,1,1,1)),
                   Conc=runif(210, 0, 0.3))
© www.soinside.com 2019 - 2024. All rights reserved.