使用magrittr tee%T>%运算符会产生错误

hac*_*ckR 4 r ggplot2 dplyr

我正在尝试创建两个图形:整个数据集之一,以及按"站点"分组因子分割时的平均图形.

这是源数据:

site.data <- structure(list(site = structure(c(1L, 1L, 1L, 1L,1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L), 
.Label = c("ALBEN", "ALDER", "AMERI"), class = "factor"),  
year = c(5L, 10L, 20L, 50L, 100L, 200L, 500L, 5L, 10L, 20L, 50L, 100L, 200L, 500L, 5L, 10L, 20L, 50L, 100L, 200L),
peak = c(101529.6, 117483.4, 132960.9, 153251.2, 168647.8, 184153.6, 204866.5, 6561.3, 7897.1, 9208.1, 10949.3,12287.6, 13650.2, 15493.6, 43656.5, 51475.3, 58854.4, 68233.3, 75135.9, 81908.3)),
.Names = c("site", "year","peak"), class = "data.frame", row.names = c(NA, -20L))  
Run Code Online (Sandbox Code Playgroud)

这是我目前的代码:

library(ggplot2)
library(dplyr)
library(magrittr)

site.data %T>%     

           # then use a tee operator to plot the graph               
ggplot(aes(year, peak, color = site)) + geom_line() + geom_point(size = 6)   %>%

           # then group by the site  
group_by(site) %>%  

           # and finally create a graph of the mean values
summarize(mean = mean(peak)) %>%
ggplot(aes(site, mean, color = site)) + geom_point(size = 6)
Run Code Online (Sandbox Code Playgroud)

但我收到此错误消息:"as.vector(x,mode)中的错误:无法强制将'environment'强制类型为'any'类型的向量

现在,如果我用常规magrittr管道操作符替换tee操作符并注释掉第一个ggplot行,那么至少我得到第二个ggplot,如下所示:

site.data %>%                                 
   #   ggplot(aes(year, peak, color = site)) + geom_line() + geom_point(size = 6)   %>%
group_by(site) %>%
summarize(mean = mean(peak)) %>%
ggplot(aes(site, mean, color = site)) + geom_point(size = 6)
Run Code Online (Sandbox Code Playgroud)

有什么建议?谢谢

MrF*_*ick 7

这是操作顺序很重要的情况.该%%运营商绑定更紧密比+呢.所以当你说

site.data %T>% ggplot(aes(year, peak, color = site)) + geom_line() 
Run Code Online (Sandbox Code Playgroud)

那是一样的

( site.data %T>% ggplot(aes(year, peak, color = site)) ) + geom_line() 
Run Code Online (Sandbox Code Playgroud)

这基本上是做的

site.data + geom_line()
Run Code Online (Sandbox Code Playgroud)

返回相同的错误.您需要将所有ggplot图层添加/修改显式分组到代码块中.尝试

site.data %T>%     
{print(ggplot(., aes(year, peak, color = site)) + geom_line() + geom_point(size = 6))}  %>%
group_by(site) %>%  
summarize(mean = mean(peak)) %>%
{ggplot(., aes(site, mean, color = site)) + geom_point(size = 6)}
Run Code Online (Sandbox Code Playgroud)