0

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  
  • a data sample: poco data nivel precip 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 – fernando schuh Nov 21 '22 at 17:27
  • 1
    (1) Please don't put sample data in a comment, it's hard to format and easy to miss by readers; please [edit] your question and place it in a code block. Thanks! (2) Likely a dupe of https://stackoverflow.com/q/58543596/3358272, please look at that answer. – r2evans Nov 21 '22 at 17:29

1 Answers1

1

Update:

If this is what you want:

Then just multiply secondary y by 100 and wrap it around rev()

sec.axis = sec_axis(~rev(.*100),name = "Rainfall (mm)")) +

enter image description here

TarJae
  • 72,363
  • 6
  • 19
  • 66
  • Thank you! but I want the secondary axis reversed as well, with the -1 of the primary axis aligned with the 0 from the secondary axis. – fernando schuh Nov 21 '22 at 17:40
  • Do you mean this? `sec.axis = sec_axis(~rev(.*100), name = "Rainfall (mm)")) +` – TarJae Nov 21 '22 at 17:43
  • It's still not quite it. I got the labels right with transforming `precip_rescaled = - precip/100` and with `sec.axis = sec_axis(~rev(-.*100), labels = function (x) - x)` but the values are not displaying accordingly with the labels – fernando schuh Nov 21 '22 at 18:00