how to replace legend 'bullet' of geom_text guide (legend)
ggplot2, r
Solution
ggplot(majdf, aes(x = val)) +
geom_point(data = majtxt, aes(x = geq, colour = species),
y = 0.2, size = 0) +
geom_density() +
geom_vline(data = majtxt, aes(xintercept = geq)) +
geom_text(data = majtxt, aes(x = geq, y = 0.2, label = geq, color = species),
angle = 90, show_guide = FALSE) +
facet_wrap(~ lvl) +
scale_colour_discrete(guide=guide_legend(override.aes=list(size=4)))
How this works: Add a point geom with the appropriate colour mapping. This will add a point into the legend. But, to keep it from showing up on the the plot, set the size of the point to 0. In the text geom, tell it not to add that part (the rotated a) to the legend (`show_guide = FALSE`). Finally, the legend will have just the point that you want and not the sideways a; unfortunately, it is drawn at the same size as in the plot, namely 0. So using the `override.aes` argument to `guide_legend` (which is passed to `guide` in `scale_colour_discrete`), set the size of the point to something "big".
This approach does not require pulling apart pieces to two different plots and stitching them back together.
An alternative way of specifying the guide parameters is using the `guides` function instead of passing it as an argument to `scale_colour_manual`:
ggplot(majdf, aes(x = val)) +
geom_point(data = majtxt, aes(x = geq, colour = species),
y = 0.2, size = 0) +
geom_density() +
geom_vline(data = majtxt, aes(xintercept = geq)) +
geom_text(data = majtxt, aes(x = geq, y = 0.2, label = geq, color = species),
angle = 90, show_guide = FALSE) +
facet_wrap(~ lvl) +
guides(colour = guide_legend(override.aes=list(size=4)))
The resulting graphic is the same.
Problem
I would like to replace the 'bullets' in legend (guide) of `geom_text`. Now it's a tilted `a`, but I would like a big fat circle or a square or any other shape that will emphasize the color (more). ``` library(ggplot2) majdf <- data.frame(lvl = rep(c("A", "B"), each = 50), val = c(rnorm(50, 1), rnorm(50, 3))) majtxt <- data.frame(species = c("sp1", "sp2", "sp3"), geq = c(0.01, 2, 2.2)) ggplot(majdf, aes(x = val)) + geom_density() + geom_vline(data = majtxt, aes(xintercept = geq)) + geom_text(data = majtxt, aes(x = geq, y = 0.2, label = geq, color = species), angle = 90) + facet_wrap(~ lvl) ```