颠倒堆叠条形图的顺序-在ggplot中突出显示

时间:2019-01-23 16:37:19

标签: r ggplot2

# Create the data
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)) %>% 
  arrange(variable, -perc) %>% 
  mutate(ordering = row_number())

# Plot the data
ggplot(dat, aes(variable, perc, fill = interaction(
  -ordering, variable))  # line #20
  ) + 
  geom_col(color = "white", size = 1.5, alpha = 0.25) + 
  facet_grid(~ variable, scales = "free_x") + 
  scale_fill_manual("ind", values = rep("black", length(dat$variable))) + 
  geom_col(data = dat %>% filter(ordering == 1),
           color = "white",
           size = 1.5,
           fill = "red",
           alpha = 0.5) +
  theme_minimal() + 
  theme(panel.grid.major.x = element_blank(), 
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(), 
        axis.title.y = element_blank(), 
        legend.position = "none") +
  scale_y_continuous(labels = scales::percent_format())

highlight stacked bar graph

我上面有突出显示,多面,堆叠的条形图。我想颠倒所有顺序,因此将第20行的-ordering更改为ordering。这给了我下面这张图。

highlighted stacked bar graph reverse

您可以看到我的第20行更改确实颠倒了此堆叠条形图灰色部分的顺序。但是,当我希望红色突出显示翻转到图表顶部时,它们会保留在图表底部。

我该如何实现?我在许多类似的SO问题中都尝试了答案,分别向我的position = position_fill(reverse = TRUE))和分别添加geom_col(),但是这三项新的尝试都没有用。我得到了与上面直接显示的图相同的图。

2 个答案:

答案 0 :(得分:3)

这是一种类似但编码不同的方法,其中还包括 alpha 的标度。这个想法是保持dat不变,但要为fillalpha设置手动比例。 ordering直接使用;无需致电interaction()

red <- 1L
n_ord <- length(unique(dat$ordering))
fill_scale <- c("red", rep("black", n_ord - 1L)) %>% 
  setNames(red * seq(n_ord))
alpha_scale <- c(0.5, rep(0.25, n_ord - 1L)) %>% 
  setNames(red * seq(n_ord))

# Plot the data
ggplot(dat, aes(variable, perc, fill = factor(red * ordering), alpha = factor(red * ordering))) + 
  # ggplot(dat, aes(variable, perc, fill = interaction(
  #   -ordering, variable))  # line #20
  # ) + 
  geom_col(color = "white", size = 1.5) + 
  scale_fill_manual(guide = "none", values = fill_scale) + 
  scale_alpha_manual(guide = "none", values = alpha_scale) + 
  facet_grid(~ variable, scales = "free_x") + 
  theme_minimal() + 
  theme(panel.grid.major.x = element_blank(), 
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(), 
        axis.title.y = element_blank(), 
        legend.position = "none") +
  scale_y_continuous(labels = scales::percent_format())

enter image description here

手动秤如下

fill_scale
      1       2       3       4       5 
  "red" "black" "black" "black" "black"
alpha_scale
   1    2    3    4    5 
0.50 0.25 0.25 0.25 0.25

如果切换变量red,即red <- -1L,我们可以重现OP的原始图:

enter image description here

答案 1 :(得分:2)

您还可以在定义变量的过程中使用交互,并通过以下方式解决问题:

library(tidyverse)

dat <- dat %>% 
  mutate(fill_breaks = as.character(interaction(-ordering, variable)), 
         fill_values = if_else(ordering == 1, "red", "black"))

fill_values <- dat$fill_values
names(fill_values) <- dat$fill_breaks


ggplot(dat, aes(variable, perc, fill = fill_breaks)) +
  geom_col(color = "white", size = 1.5, alpha = 0.25) +
  facet_grid( ~ variable, scales = "free_x") +
  scale_fill_manual(values = fill_values) +
  theme_minimal() +
  theme(panel.grid.major.x = element_blank(),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "none") +
  scale_y_continuous(labels = scales::percent_format())

enter image description here