airquality dataset : plot ozone for each month and in the same graph the mean

37 views
Skip to first unread message

Pietro Begnis

unread,
Sep 8, 2022, 8:00:20 AM9/8/22
to ggplot2
Hi,
using the data set air quality I have written the following code:

library("tidyverse")
data(airquality)
airquality<-na.omit(airquality)
airquality$date <- as.Date(paste("1973", airquality$Month,airquality$Day,
                                 sep="-"))
p1<-ggplot(airquality, aes(x= date, y = Ozone, col=factor(Month))) +geom_point() + geom_line()
p1 
Now I would like to plot in the same graph the mean of ozone for each months.
Can anyone help me?
My best regards, Peter

Robert Winkelman

unread,
Oct 23, 2022, 9:25:01 PM10/23/22
to ggplot2
add this:

airquality2 <- airquality %>% group_by(Month) %>% mutate(MeanOzone=mean(Ozone))

p2<-ggplot(airquality2, aes(x= date, y = Ozone, col=factor(Month))) + geom_point() + geom_line()
p2 + geom_line(aes(y=MeanOzone))

Reply all
Reply to author
Forward
0 new messages