我正在尝试创建两个图形:整个数据集中的一个,以及由"网站"分割时的平均图形。分组因素。
以下是源数据:
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)
但是我收到此错误消息: &#34; as.vector(x,mode)出错:无法强制类型&#39;环境&#39;矢量类型&#39;任何&#39; &#34;
现在如果我用常规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)
有什么建议吗?感谢
答案 0 :(得分:7)
这是一种操作顺序很重要的情况。 %%
运算符的绑定比+
更紧密。所以当你说
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()
返回相同的错误。您需要将所有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)}