ggplot2:只使用图例中的颜色(图中没有符号)

时间:2018-05-18 07:18:36

标签: r plot ggplot2

这是我要创建的情节: enter image description here

但是,我不想在图例周围画框,也不要在彩色正方形内画圆圈。我找到了类似的探测here,但我无法弄清楚如何将它用于我的问题,因为它基本上仍然使用该图的符号。我想知道为什么ggplot2,一个我真正喜欢的包,无论如何都有这个愚蠢的功能。

目标传奇是: enter image description here

重现情节的代码:

library(ggplot2)
library(plyr)

N   <- 25
IV1 <- c('level1', 'level1', 'level1', 'level1', 'level1', 'level1', 'level1', 'level1', 'level2', 'level2', 'level2', 'level2', 'level2', 'level2', 'level2', 'level2')
IV2 <- c('level1', 'level1', 'level1', 'level1', 'level2', 'level2', 'level2', 'level2', 'level1', 'level1', 'level1', 'level1', 'level2', 'level2', 'level2', 'level2')
IV3 <- c('level1', 'level1', 'level2', 'level2', 'level1', 'level1', 'level2', 'level2', 'level1', 'level1', 'level2', 'level2', 'level1', 'level1', 'level2', 'level2')
IV4 <- c('level1', 'level2', 'level1', 'level2', 'level1', 'level2', 'level1', 'level2', 'level1', 'level2', 'level1', 'level2', 'level1', 'level2', 'level1', 'level2')

exampleData <- data.frame(subNum = rep(1:N, each = 16),
                          DV     = rnorm(N*length(IV1)),
                          IV1    = factor(rep(IV1, N), levels = c('level1', 'level2')),
                          IV2    = factor(rep(IV2, N), levels = c('level1', 'level2')),
                          IV3    = factor(rep(IV3, N), levels = c('level1', 'level2')),
                          IV4    = factor(rep(IV4, N), levels = c('level1', 'level2')))

exampleDataSummary <- ddply(exampleData, 
                            c('IV1', 'IV2', 'IV3', 'IV4'), 
                            summarise,
                            meanDV = mean(DV),
                            N = length(DV),
                            sdDV = sd(DV),
                            seDV = sdDV/sqrt(N))

plot <- ggplot(exampleData, aes(y = DV, x = IV1, fill = IV2))
plot + facet_grid(IV3 ~ IV4) + 
  geom_dotplot(binaxis = "y", alpha = 0.7, stackdir = "center", position = position_dodge(width = 1)) +
  geom_violin(aes(y = DV, fill = IV2), alpha = 0.5, position = position_dodge(width = 1)) +
  geom_point(aes(y = meanDV, x = IV1), data = exampleDataSummary, position = position_dodge(width = 1)) +
  geom_errorbar(aes(y = meanDV, ymin = meanDV - seDV, ymax = meanDV + seDV),
                color = "black", width = 0.2, data = exampleDataSummary, position = position_dodge(width = 1))

1 个答案:

答案 0 :(得分:4)

关闭相应geom中不需要的图例元素,然后使用override.aes

plot + facet_grid(IV3 ~ IV4) + 
  geom_dotplot(binaxis = "y", alpha = 0.7, stackdir = "center", 
                 position = position_dodge(width = 1), show.legend = FALSE) +
  geom_violin(aes(y = DV, fill = IV2), alpha = 0.5, position = position_dodge(width = 1)) +
  geom_point(aes(y = meanDV, x = IV1), data = exampleDataSummary, 
               position = position_dodge(width = 1), show.legend = FALSE) +
  geom_errorbar(aes(y = meanDV, ymin = meanDV - seDV, ymax = meanDV + seDV),
                color = "black", width = 0.2, data = exampleDataSummary, position = position_dodge(width = 1)) +
  guides(fill = guide_legend(override.aes = list(color = NA, alpha = 1)))

resulting plot