按序列指示更改 ggplot2 中堆叠条形图的 y 方向顺序

Change order in y-direction of stacked bar chart in ggplot2 as indicated by sequence

使用这个可重现的例子tibble

 # install and attach packages
if (!require("pacman")) install.packages("pacman")
pacman::p_load(tidyverse, dplyr, ggplot2)  

# Create example dataset (tib1)
tib1 = tibble(location = c("loc1", rep(c("loc1", "loc2"), 8), rep("loc2",2)),
              drill = sort(c(1, rep(1:4, 4),4, 4)),
              thickness = c(20, 34, 99, 67, 29, 22, 53, 93, 64, 98, 76, 42, 49, 23, 11, 74, 
                            19, 50, 40),
              soiltype = c("gravel", rep( c("sand", "loam", "clay"),5 ), "sand", "gravel", "clay")) %>% 
  arrange(location, drill)

tib1 <- tib1 %>% group_by(location, drill) %>% 
  mutate(order = row_number(),
         bottom_of_layer = cumsum(thickness),
         top_of_layer = bottom_of_layer-thickness) %>% 
  ungroup %>% 
  select(location, drill, bottom_of_layer, top_of_layer, thickness, soiltype, order)

tib1

我想绘制土壤的横截面。我尝试使用 ggplot2 中的 geom_bargeom_col 来执行此操作。在示例数据中,给出了一个顺序 (1:3)。我希望按“顺序栏”中指定的顺序堆叠条形图。 所以这意味着酒吧在哪里:

橙色(壤土)-黄色(沙子)-灰色(砾石)-海绿色(粘土)

灰色(砾石)-黄色(沙子)-海绿色(黏土)

而其他堆叠条保持相同的顺序和颜色

换句话说:我需要堆叠条中的颜色随着“顺序”列中指示的顺序而变化。

colpalette = c("darkseagreen3", "darkgrey", "#FF9966", "palegoldenrod")

ggplot(tib1,
       aes(x = drill))+
  geom_bar(aes(fill = soiltype, y = -thickness),
           stat = "identity")+
  scale_fill_manual(values = colpalette)+
  facet_wrap(vars(location), scale = "free_x")+
  xlab("drill")+
  ylab("depth (cm)")+
  ggtitle("how to plot the bars in the 'preferred order'? ",
          subtitle = "the order of loc2 and drill == 4 should be: loam-sand-gravel-clay")+
  theme_minimal()

类似但略有不同的问题是:

and Change the order of Stacked Bar Chart in ggplot2

我想知道我要求的东西在 ggplot 中是否完全可行:

Bar charts are automatically stacked when multiple bars are placed at the same location. The order of the fill is designed to match the legend.

所以我可能不得不寻找替代方案...除非有人有技巧? 感谢任何帮助、答案、替代绘图选项、链接:) 如果可能的话,我更喜欢ggplot2。

您应该订购级别:

即:

tib1 <- tib1 %>% 
    mutate(soiltype = ordered(soiltype, c("clay", "gravel", "sand", "loam")))

colpalette = c("darkseagreen3", "darkgrey","palegoldenrod","#FF9966")

ggplot(tib1,
       aes(x = drill))+
    geom_bar(aes(fill = soiltype, y = -thickness),
             stat = "identity")+
    scale_fill_manual(values = colpalette)+
    facet_wrap(vars(location), scale = "free_x")+
    xlab("drill")+
    ylab("depth (cm)")+
    ggtitle("how to plot the bars in the 'preferred order'? ",
            subtitle = "the order of loc2 and drill == 4 should be: loam-sand-gravel-clay")+
    theme_minimal()

geom_colgeom_bar 不是合适的 ggplot2 函数,当您希望填充或颜色随 fill 和或 color 组合在一个栏中。 geom_rect 能够在 bar/rectangle.

中分隔两层 fillcolor
p1 <- ggplot(tib1)+
  geom_rect(data = tib1,
            aes(x = NULL, NULL, 
                xmin = drill-0.45, xmax = drill+0.45,
                fill = soiltype,
                ymin = -bottom_of_layer, ymax = -top_of_layer))+
  scale_fill_manual(values = colpalette)+
  facet_wrap(vars(location))+
  xlab("drill")+
  ylab("depth (cm)")+
  ggtitle("how to plot the bars in the 'preferred order'? ",
          subtitle = "the order of loc2 and drill == 4 should be: loam-sand-gravel-clay")+
  theme_minimal()

  # Optional:
  # Adding in text the column called "order"
p1 + geom_text(aes(x = drill,
                y = (bottom_of_layer + top_of_layer)/-2,
                label = order))

这对于离散的 x 轴值也是可能的: