R 条形图中每个条形和每个组的轴标签

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

我正在尝试为条形图中的每个条形和每个组创建带有回避组的轴标签。相关问题在这里:带有躲避组的条形图中每个条形和每个组的轴标签

我希望最终结果看起来像这样,但有 3 组,每组 4 个小节

我在下面添加了示例数据

我已经尝试过

scale_x_discrete
scale_x_continuous
但它似乎不起作用。

# create a dataset 
specie <- c(rep("sorgho" , 3) , rep("poacee" , 3) , rep("banana" , 3) , rep("triticum" , 3) )
condition <- rep(c("normal" , "stress" , "Nitrogen") , 4)
value <- abs(rnorm(12 , 0 , 15))
df <- data.frame(specie,condition,value)
df


# Grouped
set.seed(123)
df

ggplot(df, aes(fill = specie, y = value, x = condition)) +
  geom_bar(stat="identity", position = position_dodge(width = 0.9), width = 0.6) +
  scale_fill_manual(
    "specie",
    values = c("black", "black", "black", "black"),
    
  ) +
  theme_classic() +
  theme(panel.border = element_rect(colour = "black", fill=NA, size=1),
        text = element_text(size = 8, family = "sans"),
  ) + labs(x = "Nitrogen                  normal          stress", y = expression(paste("Total Phytoplankton Abundance (", x10^{6}, "cells/L)"))) +
  scale_y_continuous(expand = c(0,0))  +   
  theme(legend.position="none") + scale_x_discrete("", breaks=c(-0.25,0.25,0.75,1.25,1.75,2.25,2.75,3.25,3.75,4.25,4.75,5.25),
                     labels=rep(c("sorgho","poacee", "banana", "triticum"),times=3)) 

电流输出

``scale_x_discrete`` 由于某种原因删除所有 x 轴标签。

r plot bar-chart rstudio
1个回答
0
投票

也许是这样的?

ggplot(df, aes(specie, value, fill = specie)) +
  geom_col(fill = "white") +
  geom_col(alpha = 0.5) +
  facet_grid(.~condition, switch = "x") +
  scale_x_discrete(NULL, expand = c(0.1, 0.5)) +
  scale_y_continuous(expand = c(0, 0), limits = c(0, 30)) +
  scale_fill_brewer(palette = "Set1", guide = "none") +
  theme_minimal(base_size = 20) +
  theme(strip.placement = "outside",
        panel.spacing.x = unit(0, "mm"),
        panel.grid.major.x = element_blank(),
        axis.text.x = element_text(size = 12),
        axis.ticks.x = element_line(color = "gray"),
        axis.line.x = element_line())

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