突出显示ggplot堆叠条形图的各个部分

时间:2019-01-22 22:50:52

标签: 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))) + 
  geom_col(color = "black") + 
  facet_grid(~ variable, scales = "free_x") + 
  scale_fill_manual("ind", values = rep("grey", length(dat$variable))) + 
  # geom_col(data = dat %>% filter(ordering == 1), fill = "blue") %>% # line 23 
  theme(axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank(), 
        legend.position = "none") +
  scale_y_continuous(labels = scales::percent_format())

stacked bar

如何突出显示上方每个堆叠条形图的三个最大部分?基本上,我希望每个构面的底部块“ A”,“ B”和“ C”都以蓝色突出显示。

我想这是通过取消注释掉我上面注释的行(第23行)来实现的。不幸的是,这行不通。

0 个答案:

没有答案