0

我无法在堆积条形图中显示 y 值,因为它们出现但显示在条形后面,因此不可见。

我的目标是在堆积条形图中显示值,以便它们可见。任何帮助都会受到高度评价。非常感谢!

应在每个条形的相应堆栈中显示的值如下:

RAIH <- c(11,13,10,5,7,10,8,4,11,11,4,3,7,9,3,2,11,15,10,6,11,13,14,7,8,16,10,7,6,8,6,4,11,8,5,3,10,12,3,1,11,10,5,2,10,11,4,2,17,12,7,6,9,10,5,3,18,17,7,7,14,11,6,2)

我使用以下代码来显示值,这也包含在主代码中。

geom_text(aes(label = RAIH), size = 3, 
            position = position_stack(vjust = 0.5))

我用于创建图表的其余代码是:

 library(ggplot2)
  library(patchwork)
  library(dplyr)
  library(plotrix)
  library(plyr)

     ploughed1 <- Data_GG %>%
    dplyr::select(Tillage, RAI_N, Mulch, compost, Ferment, Horizont, mean, se) %>%
    filter(Tillage == "Ploughed", Mulch=="No Mulch") %>%
    group_by(Tillage)
  
  ploughed2 <- Data_GG %>%
    dplyr::select(Tillage, RAI_N, Mulch, compost, Ferment, Horizont, mean, se) %>%
    filter(Tillage == "Ploughed", Mulch=="Mulch") %>%
    group_by(Tillage)
  
  
  reduced1 <- Data_GG %>%
    dplyr::select(Tillage, RAI_N, Mulch, compost, Ferment, Horizont, mean, se) %>%
    filter(Tillage == "Reduced",  Mulch=="No Mulch") %>%
    group_by(Tillage) 
  
  reduced2 <- Data_GG %>%
    dplyr::select(Tillage, RAI_N, Mulch, compost, Ferment, Horizont, mean, se) %>%
    filter(Tillage == "Reduced",  Mulch=="Mulch") %>%
    group_by(Tillage) 

     plot_fun <- function(x, title) {
    ggplot(arrange(x, Horizont), aes(Ferment, RAI_N, label = RAIH, 
                                     fill = factor(Horizont, levels = c("4", "3", "2", "1")))) +
      geom_bar(stat = "identity", position = "dodge") +
      geom_text(aes(label = RAIH), size = 3, 
                position = position_stack(vjust = 0.5))+
      scale_fill_manual(name ="Soil layer (cm)", 
                        values = c("#FF9933", "#CC6600", "#663300", "#000000"),
                        labels = c("22,5 - 30", "15 - 22,5", "7,5 - 15", "0 - 7,5")) +
      guides(fill = guide_legend(reverse = TRUE)) +
      ylim(0, 100) +
      theme_bw() +
      facet_wrap(~compost) +
      theme(
        strip.text = element_text(size = 6),
        panel.spacing = unit(0.2, "lines"),
        plot.margin = margin(1,1,1,1)
      ) +
      geom_col(position = position_stack(reverse = TRUE)) +
      labs(x = "Ferment", y = "RAI", fill = "Horizon", title = title)
  }
  
  remove_y <- theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.y = element_blank())
        
    remove_x <- theme(
      axis.title.x = element_blank())
  
  p <- list(
    plot_fun(ploughed1, "P-"),
    plot_fun(ploughed2, "P+") + remove_y + remove_x,
    plot_fun(reduced1, "RT-") + remove_y + remove_x,
    plot_fun(reduced2, "RT+") + remove_y + remove_x
  )
  wrap_plots(p, nrow = 1) + plot_layout(guides = "collect")

输出图

4

0 回答 0