r - 如何以编程方式判断 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
解决方案
您可以使用 gg_build() - 函数访问 ggplot 数据
out <- ggplot_build(the_plot)
length(levels(out$data[[1]]$PANEL))
[1] 3
推荐阅读
- javascript - JVectorMap 错误:
属性变换:预期数字,“缩放(NaN)翻译(N ...” - r - 在 R 中,获取一个日期字符串并将其转换为日期时间格式(使用 lubridate)
- javascript - 我们可以在没有事件处理程序的情况下模拟按键吗?
- scala - 为什么 import cat.implicits._ 不再需要导入类型类实例?
- webpack - 如何使用 webpack 和 babel 生成简约的 js 包
- python - 如何使用 Python 请求库发送 cookie(标头)
- methods - F# 方法上的骑手断点
- python - 我如何将此列表拆分为对或单个元素。有没有更简单的方法来做到这一点?
- swift - 用户默认不保存,不检索数据
- node.js - 如何将列表与嵌套的 foreach 一起使用?