从现有的ggplot图表中删除geom? [英] Remove geom(s) from an existing ggplot chart?

查看:112
本文介绍了从现有的ggplot图表中删除geom?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图了解如何更改ggplot2图表的内部.我开始阅读一些可以找到的关于ggplot_builtggplot_gtable的资源,但是我无法回答以下问题.

I am trying to understand how I can make changes to the internals of a ggplot2 chart. I started reading the few ressources I could find about ggplot_built and ggplot_gtable, but I could not answer the question below.

给出具有2个geom的情节g.

g <- ggplot(iris, aes(Petal.Length, Petal.Width)) + 
  geom_point() + 
  geom_text(aes(label=Sepal.Width))
g

有没有办法深入到g对象并删除一个/多个几何?

Is there a way to dive into the g object and remove one/multiple geoms?

我可以从g开始获得没有几何图形的地块吗?

Can I get, starting from g, a plot with no geoms?

还是只删除了geom_text?

我刚刚注意到,删除几何图形可能会对其他几何图形的布局产生影响.作为默认值和大多数用例的预期行为,它可能很棒,但是我实际上需要完全相同的图表布局"(剩余几何图形的轴和位置).

I just noticed that removing geoms can have an impact on the layout of the other geoms. Probably great as a default and the intended behaviour for most use cases, but I actually need the exact same chart "layout" (axis and positions of remaining geoms).

例如,在删除一个几何图形之前:

Example, before removing one geom:

library(dplyr)
library(ggplot2)
count(mpg, class) %>%
  mutate(pct=n/sum(n)) %>%
  ggplot(aes(class, pct)) +
  geom_col(fill="blue") +
  geom_line(group=1) +
  geom_point(size=4) 

在删除一个几何图形之后(请注意,y轴不再从0开始,我猜不带线的线/点的默认行为):

after removing one geom (note that the y axis doesn't start at 0 anymore, I guess the default behaviour for line/point without bars):

library(dplyr)
library(ggplot2)
count(mpg, class) %>%
  mutate(pct=n/sum(n)) %>%
  ggplot(aes(class, pct)) +
  geom_col(fill="blue") +
  geom_line(group=1) +
  geom_point(size=4) -> p
p$layers[[1]] <- NULL
p

有什么方法可以强制ggplot保持完全相同的布局?

Any ways to force ggplot to keep the exact same layout?

推荐答案

您可以像处理其他R对象一样访问/操作g的元素.

You can access / manipulate g's elements the way you would do with other R object.

g$layers
#[[1]]
#geom_point: na.rm = FALSE
#stat_identity: na.rm = FALSE
#position_identity 

#[[2]]
#mapping: label = Sepal.Width 
#geom_text: parse = FALSE, check_overlap = FALSE, na.rm = FALSE
#stat_identity: na.rm = FALSE
#position_identity 

删除geom_text:

g$layers[[2]] <- NULL

删除所有layers

g$layers <- NULL
g

gginnards软件包提供了一些操作ggplot图层的功能,请参见小插图

The gginnards package offers some functions to manipulate ggplot layers, see the vignette User Guide: 4 Manipulation of plot layers for details.

修改

以下问题我如何提取ggplot2对象的绘图轴范围?我来到了使用ggplot_buildggplot_gtable的解决方案.这个想法只是将从ggplot_built(p)中获取的布局参数复制到新的绘图中,为此我们删除了一个图层.

Following the question of How can I extract plot axes' ranges for a ggplot2 object? I came to a solution that uses ggplot_build and ggplot_gtable. The idea is simply to copy the layout parameters taken from ggplot_built(p) to the new plot, for which we deleted a layer.

# create a copy of p
p_new <- p

# delete the first layer
p_new$layers[[1]] <- NULL
p_new_build <- ggplot_build(p_new)

# this is the important line
p_new_build$layout$panel_params <- ggplot_build(p)$layout$panel_params

library(gridExtra)
grid.arrange(p, ggplot_gtable(p_new_build), ncol = 2)

这篇关于从现有的ggplot图表中删除geom?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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