How to generate charts of daily, monthly and annual averages in time series data in R?

Asked

Viewed 106 times

1

I would like to know how to make averages of various intervals, minimum and maximum daily and monthly of a time series of a file that has two columns (date and temperature value), but the date is in the following format yyyy-MM-dd HH:mm ss:.

I read some questions/answers here and I was totally confused, sorry, I’m new to R. I installed several programs (xts, zoo, lubridate).

I ran a time series like this:

st <- ts(CEM_1$Temp, start = 2017, frequency = 48)

But I can’t decompose on daily or monthly averages, nor understand if the file is reading right.

head(CEM_1)
# A tibble: 6 x 2
  Data        Temp
  <date>     <dbl>
1 2017-04-18  25.4
2 2017-04-18  24.9
3 2017-04-18  24.8
4 2017-04-18  27.3
5 2017-04-18  26.0
6 2017-04-18  24.8

And how to make the graphs "to see better" this data?

  • 2

    Welcome to Stackoverflow! Unfortunately, this question cannot be reproduced by anyone trying to answer it. Please, take a look at this link and see how to ask a reproducible question in R. So, people who wish to help you will be able to do this in the best possible way.

  • 1

    The question is very general, but before it closes, agg <- aggregate(Temp ~ Data, CEM_1, mean, na.rm = TRUE). Afterward, library(ggplot2); ggplot(agg, aes(x = Data, y = Temp)) + geom_line().

  • Perfect, came out a general chart of temperatures all over time, and now how to make daily, monthly, annual averages??? I’ll paste the script as it is so far, okay? Thanks for the help I’m trying to understand how the community works!!!

  • #opening Excel library(readxl) > temperature <- read_excel("PELD/sheets/CEM_1.xlsx", + col_types = c("date", "Numeric")) > View(CEM_1) > #Opening package to generate time series! library(lubridate) st <- ts(CEM_1$Temp, start = 2017, Frequency = 48) Agg <- Aggregate(Temp ~ Date, CEM_1, Mean, na.rm = TRUE) #After, library(plotgg2) ggplot(Agg, aes(x = Date, y = Temp)) + geom_line()

  • Link to the chart: https://drive.google.com/file/d/1JXe1KmHd-QhUYDFLT-JLVUXVkZlP8e2V/view?usp=sharing

  • Link to the spreadsheet: https://drive.google.com/file/d/1lO7z-G-cmUB6K50AsDLOaf2wyDsjioj/view?usp=sharing

  • See if the answer is as you wish.

Show 2 more comments

1 answer

0

These two solutions, one for daily averages and the other for monthly averages, are very similar and use the package dplyr.

library(readxl)
library(lubridate)
library(ggplot2)

temperatura <- read_excel("CEM_1.xlsx", col_types = c("date", "numeric"))

temperatura %>%
  mutate(Dia = as.Date(Data)) %>%
  group_by(Dia) %>%
  summarise(Temp = mean(Temp, na.rm = TRUE)) %>%
  ggplot(aes(x = Dia, y = Temp)) + 
  geom_line()

inserir a descrição da imagem aqui

temperatura %>%
  mutate(Mes = zoo::as.yearmon(Data)) %>%
  group_by(Mes) %>%
  summarise(Temp = mean(Temp, na.rm = TRUE)) %>%
  mutate(Ano = factor(year(Mes)),
         Mes = month(Mes, label = TRUE, abbr = TRUE)) %>%
  ggplot(aes(x = Mes, y = Temp,
             group = Ano, colour = Ano)) + 
  geom_point() +
  geom_line()

inserir a descrição da imagem aqui

Browser other questions tagged

You are not signed in. Login or sign up in order to post.