How do I change the geom_text legend key symbol? In the example below, I'd like to change the symbol in the legend key from a lower case "a" to, say, an upper case "N". I've looked at an example for doing something similar here, but couldn't get that example to work.
# Some toy data
df <- expand.grid(x = factor(seq(1:5)), y = factor(seq(1:5)), KEEP.OUT.ATTRS = FALSE)
df$Count = seq(1:25)
# An example plot
library(ggplot2)
ggplot(data = df, aes( x = x, y = y, label = Count, size = Count)) +
geom_text() +
scale_size(range = c(2, 10))
With gtable
version 0.2.0 (ggplot2
v 2.1.0) installed, Kohske's original solution (see the comments) can be made to work.
# Some toy data
df <- expand.grid(x = factor(seq(1:5)), y = factor(seq(1:5)), KEEP.OUT.ATTRS = FALSE)
df$Count = seq(1:25)
# Load packages
library(ggplot2)
library(grid)
# A plot
p = ggplot(data = df, aes( x = x, y = y, label = Count, size = Count)) +
geom_text() +
scale_size(range = c(2, 10))
p
grid.ls(grid.force())
grid.gedit("key-[-0-9]-1-1", label = "N")
Or, to work on a grob object:
# Get the ggplot grob
gp = ggplotGrob(p)
grid.ls(grid.force(gp))
# Edit the grob
gp = editGrob(grid.force(gp), gPath("key-[1-9]-1-1"), grep = TRUE, global = TRUE,
label = "N")
# Draw it
grid.newpage()
grid.draw(gp)
Another option
Modify the geom
# Some toy data
df <- expand.grid(x = factor(seq(1:5)), y = factor(seq(1:5)), KEEP.OUT.ATTRS = FALSE)
df$Count = seq(1:25)
# Load packages
library(ggplot2)
library(grid)
# A plot
p = ggplot(data = df, aes( x = x, y = y, label = Count, size = Count)) +
geom_text() +
scale_size(range = c(2, 10))
p
GeomText$draw_key <- function (data, params, size) {
pointsGrob(0.5, 0.5, pch = "N",
gp = gpar(col = alpha(data$colour, data$alpha),
fontsize = data$size * .pt)) }
p
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With