为ggplot堆叠的条形图重新排列每个构面的升序 [英] reorder each facet ascending for a ggplot stacked bar graph

查看:90
本文介绍了为ggplot堆叠的条形图重新排列每个构面的升序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 库(tidyverse)dat<-read.table(text ="A B C1 232343242 34534 123 56 324 1244 34 234 1245123534654,sep =",标头= TRUE)%&%;%收集(键=变量",值=值")%&%;%group_by(变量)%&%;%mutate(ind = as.factor(rep(1:5)),perc =值/总和(值))ggplot(dat,aes(变量,perc,填充= ind))+geom_col()+scale_y_continuous(labels = scales :: percent_format())+facet_grid(〜变量,scales ="free_x")+主题(axis.title.x = element_blank(),axis.text.x = element_blank(),axis.ticks.x = element_blank()) 

上面的代码创建了堆叠的条形构面,您也可以在上面看到.每个条形图的 ind 部分的顺序与图例上显示的顺序相同.

我更希望每个 ind 部分都以升序(或降序)放置.我通常通过@dgrtwo和

解决方案

经过一些操作

与链接的答案相比,我主要对行进行排序并添加 ordering .为了恢复默认调色板,我还使用了 scales 中的 hue_pal .由于使用了 scale_fill_manual ,因此需要这样做,在这种情况下,需要手动提供颜色.

library(tidyverse)
dat <- read.table(text = "A B C
                          1   23  234 324
                          2   34  534 12
                          3   56  324 124
                          4   34  234 124
                          5   123 534 654",
                  sep = "", 
                  header = TRUE) %>% 
  gather(key = "variable", value = "value") %>% 
  group_by(variable) %>% 
  mutate(ind = as.factor(rep(1:5)), 
         perc = value / sum(value))

ggplot(dat, aes(variable, perc, fill = ind)) + 
  geom_col() +
  scale_y_continuous(labels = scales::percent_format()) + 
  facet_grid(~ variable, scales = "free_x") + 
  theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

The code above creates the stacked bar facet that you also see above. The ind portions of each bar graph are ordered the same as they are displayed on the legend.

I prefer that each ind portion is instead put in ascending (or descending) order. I usually accomplish this thanks to @dgrtwo and the reorder_within function.

reorder_within <- function(x, by, within, fun = mean, sep = "___", ...) {
  new_x <- paste(x, within, sep = sep)
  stats::reorder(new_x, by, FUN = fun)
}

But - using it in my case breaks apart the stacked bar graph above, into this exploded plot below. How do I keep the stacked bar graph, and order each ind ascending or descending?

Note - This is likely not a duplicate of several other "reorder within facets" questions. I have not been able to find a question that deals with stacked bar graphs, and the issue I describe above.

ggplot(dat, 
       aes(reorder_within(ind, value, variable), perc, fill = ind)) + 
  geom_col() +
  scale_y_continuous(labels = scales::percent_format()) + 
  facet_grid(~ variable, scales = "free_x") + 
  theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())

解决方案

After some manipulations this answer becomes

dat <- dat %>% arrange(variable, -perc) %>% mutate(ordering = row_number())
aux <- with(dat, match(sort(unique(ind)), ind))
ggplot(dat, aes(x = variable, y = perc, fill = interaction(-ordering, variable))) + 
  geom_col() + facet_grid(~ variable, scales = "free_x") + 
  scale_fill_manual("ind", values = scales::hue_pal()(5)[dat$ind],
                    labels = with(dat, ind[aux]), 
                    breaks = with(dat, interaction(-ordering, variable)[aux])) +
  theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank()) +
  scale_y_continuous(labels = scales::percent_format())

Comparing to the linked answer, I primarily sorted the rows and added ordering. As to recover the default color palette, I also used hue_pal from scales. This was needed due to using scale_fill_manual, in which case colors need to be provided manually.

这篇关于为ggplot堆叠的条形图重新排列每个构面的升序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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