How to mix different characters for key keys in ggplot2?
I have the following code that creates a drawing.
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
How can I change the character to a line without a dot in the legend for "C2P" only, without affecting the characters for "P2P" and "CP2P"?
source to share
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 graph. In this case, we want to change the legend point markers, so we want to change the shape
points. Point marker 16 is a filled circle (see ?pch
) that we want to keep for the two markers, but we use NA
to remove the point marker for the first item in the legend.
source to share