堆叠棒的顺序ggplot2-土壤剖面 [英] Order of stacked bars ggplot2 - Soil profile

查看:55
本文介绍了堆叠棒的顺序ggplot2-土壤剖面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ggplot2 中条形图的

它适用于 L F H ,但不适用于 A B (低于地面,即负值).之所以可能不起作用,是因为按 site (最大分别为正值和负值)将堆叠的条形从最大到最小排序,然后以自上而下的方式堆叠.这样对吗?如果是这样,那么对于我的正值,图例与我相信的堆积柱相匹配只是一个巧合.

我想要实现的是将条形图堆叠起来,使其与图例中的顺序(从上到下)相匹配,因此在横截面图中查看时也与土壤剖面相匹配,我不确定如何解决这个问题.

我确实尝试更改了排序行为,但它产生了与上述相同的图:

  df%>%mutate(水平=因子(水平,水平= c("L","F","H","A","B")))%>%排列(desc(值))%&%;%ggplot(aes(site,value))+ geom_col(aes(fill = horizo​​n))+ labs(y =土壤深度(cm)")df%>%mutate(水平=因子(水平,水平= c("L","F","H","A","B")))%>%排列(值)%&%;%ggplot(aes(site,value))+ geom_col(aes(fill = horizo​​n))+ labs(y =土壤深度(cm)") 

我可能必须分别对正值和负值进行排序,分别是降序还是升序?

解决方案

在堆积条形图中进行排序是根据相应因子的级别进行的.负值出现了潜在问题,这些负值反向堆叠(从负数顶部到0).为了说明问题,让所有值都为负:

  df%>%mutate(水平=因子(水平,水平= c("L","F","H","B","A")))%>%ggplot(aes(site,value-20))+ geom_col(aes(fill = horizo​​n))+ labs(y =土壤深度(cm)") 

一种解决方法是指定不同的级别顺序,这将导致所需的填充顺序(在这种情况下: levels = c("L","F","H","B","A)),然后使用 scale_fill_discrete 手动调整图例:

  df%>%mutate(水平=因子(水平,水平= c("L","F","H","B","A")))%>%ggplot(aes(site,value))+ geom_col(aes(fill = horizo​​n))+ labs(y =土壤深度(cm)")+scale_fill_discrete(breaks = c("L","F","H","A","B")) 

The documentation for bar charts in ggplot2 says (see example 3):

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.

For some reason the second sentence doesn't work for me. Here is an example data set, which represents soil layers above (leaf litter etc.) and below ground (actual soil):

df <- structure(list(horizon = structure(c(5L, 3L, 4L, 2L, 1L, 5L, 
3L, 4L, 2L, 1L, 5L, 3L, 4L, 2L, 1L, 5L, 3L, 4L, 2L, 1L, 5L, 3L, 
4L, 2L, 1L, 5L, 3L, 4L, 2L, 1L), .Label = c("A", "B", "F", "H", 
"L"), class = "factor"), site = structure(c(1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 5L, 
5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L), .Label = c("A", "B", "C", 
"D", "E", "F"), class = "factor"), value = c(2.75, 0.5, 0.25, 
-4.125, -3.375, 3.78125, 1.375, 0.625, -10.6875, -6.34375, 4.28, 
2.065, 0.68, -12.1, -10.75, 8.583333333, 4.541666667, 2.166666667, 
-10.70833333, -4.25, 7.35, 4, 1.8, -13.95, -5.175, 1.933333333, 
1.245833333, 0.641666667, -11.16666667, -2.291666667)), .Names = c("horizon", 
"site", "value"), class = "data.frame", row.names = c(NA, -30L
))

Now I try to plot the data by first specifying the order of the soil layer levels (i.e. horizons, from above to below ground):

require(ggplot2); require(dplyr)
df %>% 
  mutate(horizon = factor(horizon, levels = c("L","F","H","A","B")))  %>% 
  ggplot(aes(site, value)) + geom_col(aes(fill = horizon)) + labs(y = "Soil depth (cm)")

It works for L, F, H but not for A, B (below ground, i.e. negative values). The reason why it probably doesn't work is that the stacked bars are sorted from largest to smallest by site (for both positive and negative values separately) and then stacked in a top to bottom approach. Is this correct? If that's the case, then for my positive values it was just coincidence that the legend matched the stacked bars I believe.

What I would like to achieve is a stacking of the bars that matches the order (top to bottom) in the legend and hence also the soil profile when looking at it in a cross-sectional view and I am not sure how to approach this.

I did try to change the sorting behaviour in general but it produced the same plot as above:

df %>% 
  mutate(horizon = factor(horizon, levels = c("L","F","H","A","B")))  %>% 
  arrange(desc(value)) %>% 
  ggplot(aes(site, value)) + geom_col(aes(fill=horizon)) + labs(y = "Soil depth (cm)")

df %>% 
  mutate(horizon = factor(horizon, levels = c("L","F","H","A","B")))  %>% 
  arrange(value) %>% 
  ggplot(aes(site, value)) + geom_col(aes(fill=horizon)) + labs(y = "Soil depth (cm)")

I probably have to sort positive and negative values separately, that is descending and ascending, respectively?

解决方案

Sorting in a stacked bar plot is done according to levels of the corresponding factor. The potential problem arises with negative values which are stacked in reverse (from the negative top towards 0). To illustrate to problem lets make all the values negative:

df %>%  
  mutate(horizon = factor(horizon, levels = c("L","F","H","B","A"))) %>%
  ggplot(aes(site, value - 20)) + geom_col(aes(fill = horizon)) + labs(y = "Soil depth (cm)")

A workaround is to specify a different order of levels which will result in the wanted fill order (in this case: levels = c("L","F","H","B","A")) and manually adjust the legend using scale_fill_discrete:

df %>%  
  mutate(horizon = factor(horizon, levels = c("L","F","H","B","A"))) %>%
  ggplot(aes(site, value)) + geom_col(aes(fill = horizon)) + labs(y = "Soil depth (cm)")+
  scale_fill_discrete(breaks = c("L","F","H","A","B"))

这篇关于堆叠棒的顺序ggplot2-土壤剖面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆