I am trying to create two graphs: one of the whole dataset and also a mean graph when split by the "site" grouping factor.
Here is the source data:
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))
This is my current code:
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)
But I get this error message: "Error in as.vector(x, mode) : cannot coerce type 'environment' to vector of type 'any' "
Now if I replace the tee operator with the regular magrittr pipe operator and also comment out the first ggplot line then at least I get the second ggplot, like this:
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)
Any suggestions? Thanks
This is a case where order of operations is important. The %%
operators bind more tightly than +
does. So when you say
site.data %T>% ggplot(aes(year, peak, color = site)) + geom_line()
that's the same as
( site.data %T>% ggplot(aes(year, peak, color = site)) ) + geom_line()
which is basically doing
site.data + geom_line()
which returns the same error you get. You will need to explicitly group all your ggplot layer additions/modifications into a code block. Try
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)}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With