使用 magrittr tee %T>% 运算符会产生错误
using the magrittr tee %T>% operator creates error
我正在尝试创建两个图表:一个是整个数据集,另一个是按 "site" 分组因子拆分时的平均图。
这是源数据:
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))
这是我当前的代码:
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)
但我收到此错误消息:
"Error in as.vector(x, mode) : cannot coerce type 'environment' to vector of type '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)
有什么建议吗?谢谢
在这种情况下,操作顺序很重要。 %%
运算符比 +
绑定得更紧密。所以当你说
site.data %T>% ggplot(aes(year, peak, color = site)) + geom_line()
与
相同
( site.data %T>% ggplot(aes(year, peak, color = site)) ) + geom_line()
这基本上是在做
site.data + geom_line()
returns 你得到了同样的错误。您需要将所有 ggplot 图层 additions/modifications 显式分组到一个代码块中。尝试
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)}
我正在尝试创建两个图表:一个是整个数据集,另一个是按 "site" 分组因子拆分时的平均图。
这是源数据:
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))
这是我当前的代码:
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)
但我收到此错误消息: "Error in as.vector(x, mode) : cannot coerce type 'environment' to vector of type '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)
有什么建议吗?谢谢
在这种情况下,操作顺序很重要。 %%
运算符比 +
绑定得更紧密。所以当你说
site.data %T>% ggplot(aes(year, peak, color = site)) + geom_line()
与
相同( site.data %T>% ggplot(aes(year, peak, color = site)) ) + geom_line()
这基本上是在做
site.data + geom_line()
returns 你得到了同样的错误。您需要将所有 ggplot 图层 additions/modifications 显式分组到一个代码块中。尝试
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)}