【问题标题】:Changing the symbol in the legend key in ggplot2在ggplot2中更改图例键中的符号
【发布时间】:2012-05-01 23:38:22
【问题描述】:

如何更改 geom_text 图例键符号?在下面的示例中,我想将图例键中的符号从小写“a”更改为大写“N”。我查看了执行 something similar here 的示例,但无法使该示例正常工作。

# 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))

【问题讨论】:

  • 遗憾的是没有官方的方法。快速破解是:grid.gedit("^key-[-0-9]+$", label = "N")
  • 非常感谢。它就像一个魅力。
  • 您或@kohske 都应该将其发布为答案,以便您接受它,表明它解决了您的问题。

标签: r ggplot2 legend


【解决方案1】:

编辑:更新 ggplot 版本 0.9.2

最初的答案(见下文)在大约 0.9.0 或 0.9.1 版本中中断。以下适用于 0.9.2

# 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)

# A plot
library(ggplot2)
p = ggplot(data = df, aes( x = x, y = y, label = Count, size = Count)) + 
   geom_point(colour = NA) +
   geom_text(show.legend = FALSE) +  
   guides(size = guide_legend(override.aes = list(colour = "black", shape = utf8ToInt("N")))) +
   scale_size(range = c(2, 10))

p

原答案 回答我自己的问题并在上面@kohske 的评论中使用代码的 sn-p:

# 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)

# A plot
library(ggplot2)
p = ggplot(data = df, aes( x = x, y = y, label = Count, size = Count)) + 
    geom_text() +
    scale_size(range = c(2, 10))
p

library(grid)
grid.gedit("^key-[-0-9]+$", label = "N")

【讨论】:

    【解决方案2】:

    安装了gtable 0.2.0 版(ggplot2 v 2.1.0)后,可以使 Kohske 的原始解决方案(请参阅 cmets)工作。

    # 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)
    

    另一种选择

    修改几何

    # 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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-27
      • 1970-01-01
      • 2021-01-09
      • 2012-12-09
      • 2011-11-11
      • 1970-01-01
      • 1970-01-01
      • 2022-01-24
      相关资源
      最近更新 更多