如何更改ggplot2中特定geom的大小? [英] How to change size from specific geom in ggplot2?

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

问题描述

我有一个包含2个层geom_pointgeom_line的ggplot,如下所示.

I have a ggplot containing 2 layers geom_point and geom_line as shown below.

gp <- ggplot(data = mtcars , aes(x = disp , y = hp)) + 
       geom_point(size = 3) + geom_line(size = 1 , color = "red")

`

创建图后,在gp内,我只想更改一个geom的大小(例如,线).我该怎么办?

Once the plot created, inside the gp, I want to change the size of only one geom (e.g line). How can I do that ?

推荐答案

如果您希望将现有尺寸替换为较小的尺寸(或将实线类型替换为虚线,将实心线型替换为未填充的线型,等等. ),则重叠效果可能无法达到最佳的视觉效果.或者,您可以深入研究已创建的ggplot对象的特定层,&在那里手动更改参数.

If you wish to replace the existing size with a smaller one (or replace a solid linetype with a dashed one, a filled shape with an unfilled one, etc.), overlaying may not have the best visual effect. As an alternative, you can dig into the specific layer of the ggplot object you've created, & manually change the parameters there.

(请注意,这要求您了解对象中几何图层的顺序.)

(Note that this requires you to know the order of geom layers in the object.)

gp$layers[[2]]$aes_params$size <- 0.5 # change the size of the geom_line layer from 1 to 0.5
gp$layers[[1]]$aes_params$size <- 1   # change the size of the geom_point layer from 3 to 1

我假设您的用例涉及修改某个程序包的绘图功能输出的ggplot对象?否则,在绘图创建时指定所需的参数可能会更简单...

I assume your use case involves modifying a ggplot object outputted by some package's plotting function? Otherwise, it's probably simpler to specify the desired parameters at the point of plot creation...

这篇关于如何更改ggplot2中特定geom的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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