隨着gtable
版本0.2.0(ggplot2
v 2.1.0),Kohske的原始解決方案(請參閱評論)可以開始工作。
# 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")
或者,一個GROB對象上工作:
# 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)
另一個選項
修改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
不幸的是沒有官方的方法。快速入門:'grid.gedit(「^ key - [ - 0-9] + $」,label =「N」)' – kohske
非常感謝。它像一個魅力。 –
無論是你還是@kohske都應該將其作爲答案發布,以便你可以接受它,表明它解決了你的問題。 – joran