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

wus*_*978 49 r ggplot2

我该如何更改图例中符号的大小?我检查了文件,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)
Run Code Online (Sandbox Code Playgroud)

Mar*_*ius 60

您可以使用以下override.aes参数手动进行这些更改guide_legend():

g <- g + guides(shape = guide_legend(override.aes = list(size = 5)))
print(g)
Run Code Online (Sandbox Code Playgroud)


小智 46

你应该使用:

theme(legend.key.size = unit(3,"line"))
Run Code Online (Sandbox Code Playgroud)

  • 此代码增加了符号周围的区域,而不是符号本身.指南代码更合适. (20认同)
  • 恕我直言,这是更合适的答案,因为它使用了问题中提到的"主题". (3认同)
  • 这对我有用。然而,接受的答案却没有。 (3认同)
  • 但这实际上是实现我们大多数人想要执行的操作的代码... (2认同)

onl*_*tom 21

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

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

g + guides(color = guide_legend(override.aes = list(size=5)))
Run Code Online (Sandbox Code Playgroud)

  • 我认为它不适合你的原因是因为你的情节中有一个色标而不是形状比例.Marius的答案改变了形状图例中形状的大小.您的代码行会更改颜色比例的图例形状大小 (5认同)

fil*_*s21 9

如果您想独立更改图例的 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')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

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

在此处输入图片说明

# 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')
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明


小智 9

截至 2022 年 12 月 1 日,在 ggplot2 版本 3.4.0 中,参数:

guides(shape = guide_legend(override.aes = list(size = 5)))
Run Code Online (Sandbox Code Playgroud)

不再有效......相反,替换"size = 5""linewidth = 5"