Codi general

library(RODBC)
library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
# mida de les figures en els informes
knitr::opts_chunk$set(fig.width=15, fig.height=8) 

# CONNEXIO A LA BASE DE DADES
channel <- odbcDriverConnect("Driver={PostgreSQL ODBC Driver(ANSI)};Server=localhost;port=5434;Uid=postgres;Pwd=Tundra01;database=meteobailen")

# Dades mensuals
script <- "select * from dades.meteo_mes"

df.p <- sqlQuery(channel, script)
df.p$any <- as.character(as.numeric(df.p$any))  # Convert one variable to numeric

# DADES DIARIES
script.d <- "select *, extract(doy from data) diaany, round(avg(tempc_avg) OVER (PARTITION BY estacio, 'any'),1) tempc_avg_any from dades.meteo_dia"

df.d <- sqlQuery(channel, script.d)
df.d$any <- as.character(as.numeric(df.d$any))  # Convert one variable to numeric

#DADES DIARIES AMB TOTES LES DADES
script.t <- "select *, extract(doy from dateeuropemadrid) diaany, to_char(dateeuropemadrid,'yyyy') any from dades.meteo"

df.t <- sqlQuery(channel, script.t)
df.t$any <- as.character(as.numeric(df.t$any))  # Convert one variable to numeric

Figures

Dades mensuals

# linies de la precipitació mensual
plot <- ggplot(data=df.p, aes(x=mes, y=rainmm_sum)) +
  geom_bar(stat="identity", fill="steelblue") +
  scale_color_brewer(palette="Paired") +
  xlim("01", "02","03","04","05","06","07","08","09","10","11","12") +
  ggtitle("Precipitació mensual per cada any") + xlab("Mes") + ylab("Precipitació (mm)") +
  facet_grid(any ~ estacio)

plot

# linies de la temperatura mesnual mitjana
plot <- ggplot(data=df.p, aes(x=mes, y=tempc_avg, color =  any)) +
  geom_line() +
  geom_point() +
  xlim("01", "02","03","04","05","06","07","08","09","10","11","12") +
  ggtitle("Temperatura mitjana mensual") + xlab("Mes") + ylab("Temperatura (ºC)") + labs(color = "Any") +
  facet_grid(. ~ estacio)

plot

# amb barres d'error
plot <- ggplot(df.p, aes(x=mes, y=tempc_avg, group=any, color=any)) + 
  geom_errorbar(aes(ymin=tempc_avg-tempc_sd, ymax=tempc_avg+tempc_sd), width=.1, 
                position=position_dodge(0.05), size = 1) +
  geom_line(size = 1) + geom_point()+
  scale_color_brewer(palette="Dark2") + 
  xlim("01", "02","03","04","05","06","07","08","09","10","11","12") +
  ggtitle("Temperatura mitjana mensual") + xlab("Mes") + ylab("Temperatura (ºC)") + labs(color = "Any") +
  #theme_classic() +
  facet_grid(. ~ estacio)
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
plot

Dades diàries

plot <- ggplot(df.d, aes(x=data, y=tempc_avg, group=any, color=any)) + 
  geom_errorbar(aes(ymin=tempc_avg-tempc_sd, ymax=tempc_avg+tempc_sd), width=.1, 
                position=position_dodge(0.05), size = 1) +
  geom_line(size = 1) + geom_point()+
  scale_color_brewer(palette="Dark2") + 
  ggtitle("Temperatura mitjana diaria") + xlab("Dia") + ylab("Temperatura (ºC)") + labs(color = "Any") +
  #theme_classic() +
  facet_grid(estacio ~ .)

plot
## Warning: Removed 5 rows containing missing values (`geom_point()`).

ggplotly(plot)
plot <- ggplot(df.d, aes(x=diaany, y=tempc_avg, group=any, color=any)) + 
  geom_errorbar(aes(ymin=tempc_avg-tempc_sd, ymax=tempc_avg+tempc_sd), width=.1, 
                position=position_dodge(0.05), size = 1) +
  geom_line(size = 1) + geom_point()+
  scale_color_brewer(palette="Dark2") + 
  ggtitle("Temperatura mitjana diaria") + xlab("Dia de l'any") + ylab("Temperatura (ºC)") + labs(color = "Any") +
  #theme_classic() +
  facet_grid(estacio ~ .)

plot
## Warning: Removed 5 rows containing missing values (`geom_point()`).

plot <- ggplot(data=df.d, aes(x=diaany, y=tempc_avg, color =  any)) +
  #  geom_line() +
  geom_point(size = .1) +
  geom_smooth(span = 0.3) +
  geom_hline( aes(yintercept = (tempc_avg_any), col = any)) + #línia de la mitjana per anys, però no entenc perquè no surt la de 2021
  ggtitle("Temperatura mitjana diaria") + xlab("Dia de l'any") + ylab("Temperatura (ºC)") + labs(color = "Any") +
  facet_grid(estacio ~ .)

plot
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
## Warning: Removed 5 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 5 rows containing missing values (`geom_point()`).

plot <- ggplot(data=df.t, aes(x=diaany, y=tempc, color =  any)) +
#  geom_line() +
  geom_point(size = .1) +
  geom_smooth() +
   ggtitle("Temperatura diaria") + xlab("Dia de l'any") + ylab("Temperatura (ºC)") + labs(color = "Any") +
  facet_grid(. ~ estacio)

plot
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'
## Warning: Removed 4121 rows containing non-finite values (`stat_smooth()`).
## Warning: Removed 4121 rows containing missing values (`geom_point()`).