繁体   English   中英

如何用ggplot2中的另一个变量填充分组/躲闪条?

[英]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对应的条的部分具有不同的颜色阴影。

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

为标志使用不同的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")

在此输入图像描述

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")

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM