我经常遇到这样的场景,不同的几何对象需要看到提供给初始调用ggplot()
的 data
参数的子集。
通常,这发生在长链的可视化之后,在此期间,我实际上不需要保存在我的工作区中的数据,但我的当前解决方法是这样做,然后在每个几何显式地引用它,例如:
(
#...
# long chain
# ...
) -> to_plot
(
ggplot()
+ geom_ribbon(
data = (
to_plot #using intermediate object saved to workspace
%>% dplyr::filter(
cells_or_boots=='bootss'
)
)
, mapping = aes(
x = x
, ymin = lo
, ymax = hi
)
)
+ geom_line(
data = (
to_plot #using intermediate object saved to workspace
%>% dplyr::filter(
cells_or_boots=='cells'
)
)
, mapping = aes(
x = x
, y = mean
)
)
)
是否有类似于 dplyr::cur_data()
的东西可以用于 ggplot2,以实现类似以下的功能:
(
#...
# long chain
# ...
%>% ggplot()
+ geom_ribbon(
data = (
ggplot_cur_data() #place for hoped-for dplyr::cur_data() equivalent
%>% dplyr::filter(
cells_or_boots=='boots'
)
)
, mapping = aes(
x = x
, ymin = lo
, ymax = hi
)
)
+ geom_line(
data = (
ggplot_cur_data() #place for hoped-for dplyr::cur_data() equivalent
%>% dplyr::filter(
cells_or_boots=='cells'
)
)
, mapping = aes(
x = x
, y = mean
)
)
)
?
( dat %>% (function(x){print(x); return(x)}) %>% more_operations() ... )
,其中链中的第一个函数是一个匿名函数。) - undefined.
的一个有趣的必要条件是,你不能只写成data = .
,而是必须在管道中使用data = (. %>% identity())
。当然,如果我们对数据不做任何操作,我们应该直接省略data =
,但我想提一下这个问题,因为我遇到过这种情况,有一段时间感到困惑。 - undefined