JAQuent
JAQuent

Reputation: 1224

ggplot2: Just using colours in legend (no symbols for plots)

This is the plot I want to create: enter image description here

However, I don't want a frame around the legend and neither the circles within the coloured squares. I found a similar probem here, but I can't figure out how to use it for my problem because it essentially still uses the symbols of that plot. I wonder why ggplot2, a package I really love, has this stupid feature anyway.

The target legend is: enter image description here

The code to reproduce the plot:

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

Upvotes: 3

Views: 196

Answers (1)

Roland
Roland

Reputation: 132706

Switch off the legend elements you don't need in the respective geoms and then use 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

Upvotes: 4

Related Questions