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

标签 r plot ggplot2

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

但是,我不想要图例周围的框架,也不想要彩色方 block 内的圆圈。我发现了一个类似的 probem 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))

最佳答案

在各自的geoms中关闭你不需要的图例元素,然后使用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

关于r - ggplot2:只使用图例中的颜色(没有符号用于绘图),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50405845/

相关文章:

r - 使用 with par(mfrow=c()) 在一张图中绘制多个 fill.contour 图

r - 翻转 gg TreeMap

r - 如何更改 ggplot2 中的轴标签颜色?

r - 通过结合两个随机变量之间的相关性从指数分布生成数据

在 R 中旋转条形图的 x 轴标签

r - 如何在ggplot2中抖动线条

r - 在ggplot2中使用stat_summary计算平均值和sd,然后连接误差线的平均值

r - 如何使用 sliderInput 值来选择列并使用它来更改 map R?

r - 如何在ggplot2图例标签中添加Latex代码?

r - 如何剪辑 coord_polar 图