Cheng
Cheng

Reputation: 770

How to mix different symbols for legend keys in ggplot2?

I have the following code that produces the figure.

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(position=pd) + 
  geom_hline(aes(yintercept = cp.best$slope, colour = "C2P"), show_guide = FALSE) + 
  scale_color_manual(name="Appraoch", 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

Figure Produced

How can I change the symbol to a line without a dot in the legend for "C2P" only, without affecting the symbols for "P2P" and "CP2P"?

Upvotes: 3

Views: 491

Answers (1)

eipi10
eipi10

Reputation: 93871

You can remove the point marker from the legend using override.aes by adding the following line of code to your plot:

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

override.aes changes the legend without changing the plot. In this case, we want to change the legend point markers, so we want to change the shape of the points. Point marker number 16 is a filled circle (see ?pch), which we want to keep for two of the markers, but we use NA to remove the point marker for the first item in the legend.

Upvotes: 5

Related Questions