ggplot2:调整图例中符号的大小

96

我该如何改变图例中符号的大小?我查看了 theme 的文档,但没有找到答案。

这是一个例子:

library(ggplot2);library(grid)
set.seed(1000)
x <- 1:6
mu <- sin(x)
observed <- mu + rnorm(length(x), 0, 0.5*sd(mu))
data <- data.frame(
  t=rep(x, 2), 
  value=c(mu, observed) - min(mu, observed) + 0.5, 
  class = rep(c("mu", "observed"), each=length(x)))
mu <- data$value[1:length(x)]
observed <- data$value[1:length(x) + length(x)]
mu.min <- mu - 3 * 0.5 * sd(mu)
mu.max <- mu + 3 * 0.5 * sd(mu)
g <- ggplot(data=data)
g <- g + geom_point(aes(x=value, y=t, shape=class, size=24)) + scale_size(guide="none")
g <- g + scale_shape(name="", labels=expression(paste(S[u](t), ", the observation at time ", t), paste(mu[u](t), ", the mean of ", tilde(S)[u](t), "          ")))
stat_function.color <- gray(0.5)
g <- g + geom_segment(aes(y=1:6, yend=1:6, x=mu.min, xend=mu.max, linetype="2", alpha = 1), color=stat_function.color) + scale_alpha(guide="none") + scale_linetype(name= "", labels=expression(paste("probability density function (pdf) of ", tilde(S)[u], " at time ", t)))
for(i in 1:length(x)) {
  g <- g + stat_function(fun=function(x, i) {
    ifelse( x <= mu.max[i] & x >= mu.min[i], dnorm(x, mu[i], sd(mu)) + i, NA)
    }, color=stat_function.color, args=list(i=i))
}
background.color <- gray(0.75)
g <- g + theme(
  axis.text=element_blank(),
  title=element_text(size=rel(1.5)),
  legend.text=element_text(size=rel(1.5)),
  legend.position="top",
  legend.direction="vertical",
#   legend.key.size = unit(2, "cm"),
  panel.background=element_rect(fill=background.color), 
  panel.grid.major=element_line(color=background.color),
  panel.grid.minor=element_line(color=background.color)
  ) + coord_flip()
plot(g)

2
控制ggplot2图例外观而不影响图形 - PatrickT
5个回答

115

你应该使用:

theme(legend.key.size = unit(3,"line"))

5
在我看来,这个答案更为恰当,因为它使用了在问题中提到的“主题”。 - Josef Eisl
54
这段代码增加了符号周围的区域,而不是符号本身。指南中的代码更加适合。 - LListhewaytobe
8
但这是实现我们大多数人来到这个页面寻找的功能的代码。 - theforestecologist
3
这对我有用,而被接受的答案则没有。 - jdmartin86
1
注意:在试图减小图例键的大小时,我发现了两件事。1)legend.key.size同时改变X和Y维度的键。如果你想要一个矩形而不是正方形,你需要使用legend.key.heightlegend.key.width。2)如果你将legend.key.size设置为小于字体大小,它似乎会将高度设置为字体大小,宽度设置为你设置的任何值。@PatrickT如果你想要比1行更小的东西,请尝试使用unit(1, "pt") - ESELIA
显示剩余3条评论

104
您可以使用override.aes参数手动进行此类更改,以在guide_legend()中实现:

g <- g + guides(shape = guide_legend(override.aes = list(size = 5)))
print(g)

@Ibo:你确定吗?你的示例代码看起来只适用于在图例中具有颜色比例尺的情况,而我的示例是针对形状比例尺,就像原始问题中一样。据我所知,如果您正在修改形状比例尺,则仍然可以使用此方法。但是,此时我更喜欢Duc_Hokie的建议,即使用主题选项。 - Marius

59

对我来说,Marius的答案在R版本3.2.2上不起作用。您仍然可以使用相同的override.aes参数调用guide_legend(),但需要在包装函数中指定color而不是shape

因此,如果您正在运行更高版本的R,请尝试使用以下方法:

g + guides(color = guide_legend(override.aes = list(size=5)))

编辑

正如评论中@Ibo所指出的那样,这可能是由于ggplot对象中的颜色比例尺。如果ggplot对象包含颜色比例尺,则大小映射 (size=5) 必须设置在颜色上。


6
我认为你的问题出在图表上使用了颜色比例尺而非形状比例尺。Marius 的回答更改了形状比例尺图例中形状的大小。而你的代码行更改了颜色比例尺图例中形状的大小。 - Ibo

13
如果您想要独立地更改图例中 2 个组件的大小,则会变得更加棘手,但可以通过使用 grid 包手动编辑绘图的各个组件来完成。
基于此 SO 答案的示例:
set.seed(1)
dat <- data.frame(x = runif(n = 100),
                  x2 = factor(rep(c('first', 'second'), each = 50)))
set.seed(1)
dat$y = 5 + 1.8 * as.numeric(dat$x2) + .3 * dat$x + rnorm(100)

# basic plot
g <- ggplot(data = dat,
       aes(x = x, y = y, color = x2))+
   theme_bw()+
   geom_point()+
   geom_smooth(method = 'lm')

在此输入图片描述

# make the size of the points & lines in the legend larger
g + guides(color = guide_legend(override.aes = list(size = 2)))

输入图像描述

# Make JUST the legend points larger without changing the size of the legend lines:
# To get a list of the names of all the grobs in the ggplot
g
grid::grid.ls(grid::grid.force())

# Set the size of the point in the legend to 2 mm
grid::grid.gedit("key-[-0-9]-1-1", size = unit(4, "mm"))

# save the modified plot to an object
g2 <- grid::grid.grab()
ggsave(g2, filename = 'g2.png')

输入图像描述


9
截至2022年12月1日,在ggplot2版本3.4.0中,参数如下:
guides(shape = guide_legend(override.aes = list(size = 5)))

不再起作用...相反,将"size = 5"替换为"linewidth = 5"


没错,它确实不再起作用了,我很困惑为什么运行一些旧脚本会导致图例符号变小。非常感谢。 - Alireza Amani
没错,它不再起作用了,我很困惑为什么运行一些旧脚本会导致图例符号变小。非常感谢。 - Alireza Amani
1
在geom_line()中,size已被弃用,应使用linewidth。尽管在geom_line中仍然可以使用size,但正如Andrew所指出的那样,在legend参数中不会生成期望的结果。请参见https://www.tidyverse.org/blog/2022/08/ggplot2-3-4-0-size-to-linewidth/。 - Dan Tarr

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接