首页 > 解决方案 > 如何以编程方式判断 ggplot 有多少个方面?

问题描述

下面是代码和图表。

该图具有三个方面。我在哪里the_plot可以找到它具有三个方面?是的,我可以从mtcars数据框中得到它,或者the_plot$data,但我不想重新创建数据分析。相反,我想检查 的图形元素the_plot,因此我不必在多个地方重复应用程序逻辑。 the_plot$facet没有显示任何我认识的东西,其他情节变量也没有。

我正在使用 tidyverse 1.3.0。

library(tidyverse)
data(mtcars)
the_plot<-ggplot(mtcars, aes(mpg, disp, group=cyl)) + facet_wrap(~cyl) + geom_point()
the_plot

刻面图

标签: rggplot2

解决方案


您可以使用 gg_build() - 函数访问 ggplot 数据

out <- ggplot_build(the_plot)

length(levels(out$data[[1]]$PANEL))
[1] 3


推荐阅读