I want to draw a custom legend in ggplot2. I tried, as suggested in other questions here on a similar topic, scale_colour_manual
, guide=legend
, etc., yet the default legend is immovable so far. The default legend ignores two plots, and needlessly draws size levels for the third plot. I would want a legend like that:
[light gray line] graph 1
[black circle of size=1] graph 2
[gray rectangle of size=1] graph 3
Here is the code, with example data frames. Custom legend modifications, like these from this question, appear to do nothing.
cloud2 <- data.frame(x = c(0, 1, 2), y = c(0.3, 0.4, 0.5))
sandwich3 <- data.frame(x = c(1, 2, 3), y = c(0.4, 0.5, 0.6), p = c(0.1, 0.6, 0.3))
sandwich4 <- data.frame(x = c(3, 4, 5), y = c(0.6, 0.3, 0.5), p = c(0.1, 0.7, 0.2))
ggplot(cloud2, aes(x=x, y=y)) + geom_line(size=0.1,colour="gray70") +
aes(x=x, y=y, size=sqrt(p)) +
geom_point(data=sandwich3,colour="gray40",shape=15) + scale_size(range=c(0,2)) +
geom_point(data=sandwich4,colour="black",shape=16) +
theme(legend.position=c(0.905, 0.14), legend.title=element_blank(),
axis.ticks = element_line(colour = "black"), axis.text = element_text(colour = "black")) +
scale_x_continuous(limits = c(-5, 5), breaks=c(-2, 0, 2)) +
scale_y_continuous(limits = c(-1.1, 1.2))
The resulting plot, using the original data frames: