I'm trying to display data of two variables 'nivel' and 'precip' over time, on two y-axis (first: nivel and second: precip). I want to align the primary y axis ranging from -1 to 5 and the secondary y axis ranging from 0 to 600, both reversed, but I can't seem to find the right transformation of axis and labels for the secondary y axis (the first one is alright).
I've uploaded the data in: https://github.com/schuhf/schuhf/blob/main/DadosCompletos_PA.xlsx
My current code is:
## load data base_nivelprecip <- read_excel("C:/Users/frori/Documents/UFRGS/FAPERGS_SAQC/VNA/AnoHidro/REVISAO/DadosCompletos_PA.xlsx")
base_nivelprecip %>%
mutate(nivel = as.double(nivel),
precip = as.double(precip),
precip_rescaled = precip,
nivel_rescaled = - nivel + 5 ) %>% ## transform data
ggplot(aes(x = data, y = nivel_rescaled,
xmin = as.POSIXct("2012-05-01", "%Y-%m-%d"),
xmax = as.POSIXct("2020-04-30", "%Y-%m-%d"))) + ## set variables and limits
geom_col(aes(x = data, y = precip_rescaled), color = '#5F7161', fill = '#5F7161', size = 0.1) +
geom_line(color = '#041562', size = 0.3) +
labs(x = "", y = "Water level (m)") +
scale_y_continuous(expand = c(0.001,0.001),
labels = function(x) -x + 5,
limits = c(0, 6),
sec.axis = sec_axis(~.,
name = "Rainfall (mm)")) +
lemon::facet_rep_wrap(~poco, nrow = 4, repeat.tick.labels = 'all', scales = 'free') +
theme_bw() +
theme(text=element_text(size=12),
axis.title.y = element_text(size = 10),
axis.text.y = element_text(size = 8),
axis.text.x = element_text(size = 8),
panel.grid = element_blank())
A small data sample is:
# A tibble: 5 × 4
poco data nivel precip
<dbl> <dttm> <chr> <chr>
1 4300020528 2014-05-02 00:00:00 2.2000000000000002 0
2 4300020528 2014-05-03 00:00:00 2.2200000000000002 0
3 4300020528 2014-05-04 00:00:00 2.2200000000000002 0
4 4300020528 2014-05-05 00:00:00 2.23 1.1000000000000001
5 4300020528 2014-05-06 00:00:00 2.2400000000000002 0