11.1 Pré-requisitos
11.1.1 Pacotes
O pacote {ggplot2}
faz parte do {tidyverse}
, mas além dele precisaremos de outros pacotes com funcionalidades que complementam o {ggplot2}
.
pacotes <- c(
"openair",
"lubridate",
"scales",
"rio",
"tidyverse",
#"ggrepel",
#"ggthemes",
#"viridis",
#"ggpubr",
#"ggmap",
"psych",
"grid",
"lattice",
"gcookbook"
)
easypackages::libraries(pacotes)
11.1.2 Dados
- dados simulados de uma oscilação com amplitude variável
x1 <- seq(
from = -100,
to = 100,
by = 0.05
)
A <- seq(
from = -1,
to = 1,
length.out = length(x1)
) %>%
sin(x = .)
y1 <- exp(-0.07 * A * x1) * cos(x1 + pi / 2)
onda <- tibble(x1, y1)
onda
#> # A tibble: 4,001 x 2
#> x1 y1
#> <dbl> <dbl>
#> 1 -100 -0.00140
#> 2 -100. -0.00153
#> 3 -99.9 -0.00165
#> 4 -99.8 -0.00177
#> 5 -99.8 -0.00188
#> 6 -99.8 -0.00199
#> 7 -99.7 -0.00210
#> 8 -99.6 -0.00221
#> 9 -99.6 -0.00231
#> 10 -99.6 -0.00240
#> # … with 3,991 more rows
- Dados de qualidade do ar em NY
#help(airquality)
head(airquality)
#> Ozone Solar.R Wind Temp Month Day
#> 1 41 190 7.4 67 5 1
#> 2 36 118 8.0 72 5 2
#> 3 12 149 12.6 74 5 3
#> 4 18 313 11.5 62 5 4
#> 5 NA NA 14.3 56 5 5
#> 6 28 NA 14.9 66 5 6
# conversão da Temp de Farenheith para Celsius
aq <- airquality %>%
mutate(Temp = (Temp - 32)/5,
# adicionando coluna date criada das colunas Day e Month
# o ano das medidas é 1973, conforme help
date = as.Date(
paste(Day, Month, "1973"),
format = "%d %m %Y")
) %>%
# removendo as colunas Month e Day e reordenando as colunas
dplyr::select(., date, Ozone:Temp, -c(Month, Day))
head(aq)
#> date Ozone Solar.R Wind Temp
#> 1 1973-05-01 41 190 7.4 7.0
#> 2 1973-05-02 36 118 8.0 8.0
#> 3 1973-05-03 12 149 12.6 8.4
#> 4 1973-05-04 18 313 11.5 6.0
#> 5 1973-05-05 NA NA 14.3 4.8
#> 6 1973-05-06 28 NA 14.9 6.8
- Precipitação climatológica mensal das estações climatológicas do INMET
# importando dados
prec <- import(
"https://www.dropbox.com/s/9ym0845apcj38wq/PrecAccInmet_61_90.rds?dl=1",
format = "rds"
)
head(prec)
#> codigo nome estado mes value
#> 1 82704 Cruzeiro do Sul AC jan 257.9
#> 2 82915 Rio Branco AC jan 289.0
#> 3 82807 Tarauacá AC jan 286.6
#> 4 83098 Coruripe AL jan 21.6
#> 5 82994 Maceió AL jan 78.1
#> 6 82988 Mata Grande AL jan 62.1
- Dados de precipitação anual de algumas capitais do mundo
## dados rain
rain <- import(
file = "https://www.dropbox.com/s/z873gcyjouegspk/cityrain.csv?dl=1",
format = "csv",
header = TRUE
)
head(rain)
#> Month Tokyo NewYork London Berlin
#> 1 Jan 49.9 83.6 48.9 42.4
#> 2 Feb 71.5 78.8 38.8 33.2
#> 3 Mar 106.4 98.5 39.3 34.5
#> 4 Apr 129.2 93.4 42.4 39.7
#> 5 May 144.0 106.0 47.0 52.6
#> 6 Jun 176.0 84.5 48.3 70.5
- anomalias de temperatura do ar global Global de 1800 a 2011
data(climate, package = "gcookbook")
#help(climate,package = "gcookbook")
head(climate)
#> Source Year Anomaly1y Anomaly5y Anomaly10y Unc10y
#> 1 Berkeley 1800 NA NA -0.435 0.505
#> 2 Berkeley 1801 NA NA -0.453 0.493
#> 3 Berkeley 1802 NA NA -0.460 0.486
#> 4 Berkeley 1803 NA NA -0.493 0.489
#> 5 Berkeley 1804 NA NA -0.536 0.483
#> 6 Berkeley 1805 NA NA -0.541 0.475
- Metadados das estações meteorológicas automáticas (EMA) do INMET;
# dados de estações com localização, alt e tmed
sulbr_md <- import(
"https://www.dropbox.com/s/3ddxq5v5a8i7dnw/info_sumary_tair_sul.rds?dl=1",
format = "rds"
) %>%
mutate(.,
tmed = (tmax_med + tmin_med) / 2
)
sulbr_md
#> # A tibble: 82 x 18
#> site tmax_med tmin_med dtr_med sdate edate period max_tair
#> <chr> <dbl> <dbl> <dbl> <date> <date> <dbl> <dbl>
#> 1 A801 24.8 16.1 8.66 2000-09-22 2015-12-31 15.3 40
#> 2 A802 22.1 15.3 6.77 2001-11-16 2015-12-31 14.1 38
#> 3 A803 24.8 14.8 9.94 2001-11-26 2015-12-31 14.1 40
#> 4 A805 24.7 15.1 9.60 2001-12-05 2015-12-31 14.1 36
#> 5 A806 24.5 18.0 6.49 2003-01-22 2015-12-31 12.9 39
#> 6 A807 22.9 14.3 8.65 2003-01-28 2015-12-31 12.9 34
#> 7 A808 22.2 16.3 5.87 2006-06-01 2015-12-31 9.6 41
#> 8 A809 25.5 15.0 10.5 2006-09-28 2015-12-31 9.3 40
#> 9 A810 26.5 15.3 11.2 2006-11-15 2015-12-31 9.1 39
#> 10 A811 20.9 13.0 7.84 2007-01-24 2015-12-31 8.9 37
#> # … with 72 more rows, and 10 more variables: min_tair <dbl>, missing <dbl>,
#> # long_gap <dbl>, sdate_lg <dttm>, name <chr>, state <chr>, lon <dbl>,
#> # lat <dbl>, alt <dbl>, tmed <dbl>
- dados meteorológicos horários das EMAs
# dados com séries temporais
sulbr_dh <- import("https://www.dropbox.com/s/iesn64ij633rofp/data_inmet_sul_RS.rds?dl=1",
format = "rds"
) %>%
#filter(year(date) >= 2008, year(date) <= 2015) %>%
select(site:tair, rh, prec, rg, wd, ws, wsmax) %>% as_tibble()
glimpse(sulbr_dh)
#> Rows: 2,806,728
#> Columns: 9
#> $ site <chr> "A801", "A801", "A801", "A801", "A801", "A801", "A801", "A801",…
#> $ date <dttm> 2000-09-22 00:00:00, 2000-09-22 01:00:00, 2000-09-22 02:00:00,…
#> $ tair <dbl> NA, NA, NA, NA, 15.5, 15.3, 15.1, 14.8, 14.5, 14.4, 14.4, 15.3,…
#> $ rh <dbl> NA, NA, NA, NA, 94, 95, 94, 95, 94, 85, 86, 84, 76, 62, 49, 45,…
#> $ prec <dbl> NA, NA, NA, NA, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,…
#> $ rg <dbl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 16.944444, 141.111111, …
#> $ wd <dbl> NA, NA, NA, NA, 231, 255, 245, 246, 239, 177, 217, 166, 159, 18…
#> $ ws <dbl> NA, NA, NA, NA, 1.2, 2.2, 1.9, 1.5, 1.7, 1.7, 1.8, 2.2, 2.2, 2.…
#> $ wsmax <dbl> NA, NA, NA, NA, 4.4, 4.2, 4.1, 3.9, 3.3, 6.7, 7.6, 5.3, 6.1, 4.…
#range(sulbr_dh$date)
Vamos obter a média da tair
para cada dia do ano na EMA de POA.
tair_poa_dly <- sulbr_dh %>%
filter(site == "A801") %>%
select(site:tair) %>%
group_by(date = as.Date(date)) %>%
summarise(tair = mean(tair, na.rm = TRUE)) %>%
mutate(tair = ifelse(is.nan(tair), NA, tair))
glimpse(tair_poa_dly)
#> Rows: 5,579
#> Columns: 2
#> $ date <date> 2000-09-22, 2000-09-23, 2000-09-24, 2000-09-25, 2000-09-26, 200…
#> $ tair <dbl> 17.12000, 17.60417, 14.99583, 10.70417, 11.83333, 14.52917, 18.5…
tair_poa_clim <- tair_poa_dly %>%
group_by(doy = lubridate::yday(date)) %>%
summarise(med = mean(tair, na.rm = TRUE),
max = max(tair, na.rm = TRUE),
min = min(tair, na.rm = TRUE),
q5 = quantile(tair, p = 0.05, na.rm = TRUE),
q95 = quantile(tair, p = 0.95, na.rm = TRUE),
n_anos = n(),
n_obs = sum(!is.na(tair))
) %>%
ungroup()
tair_poa_clim
#> # A tibble: 366 x 8
#> doy med max min q5 q95 n_anos n_obs
#> * <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <int> <int>
#> 1 1 24.3 27.5 19.6 20.0 27.4 15 15
#> 2 2 24.2 28.4 20.2 20.3 28.1 15 15
#> 3 3 23.5 27.3 16.0 19.8 27.3 15 15
#> 4 4 24.4 27.3 19.2 21.5 27.1 15 15
#> 5 5 24.9 27.8 20.9 21.7 26.9 15 15
#> 6 6 25.5 28.6 22.5 23.6 27.9 15 15
#> 7 7 25.8 28.8 21.8 23.1 28.2 15 15
#> 8 8 25.6 28.5 23.4 23.5 27.9 15 15
#> 9 9 26.0 29.5 21.9 23.8 28.5 15 15
#> 10 10 25.8 29.4 22.3 22.8 28.6 15 15
#> # … with 356 more rows
#timePlot(filter(sulbr_dh, site == "A801"), "tair")