r - How to merge legends for color and shape when geom_hline has a separate (additional) entry in the color legend? -


i have following code, produces following plot:

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, = 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 try show_guide = false in geom_point, shape of point in upper legend set default solid circles.

how can make lower legend disappear, without affecting upper legend?

this solution, complete 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 new row filled 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, = 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 make sure of desired levels of all.m$approach appear in all.m, if 1 of them gets dropped out of graph. warning omitted point suppressed na.rm = true argument geom_point.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -