2

I've tried searching stack overflow for the answer but I couldn't get it.

I'm using a reversed y axis to plot one y variable over time and I wanted to get my secondary y axis in normal order (not reversed).

library(tidyverse)
library(scales)
library(lemon)

    base_nivelprecip %>%
      mutate(nivel = as.double(nivel),
             precip = as.double(precip)) %>% 
      mutate(precip_rescaled = precip/200) %>% 
      ggplot(aes(x = data, y = nivel,
                 xmin = as.POSIXct("2012-05-01", "%Y-%m-%d"), 
                 xmax = as.POSIXct("2020-04-30",  "%Y-%m-%d"))) + 
    
      geom_col(aes(x = data, y = precip_rescaled), fill = '#8D8DAA', size = 3) +
      geom_line(group = 1, color = '#041562', size = 0.3) +
      labs(x = "", y = "Groundwater level (m)") +
      scale_y_reverse(limits = c(5,-0.5) ,  sec.axis = sec_axis(~rev(.)*200, name = "Precipitation (mm)")) +
      lemon::facet_rep_wrap(~poco, nrow = 3, repeat.tick.labels = TRUE) +
      theme_bw() +
      theme(text=element_text(size=12),
            axis.text.x = element_text(size = 8)

Although I've tried some transformations, the best I could get was this: enter image description here

But I wanted the geom_col (in secondary axis) to start at the secondary axis 0, not at the primary axis. Can somebody help me? Thanks. A sample of my data is here: https://docs.google.com/spreadsheets/d/1xgj3Gan1Lj9qO2aEGdLpiUOBk9g8USMS/edit?usp=sharing&ouid=111814647952336460862&rtpof=true&sd=true

6
  • Could you please share a sample of your data using head(dput(data), 20)? Commented Apr 16, 2022 at 15:02
  • poco data nivel precip <dbl> <dttm> <dbl> <dbl> 1 4300009528 2017-05-01 1.73 0 2 4300009528 2017-05-02 00:00:00 1.73 0 3 4300009528 2017-05-03 00:00:00 1.75 0 4 4300009528 2017-05-04 00:00:00 1.76 0 5 4300009528 2017-05-05 00:00:00 1.78 80 6 4300009528 2017-05-06 00:00:00 1.8 0 7 4300009528 2017-05-07 00:00:00 1.8 5 8 4300009528 2017-05-08 00:00:00 1.8 0 9 4300009528 2017-05-09 00:00:00 1.82 0 10 4300009528 2017-05-10 00:00:00 1.83 3.3 Commented Apr 16, 2022 at 15:07
  • Could you please copy and paste this from your console in your question above? Commented Apr 16, 2022 at 15:09
  • I'm sorry! but it gives me a huge string that doesn't fit in the comment section Commented Apr 16, 2022 at 15:11
  • head(dput(base_nivelprecip), 10) or less is also possible. Commented Apr 16, 2022 at 15:12

2 Answers 2

1

Perhaps the easiest way to achieve this is to "fake" the primary axis in the same way that you fake the secondary axis. That way, you don't need the added complication of scale_y_reverse:

library(tidyverse)
library(scales)
library(lemon)

base_nivelprecip %>%
  mutate(nivel = as.double(nivel),
         precip = as.double(precip),
         data = as.POSIXct(data, format = "%m/%d/%Y")) %>% 
  mutate(precip_rescaled = precip/200 ,
         nivel_rescaled = -nivel + 2.5) %>% 
  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"))) +

  geom_col(aes(x = data, y = precip_rescaled), 
           colour = '#8D8DAA', size = 1) +
  geom_line(color = '#041562', size = 0.3) +
  labs(x = "", y = "Groundwater level (m)") +
  scale_y_continuous(labels = function(x) -(x - 2.5), limits = c(0, 2.5),
                  sec.axis = sec_axis(~.*200, 
                                      name = "Precipitation (mm)")) +
  lemon::facet_rep_wrap(~poco, nrow = 3, repeat.tick.labels = TRUE) +
  theme_bw() +
  theme(text=element_text(size=12),
        axis.text.x = element_text(size = 8))

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

1

Keep in mind that in ggplot2, secondary axes are a decoration that does not change the mapping of your data. So if you want your data to be mapped to a secondary axis, you need to transform it to appear where you want in terms of the primary axis (it's not like Excel where that happens automatically). Then you also need to define the transformation of the secondary axis.

In the case below, I have a reversed primary axis which determines the placement of the points. I also have a secondary axis that transforms the primary axis into the opposite slope. Here, ~35-. means "transform so that the value for input 0 turns into 35, and the slope is reversed."

ggplot(mtcars, aes(wt, mpg)) +
  geom_point() +
  scale_y_continuous(trans = "reverse",
    sec.axis = sec_axis(~35-., name = "how far below 35 mpg"))

enter image description here

1 Comment

I think the OP is specifically trying to get bars going upwards starting from 0 on the secondary axis Jon

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.