Reputation: 623
I have measured the emission
of two compounds
(acetone and acetaldehyde) from two soil_type
(A and B), across a period of 94 days on thre specific occasions measuring_day
.
I wish to illustrate the cumulated emission across the 94 days
This is what I have unsuccesfully tried:
df<- df%>%
group_by(daysincubated4, compound, soil_type)%>%
summarise(mean=mean(emission))%>%
mutate(cum_emission=cumsum(mean))
plot <- ggplot(df, aes(x = daysincubated4, y = cum_emission, colour=factor(soil_type))) +
geom_line(size = 1)+
facet_wrap(~compound)
I'm looking for something similar to this:
My data looks like this:
df <- structure(list(daysincubated4 = c(0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 94, 94, 94, 94, 94,
94, 94, 94), soil = c(2, 2, 1, 1, 4, 4, 5, 5, 3, 3, 2, 2, 1,
1, 4, 4, 5, 5, 3, 3, 2, 2, 4, 4, 5, 5, 3, 3), soil_type = c("B",
"B", "B", "B", "B", "B", "A", "A", "B", "B", "B", "B", "B", "B",
"B", "B", "A", "A", "B", "B", "B", "B", "B", "B", "A", "A", "B",
"B"), compound = c("Acetaldehyde", "Acetone", "Acetaldehyde",
"Acetone", "Acetaldehyde", "Acetone", "Acetaldehyde", "Acetone",
"Acetaldehyde", "Acetone", "Acetaldehyde", "Acetone", "Acetaldehyde",
"Acetone", "Acetaldehyde", "Acetone", "Acetaldehyde", "Acetone",
"Acetaldehyde", "Acetone", "Acetaldehyde", "Acetone", "Acetaldehyde",
"Acetone", "Acetaldehyde", "Acetone", "Acetaldehyde", "Acetone"
), emission = c(0.44, 0.42, 0.65, 0.7, 0.09, 0, 0, 0.01, 0.03,
0.18, 0.01, 0.54, 0.01, 0.77, 0.01, 0, 0, 0.11, 0.02, 0.04, 0,
0.01, 0, 0.01, 0.01, 0, 0.01, 0.02)), row.names = c(NA, -28L), class = c("tbl_df",
"tbl", "data.frame"))
Any ideas?
Upvotes: 0
Views: 27
Reputation: 66415
I think you want:
...
group_by(compound, soil_type, daysincubated4)%>%
...
The reason is that summarise
works from the outside in, i.e. from the last grouping back to the first. With your original version, the data after summarise
was still grouped by daysincubated and soil type, so the cumulative sum happened within those. In your case, you want the data grouped by compound and soil type, but the cumulative across daysincubated.
Upvotes: 1