r - 如何以编程方式告诉 ggplot 有多少个方面?

标签 r ggplot2

下面是代码和图表。

该图具有三个方面。在哪里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

faceted plot

最佳答案

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

out <- ggplot_build(the_plot)

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

关于r - 如何以编程方式告诉 ggplot 有多少个方面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61346939/

相关文章:

消除 ggplot y 轴和第一个 x 值之间的差距

r - 在 R/ggplot2 中绘制多个图形并保存结果

r - facet_wrap的百分比直方图

删除 R 中的重复行,即使顺序不同

R ggplot2 : Title and legend in one line

在 data.frame 中将每行选择变量复制 n 次

python - 相当于Python中ggplot2的rpy2中的scale_size_area?

r - 非标准评估、lapply 和 ggplot

r - 使用 R 中的 quarto_render 渲染 qmd 文件的两个问题

r - 如何并行化具有多个参数的函数?