For a single dataframe that has multiple variables, I used the following code to compute statistics and for plotting.
library(tidyverse)
DF1 <- data.frame(seq(as.Date("2001-01-01"), to= as.Date("2003-12-31"), by="day"),
MaxTemp = runif(1095,-5,20),
MinTemp = runif(1095,-8,15),
MeanTemp = runif(1095,-10,10),
DF = rep("DF1",1095))
colnames(DF1) <- c("Date", "MaxTemp","MinTemp","MeanTemp","DF")
DF_1 <- DF1 %>% mutate(JDay = yday(Date)) %>%
group_by(JDay) %>%
summarise(AveMaxTemp = mean(MaxTemp, na.rm = T),
AveMinTemp = mean(MinTemp, na.rm = T),
AveMeanTemp = mean(MeanTemp, na.rm = T))
DF_1 %>% gather(key = "Variable", value = "Value", -c(JDay)) %>%
ggplot(aes(x = JDay, y = Value, col = Variable))+
geom_line(aes(y = Value))
In case, I have multiple data.frame
(see the example below). Is there a way, to combine the two data.frame
(ie, DF1
and DF2
), compute statistics, and then plot it using [Tag:facet_wrap]?
DF2 <- data.frame(Date = DF1$Date,
MaxTemp = runif(1095, -2,15),
MinTemp = runif(1095,-6,15),
MeanTemp = runif(1095,-5,10),
DF = rep("DF2",1095))
I am looking for a plot like below
try to do so
DF3 <- bind_rows(DF1, DF2)
DF_3 <- DF3 %>%
mutate(JDay = yday(Date)) %>%
group_by(JDay, DF) %>%
summarise(AveMaxTemp = mean(MaxTemp, na.rm = T),
AveMinTemp = mean(MinTemp, na.rm = T),
AveMeanTemp = mean(MeanTemp, na.rm = T))
DF_3 %>% gather(key = "Variable", value = "Value", -c(JDay, DF)) %>%
ggplot(aes(x = JDay, y = Value, col = Variable)) +
geom_line(aes(y = Value)) +
facet_wrap(~ DF)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.