Reputation: 71
I am trying to plot a mean line and a quintile line using ggplot2
.
DF<-data.frame(DOB = c(1965, 1949, 1964, 1979, 1960, 1992, 1991, 1963, 1964, 1992, 1971, 1965),
trip.duration.hr =c(3.36, 2.25, 5.31, 10.7, 1.96, 4.33, 23.55, 3.92, 5.46, 3.45, 13.72, 7.33))
I have inserted my code below. When I try to run it, it gives me the following error message:
No summary function supplied, defaulting to
mean_se()
No summary function supplied, defaulting to
mean_se()
Warning messages:
1: Ignoring unknown parameters: fun.y 2: Ignoring unknown parameters: fun.y 3: Removed 40135 rows containing non-finite values (stat_summary). 4: Removed 40135 rows containing non-finite values (stat_summary). 5: Removed 40216 rows containing missing values (geom_point).
My code below:
ggplot(DF, aes(x=DOB, y=trip.duration.hr)) +
geom_jitter(alpha=1/10) +
geom_line(stat = 'summary', fun.y = "mean", color="orange", size=1) +
geom_line(stat = 'summary', fun.y = "quantile", fun.args = list(probs = .9), linetype=2, color="red")
Upvotes: 3
Views: 6575
Reputation: 1
change geom_line(stat = 'summary'
to stat_summary(geom ='line'
and the code will run well.
ggplot(DF, aes(x = DOB, y = trip.duration.hr)) +
geom_jitter(alpha = 1/10) +
stat_summary(geom = "line", fun = "mean", color = "orange", size = 1) +
stat_summary(geom = "line", fun = "quantile", fun.args = list(probs = .9), linetype = 2, color = "red")
Upvotes: 0
Reputation: 1422
Just replace geom_line
with stat_summary
including geom = "line"
like so:
ggplot(DF, aes(x = DOB, y = trip.duration.hr)) +
geom_jitter(alpha = 1/10) +
stat_summary(geom = "line", fun = "mean", color = "orange", size = 1) +
stat_summary(geom = "line", fun = "quantile", fun.args = list(probs = .9), linetype = 2, color = "red")
It will also tell you fun.y is deprecated, so I've just used fun instead.
edit based on OP request for legend
library(tidyverse)
DF %>%
group_by(DOB) %>%
mutate(mean = mean(trip.duration.hr),
quantile = quantile(trip.duration.hr, probs = 0.9)) %>%
ungroup %>%
pivot_longer(cols = c(mean, quantile), names_to = "summary_stat") %>%
ggplot(aes(x = DOB, y = value, group = summary_stat)) +
geom_jitter(aes(x = DOB, y = trip.duration.hr), inherit.aes = FALSE, alpha = 1/10) +
geom_line(aes(lty = summary_stat, col = summary_stat)) +
scale_colour_manual(values = c("orange", "red")) +
labs(y = "trip.duration.hr")
Upvotes: 7