ggplot堆叠栏的奇怪排序 [英] ggplot strange ordering of stacked bar

查看:72
本文介绍了ggplot堆叠栏的奇怪排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个类似



问题与零在右侧堆叠的数据的顺序有关,在这些位置,它们的堆叠似乎是降序排列的订购。我们将不胜感激关于如何解决此问题的任何想法(预期顺序为0-10,而不是0-5,10-5)

解决方案

一个艰难的人!我在按顺序玩游戏,当您在其中组合正负值时,似乎 geom_bar geom_col 不喜欢它常见的相同顺序。因此,我将数据框内的数据分为正值和负值,为每个响应值生成颜色,并对正负值分别使用两个几何:

  library(tidyverse)
库(RColorBrewer)
x<-tribble(
〜response,〜count,
0,-27,
1 ,-9,
2,-41,
3,-43,
4,-58,
5,-120,
5,120,
6,233,
7,379,
8,388,
9,145,
10,61
)%>%
#获取绝对值并添加虚拟值以区别正负值
mutate(subzero = count <0,
count = abs(count))

#生成带有颜色的变量从ColorBrewer获得的每个响应级别(难看但有效)
colors<-brewer.pal(length(unique(x $ response)), RdBu)
x $ colors-NA
代表(i in 1:nrow(x)){
x $ colors [i]<-color s [x $ response [i] +1]
}


ggplot()+
geom_bar(data = x [x $ subzero == T,] ,aes(x =,y = -count,fill = reorder(colors,response)),position = stack,stat = identity)+
geom_bar(data = x [x $ subzero = = F,],aes(x =,y =计数,填充=重新排序(颜色,-响应)),position = stack,stat = identity)+
geom_hline(yintercept = 0, color = c( black))+
scale_fill_identity( Response,标签=唯一(x $ response),breaks =唯一(x $ colors),guide = legend)+
coord_flip ()+
labs(y =,x =)+
theme(legend.position = bottom,legend.direction = horizo​​ntal)+
scale_y_continuous(breaks = seq(-1400,1400,200),限制= c(-1400,1400))

UPD:使Y比例平衡,因此看起来更清晰


I am attempting to create a diverging stacked bar like here, and am experiencing a similar issue to this SO question. My approach is slightly different though as I am managing it all via a single dataset, rather than two and my colours are independent of my data.

Reprex as follows:

library(tidyverse)
library(RColorBrewer)
x <- tribble(
  ~response, ~count,
  0,         -27,
  1,          -9,
  2,         -41,
  3,         -43,
  4,         -58,
  5,        -120,
  5,         120,
  6,         233,
  7,         379,
  8,         388,
  9,         145,
  10,         61
) %>% 
  mutate(response = factor(response))

ggplot(x, aes(x = 1, y = count, fill = response)) +
  geom_col() +
  scale_fill_brewer(palette = "RdBu") +
  coord_flip()

This gives me an image like this:

The issue is to do with the ordering of the stacked data on the right hand side of the zero where they stacking appears to be in descending order. Any thoughts on how to fix this would be greatly appreciated (expected ordering would be 0-10, not 0-5,10-5)

解决方案

A tough one! I played with ordering and it seems that geom_bar and geom_col don't like it when you combine positive and negative values in the common same order. So I divided your data inside the dataframe for positive and negative values, generated colors for every response value and used two geoms for positive and negative values separately:

library(tidyverse)
library(RColorBrewer)
x <- tribble(
  ~response, ~count,
  0,         -27,
  1,          -9,
  2,         -41,
  3,         -43,
  4,         -58,
  5,        -120,
  5,         120,
  6,         233,
  7,         379,
  8,         388,
  9,         145,
  10,         61
) %>% 
  # Get absolute values and add dummy to distuingish positive and negative values
  mutate(subzero = count < 0,
         count = abs(count))

# Generate variable with colors from ColorBrewer for every response level (ugly but works)
colors <- brewer.pal(length(unique(x$response)),"RdBu")
x$colors <- NA
for (i in 1:nrow(x)){
  x$colors[i] <- colors[x$response[i]+1]
}


ggplot() +
  geom_bar(data = x[x$subzero==T,], aes(x = "", y = -count, fill = reorder(colors, response)), position="stack", stat="identity") +
  geom_bar(data = x[x$subzero==F,], aes(x = "", y = count, fill = reorder(colors, -response)), position="stack", stat="identity") +
  geom_hline(yintercept = 0, color =c("black")) +
  scale_fill_identity("Response", labels = unique(x$response), breaks=unique(x$colors), guide="legend") +
  coord_flip() +
  labs(y="",x="") +
  theme(legend.position = "bottom", legend.direction = "horizontal") +
  scale_y_continuous(breaks=seq(-1400,1400,200), limits=c(-1400,1400))

UPD: made Y-scale balanced so it look more clear

这篇关于ggplot堆叠栏的奇怪排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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