如何在 ggplot2 中用另一个变量填充 grouped/dodged 条?

How to fill grouped/dodged bars with another variable in ggplot2?

我想得到这段代码给出的相同图表:

library(ggplot2)

name <- c("A","A","A","A","B","B","B","B")
size <- c("small","small","tall","tall","small","small","tall","tall")
flag <- c(0,1,0,1,0,1,0,1)
quantity <- c(26,13,12,4,19,14,13,5)

df <- data.frame(name,size,flag,quantity)

ggplot(data = df, mapping = aes(x = name, y = quantity)) +
 geom_bar(aes(fill = size), position = "dodge", stat = "identity")

除了我想根据变量标志拆分条形。理想的情况是,对应于 flag = 0.

的条形部分具有不同的颜色深浅

我还需要标志变量的图例。

library(ggplot2)

df1 <- data.frame(name = c("A","A","A","A","B","B","B","B"),
                  size = c("small","small","tall","tall","small","small","tall","tall"),
                  flag = c(0,1,0,1,0,1,0,1),
                  quantity = c(26,13,12,4,19,14,13,5))

ggplot(data = df1, mapping = aes(x = name, y = quantity)) +
  geom_bar(aes(fill = size), alpha = ifelse(flag==0, 0.6, 1),
           position = "dodge", stat = "identity")

@kath 答案之上添加:

library(grid)
library(ggplot2)

gg_color_hue <- function(n) {
                             hues = seq(15, 375, length = n + 1)
                             hcl(h = hues, l = 65, c = 100)[1:n]
                             }

mycols <- gg_color_hue(length(unique(interaction(df$size, df$flag)))/2)


ggplot(data = df, mapping = aes(x = interaction(name, size), y = quantity)) +
  geom_bar(aes(fill = interaction(factor(size), factor(flag))), 
           position = "stack", stat = "identity") +
  scale_fill_manual(name = "Size and Flag",
                    values = c("small.0" = alpha(mycols[1], 3/5),
                               "tall.0" = alpha(mycols[2], 3/5),
                               "small.1" = alpha(mycols[1], 1),
                               "tall.1" = alpha(mycols[2], 1)),
                    labels = c("Size: small and Flag: 0",
                               "Size: tall and Flag: 0",
                               "Size: small and Flag: 1",
                               "Size: tall and Flag: 1")) +
  facet_wrap(~ name, strip.position = "bottom", scales = "free_x") +
  theme(axis.ticks.x = element_blank(), 
        axis.text.x = element_blank(), 
        strip.background = element_blank(),
        panel.spacing = unit(-1.25, "lines")) + 
  xlab("name")

为标志使用不同的 alpha 值:
这为标志添加了不同的透明度值。然而,所有的酒吧都被闪避了。

ggplot(data = df, mapping = aes(x = name, y = quantity)) +
  geom_bar(aes(fill = size, alpha = factor(flag)), position = "dodge", stat = "identity") +
  scale_alpha_manual("flag", values = c(0.3, 1))

facet_wrap 与 x 轴(名称、大小)交互的组合:
保持相同大小的条形堆叠,需要解决方法才能获得漂亮的 x 轴。

ggplot(data = df, mapping = aes(x = interaction(name, size), y = quantity)) +
  geom_bar(aes(fill = size, alpha = factor(flag)), 
           position = "stack", stat = "identity") +
  scale_alpha_manual("flag", values = c(0.3, 1)) +
  facet_wrap(~ name, strip.position = "bottom", scales = "free_x") +
  theme(axis.ticks.x = element_blank(), 
        axis.text.x = element_blank(), 
        axis.title.x = element_blank(), 
        strip.background = element_blank())

与大小的互动:
通过指定 scale_fill_manual,您可以为大小和标志的不同组合分配不同的颜色。

ggplot(data = df, mapping = aes(x = name, y = quantity)) +
  geom_bar(aes(fill = interaction(size, flag)), position = "dodge", stat = "identity")