更改ggplot2中图例键中的符号 [英] Changing the symbol in the legend key in ggplot2

查看:708
本文介绍了更改ggplot2中图例键中的符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何更改geom_text图例关键字符号?在下面的例子中,我想将图例中的符号从小写字母a改为大写字母N。我已经看过一个例子来做类似的东西,但无法让这个例子工作。

 #一些玩具数据
df< - 展开.grid(x = factor(seq(1:5)),y = factor(seq(1:5)),KEEP.OUT.ATTRS = FALSE)
df $ Count = seq(1:25)

#示例图
library(ggplot2)
ggplot(data = df,aes(x = x,y = y,label = Count,size = Count))+
geom_text()+
scale_size(range = c(2,10))

解决方案

使用 gtable 版本0.2.0( ggplot2 v 2.1.0),Kohske的原始解决方案(见评论)可以开展工作。

 #一些玩具数据
df < - expand.grid(x = factor(seq(1:5) ),y = factor(seq(1:5)),KEEP.OUT.ATTRS = FALSE)
df $ Count = seq(1:25)

#加载软件包
图书馆(ggplot2)
图书馆(网格)

#绘图
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对象上工作:

 #获取ggplot grob 
gp = ggplotGrob(p)
grid.ls(grid.force(gp))

#编辑grob
gp = editGrob(grid.force(gp),gPath key- [1-9] -1-1),grep = TRUE,global = TRUE,
label =N)

#绘制
网格。 newpage()
grid.draw(gp)

另一个选项



修改geom

 #一些玩具数据
D f < - expand.grid(x = factor(seq(1:5)),y = factor(seq(1:5)),KEEP.OUT.ATTRS = FALSE)
df $ Count = seq 1:25)

#加载软件包
库(ggplot2)
库(网格)

#绘图
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< - 函数(数据,参数,大小){
pointsGrob(0.5,0.5,pch =N,
gp = gpar(col = alpha (data $ color,data $ alpha),
fontsize = data $ size * .pt))}

p


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

这篇关于更改ggplot2中图例键中的符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆