2

我正在制作一个图,其中我有一个从facet_wrap. 九个地块中有八个使用geom_violin,而其余地块使用geom_bar. 在网站上找到一些有用的答案后,我得到了这一切。我遇到的问题是,当我fill = "white, color = "black"用于条形图时,它会在条形图绘制这些线。

这是一些示例代码和数字。

library(tidyverse)
n <- 100
tib <- tibble(value = c(rnorm(n, mean = 100, sd = 10), rbinom(n, size = 1, prob = (1:4)/4)),
              variable = rep(c("IQ", "Sex"), each = n),
              year = factor(rep(2012:2015, n/2)))
ggplot(tib, aes(x = year, y = value)) + 
  facet_wrap(~variable, scales = "free_y") +
  geom_violin(data = filter(tib, variable == "IQ")) +
  geom_bar(data = filter(tib, variable == "Sex"), stat = "identity", 
           color = "black", fill = "white")

在此处输入图像描述

现在我的问题是:我如何摆脱酒吧内的这些线条?我只希望它是带有黑色边框的白色。我一直在尝试各种配置,我可以设法摆脱线条,但代价是把刻面搞砸了。我相当肯定这与统计数据有关,但我在试图修复它时不知所措。有什么建议么?

4

2 回答 2

3

我建议在条形图中总结数据:

ggplot(tib, aes(x = year, y = value)) + 
  facet_wrap(~variable, scales = "free_y") +
  geom_violin(data = filter(tib, variable == "IQ")) +
  geom_bar(data = tib %>%
             group_by(year,variable) %>%
             summarise(value=sum(value)) %>%
             filter(variable == "Sex"),
           stat = "identity", 
           color = "black",
           fill = "white")
于 2017-05-24T09:04:31.270 回答
3

我不确定这是表示数据的好方法,不同面板的 y 轴表示非常不同的事物,但请接受您的示例可能与您的实际用例不匹配。制作单独的图然后使用gridExtra::grid.arrange, orcowplot::plot_grid可能是更好的解决方案。

但是如果你想这样做

ggplot(tib, aes(x = year, y = value)) + 
  facet_wrap(~variable, scales = "free_y") +
  geom_violin(data = filter(tib, variable == "IQ")) +
  geom_col(data = filter(tib, variable == "Sex") %>%
                  group_by(year, variable) %>% 
                  summarise(value = sum(value)), 
    fill = "white", colour = "black")

使用geom_col而不是geom_bar所以我不需要使用stat = identity.

于 2017-05-24T09:09:03.947 回答