How to combine legends for color and shape when geom_hline has a separate (optional) entry in the color legend?

I have the following code that creates the following graph:

cols <- brewer.pal(n = 3, name = 'Dark2')

p4 <- ggplot(all.m, aes(x=xval, y=yval, colour = Approach, ymax = 0.95)) + theme_bw() + 
  geom_errorbar(aes(ymin= yval - se, ymax = yval + se), width=5, position=pd) + 
  geom_line(position=pd) + 
  geom_point(aes(shape=Approach, colour = Approach), size = 4) + 
  geom_hline(aes(yintercept = cp.best$slope, colour = "C2P"), show_guide = FALSE) + 
  scale_color_manual(name="Approach", breaks=c("C2P", "P2P", "CP2P"), values =  cols[c(1,3,2)]) + 
  scale_y_continuous(breaks = seq(0.4, 0.95, 0.05), "Test AUROC") +
  scale_x_continuous(breaks = seq(10, 150, by = 20), "# Number of Patient Samples in Training")
p4 <- p4 + theme(legend.direction = 'horizontal', 
      legend.position = 'top', 
      plot.margin = unit(c(5.1, 7, 4.5, 3.5)/2, "lines"), 
      text = element_text(size=15), axis.title.x=element_text(vjust=-1.5), axis.title.y=element_text(vjust=2))   
p4 <- p4 + guides(colour=guide_legend(override.aes=list(shape=c(NA,17,16))))

p4

enter image description here When I try show_guide = FALSEin geom_point, the shape of the point in the top legend is set to solid circles by default.

How can I make the lower legend disappear without affecting the upper legend?

+4
source share
2 answers

This solution, complete with reproducible data:

library("ggplot2")
library("grid")
library("RColorBrewer")

cp2p <- data.frame(xval = 10 * 2:15, yval = cumsum(c(0.55, rnorm(13, 0.01, 0.005))), Approach = "CP2P", stringsAsFactors = FALSE)
p2p <- data.frame(xval = 10 * 1:15, yval = cumsum(c(0.7, rnorm(14, 0.01, 0.005))), Approach = "P2P", stringsAsFactors = FALSE)

pd <- position_dodge(0.1)
cp.best <- list(slope = 0.65)

all.m <- rbind(p2p, cp2p)
all.m$Approach <- factor(all.m$Approach, levels = c("C2P", "P2P", "CP2P"))
all.m$se <- rnorm(29, 0.1, 0.02)
all.m[nrow(all.m) + 1, ] <- all.m[nrow(all.m) + 1, ] # Creates a new row filled with NAs
all.m$Approach[nrow(all.m)] <- "C2P"
cols <- brewer.pal(n = 3, name = 'Dark2')

p4 <- ggplot(all.m, aes(x=xval, y=yval, colour = Approach, ymax = 0.95)) + theme_bw() + 
  geom_errorbar(aes(ymin= yval - se, ymax = yval + se), width=5, position=pd) + 
  geom_line(position=pd) + 
  geom_point(aes(shape=Approach, colour = Approach), size = 4, na.rm = TRUE) + 
  geom_hline(aes(yintercept = cp.best$slope, colour = "C2P")) + 
  scale_color_manual(values = c(C2P = cols[1], P2P = cols[2], CP2P = cols[3])) + 
  scale_shape_manual(values = c(C2P = NA, P2P = 16, CP2P = 17)) +
  scale_y_continuous(breaks = seq(0.4, 0.95, 0.05), "Test AUROC") +
  scale_x_continuous(breaks = seq(10, 150, by = 20), "# Number of Patient Samples in Training")
p4 <- p4 + theme(legend.direction = 'horizontal', 
                 legend.position = 'top', 
                 plot.margin = unit(c(5.1, 7, 4.5, 3.5)/2, "lines"), 
                 text = element_text(size=15), axis.title.x=element_text(vjust=-1.5), axis.title.y=element_text(vjust=2))   
p4

Plot of example

The trick is to make sure that all the desired levels are all.m$Approachdisplayed in all.m, even if one of them drops out of the graph. The warning about a missing point is suppressed by the argument na.rm = TRUEto geom_point.

+2
source

:
geom_point ( ), shape level, geom_hline.

geom_point(aes(shape = "int"), alpha = 0) 

:
ggplot / aes. , colour shape , .

, "x", "y" "grp" :

df <- data.frame(x = rep(1:2, 2), y = 1:4, grp = rep(c("a", "b"), each = 2))

color, shape 'grp'

ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
  geom_line() +
  geom_point(size = 4)

enter image description here

, aes, color shape .

a geom_hline. , geom_line . , color , .. color aes geom_hline. , . , .

ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
  geom_line() +
  geom_point(size = 4) +
  geom_hline(aes(yintercept = 2.5, color = "int"))

enter image description here

: color aes geom_line geom_hline, shape geom_point s. , "", color, : "grp" "int", geom_hline aes. , color shape, ggplot .

?

, shape, color, geom_point (alpha= 0), aes :

ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
  geom_line() +
  geom_point(size = 4) +
  geom_hline(aes(yintercept = 2.5, color = "int")) +
  geom_point(aes(shape = "int"), alpha = 0) # <~~~~ a blank geom_point

enter image description here

, factor "geom_hline" . drop = FALSE scale_shape_discrete, " ":

datadf$grp <- factor(df$grp, levels = c(unique(df$grp), "int"))

ggplot(data = df, aes(x = x, y = y, color = grp, shape = grp)) +
  geom_line() +
  geom_point(size = 4) +
  geom_hline(aes(yintercept = 2.5, color = "int")) +
  scale_shape_discrete(drop = FALSE)

, , guides "override" shape aes geom_hline, NA:

guides(colour = guide_legend(override.aes = list(shape = c(16, 17, NA))))
+1

All Articles