ggplot2可以在一个图例中分别控制点的大小和线的大小(线宽)吗? [英] Can ggplot2 control point size and line size (lineweight) separately in one legend?
问题描述
一个使用 ggplot2
来绘制连接每个组的平均值的数据点和线组的示例,使用相同的 aes
shape 和 linetype
:
<$ p (大小= 2)+
>
p < - ggplot(mtcars,aes(gear,mpg,shape = factor(cyl),linetype = factor(cyl))+
+
stat_summary(fun.y = mean,geom =line,size = 1)+
scale_shape_manual(values = c(1,4,19))
问题是相对于线符号而言,图例中的点符号显得有点太小而无法看到:
试图放大图例中的磅值也增大了线宽,所以在这里没有用。
p1 < - p + guides(shape = guide_legend(override.aes = list(size = 4)))
如果线宽是独特的美学来自 size
。
我试着添加
+ guides(linetype = guide_legend(override.aes = list(size = 1)))
这只是一个警告。
>警告消息:
在guide_merge.legend(init,x [[i]])中:忽略重复的override.aes。
如果我移动 linetype
出 stat_summary()
。如果我只想要点符号,我可以从图例这种方式消除线。
p2 < - p + guides(shape = guide_legend(override.aes = list(size = 4,linetype = 0)))
p>
相反,(在图形本身中保留小点符号)我想要一个单独的图例与两个大点符号在最后一张图像和细线符号中,如第一张图像。有没有办法做到这一点?
确实似乎很难独立设置这些属性。我只是能够拿出一个黑客。如果您的实际数据差别很大,则可能需要进行调整。但是我所做的是使用 override.aes
来设置点的大小。然后,我进入并构建了绘图,然后手动更改实际低级网格对象中的线宽设置。这里是代码
pp <-ggplot(mtcars,aes(gear,mpg,shape = factor(cyl),linetype = factor (cyl)))+
geom_point(size = 3)+
stat_summary(fun.y = mean,geom =line,size = 1)+
scale_shape_manual(values = c( ($)
guides(shape = guide_legend(override.aes = list(size = 5)))
build< - ggplot_build(pp)
gt < - ggplot_gtable(build)
segs< - grepl(geom_path.segments,sapply(gt $ grobs [[8]] [[1]] [[1]] $ grobs ,'[[','name'))
gt $ grobs [[8]] [[1]] [[1]] $ grobs [segs]< -lapply(gt $ grobs [[8] ($ x $ gp $ lwd< -2; x})
grid.draw(gt)$ b($)$ [$ 1] $ b
魔数8是 gt $ grobs [[8]] $ name ==guide-box
所以我知道我在做传奇。我并不是最好的网格图形和gtables,所以也许有人可能会提出一个更优雅的方式。
An example using ggplot2
to graph groups of data points and lines connecting the means for each group, mapped with the same aes
for shape
and for linetype
:
p <- ggplot(mtcars, aes(gear, mpg, shape = factor(cyl), linetype = factor(cyl))) +
geom_point(size = 2) +
stat_summary(fun.y = mean, geom = "line", size = 1) +
scale_shape_manual(values = c(1, 4, 19))
Problem is that point symbols in the legend appear a bit too small to see, relative to the line symbols:
Trying to enlarge point size in legend also enlarges lineweight, so that is not useful here.
p1 <- p + guides(shape = guide_legend(override.aes = list(size = 4)))
It would be nice if lineweight were a distinct aesthetic from size
.
I tried adding
+ guides(linetype = guide_legend(override.aes = list(size = 1)))
which just gives a warning.
> Warning message:
In guide_merge.legend(init, x[[i]]) : Duplicated override.aes is ignored.
It seems to make no difference either if I move the linetype
aes
out of ggplot()
and into stat_summary()
. If I wanted only the point symbols, I could eliminate lines from the legend this way.
p2 <- p + guides(shape = guide_legend(override.aes = list(size = 4, linetype = 0)))
Instead, (keeping small point symbols in the graph itself) I want one single legend with both big point symbols as in this last image and thin line symbols as in the first image. Is there a way to do this?
It sure does seem to be difficult to set those properties independently. I was only kind of able to come up with a hack. If your real data is much different it will likely have to be adjusted. But what i did was used the override.aes
to set the size of the point. Then I went in and built the plot, and then manually changed the line width settings in the actual low-level grid objects. Here's the code
pp<-ggplot(mtcars, aes(gear, mpg, shape = factor(cyl), linetype = factor(cyl))) +
geom_point(size = 3) +
stat_summary(fun.y = mean, geom = "line", size = 1) +
scale_shape_manual(values = c(1, 4, 19)) +
guides(shape=guide_legend(override.aes=list(size=5)))
build <- ggplot_build(pp)
gt <- ggplot_gtable(build)
segs <- grepl("geom_path.segments", sapply(gt$grobs[[8]][[1]][[1]]$grobs, '[[', "name"))
gt$grobs[[8]][[1]][[1]]$grobs[segs]<-lapply(gt$grobs[[8]][[1]][[1]]$grobs[segs],
function(x) {x$gp$lwd<-2; x})
grid.draw(gt)
The magic number "8" was where gt$grobs[[8]]$name=="guide-box"
so i knew I was working the legend. I'm not the best with grid graphics and gtables yet, so perhaps someone might be able to suggest a more elegant way.
这篇关于ggplot2可以在一个图例中分别控制点的大小和线的大小(线宽)吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!