1

I've got a dataframe df of calculated Annual exceedance probability (AEP) of daily maximum liquid precipitation (P):

df <- tibble::tribble(
   ~AEP,          ~P,
  0.001, 299.0973209,
   0.01, 254.7226534,
   0.03, 233.0298722,
   0.05, 223.9571177,
    0.1, 211.2898816,
    0.3, 190.5075232,
    0.5, 182.3294549,
      1, 170.5569051,
      3, 148.9113334,
      5,  138.991102,
     10, 125.4449161,
     20, 110.1408306,
     25,   104.74124,
     30, 100.2363357,
     40, 92.15268627,
     50, 85.75477796,
     60, 79.55311702,
     70, 73.44249835,
     75, 70.21061223,
     80, 66.79821521,
     90, 58.54507042,
     95, 52.44861458,
     97, 48.86357489,
     99, 43.12184627,
   99.5, 39.72675936,
   99.7,  37.5826596,
   99.9, 33.91759317
  )

All I need is to create a specific scale with equal distance between breaks in the middle and increasing on both ends. A perfect example from book is here:

enter image description here

All I was able to create by myself (based on code chuncks from this gist) return me a mess of labels:

library(dplyr)
library(scales)
library(ggplot2)

df %>% 
  ggplot(aes(x = AEP, y = P)) + 
  geom_point() +
  geom_line() +
  scale_y_continuous(name = "Precipitation (P), mm",
                     labels = scales::comma,
                     breaks = seq(0, 300, 50)) +
  scale_x_continuous(name = "AEP, %",
                     breaks = df$AEP,
                     labels = str_c(df$AEP,'%'),
                     expand = c(0.001,0.001)) +
  theme_grey(base_size = 12)

enter image description here

atsyplenkov
  • 1,158
  • 13
  • 25
  • How exactly do you want to determine the breaks? Is taking all the AEP values from your data what you want, or just the first try at it? – camille Nov 16 '18 at 16:09
  • @camille I want all AEP values to be breaks or at least as on the example (e.g. without 0.001) – atsyplenkov Nov 16 '18 at 16:12
  • I'm not familiar with the context of this type of data or chart. Can you explain how the breaks in your chart differ from those in the example? Where do the example's breaks come from? – camille Nov 16 '18 at 16:37
  • @camille the distance between breaks should not be the same. On both ends where the probability is below 20 (or higher than 80) it is logscale. I know that in Python it's possible to do with 'probscale', for example (https://matplotlib.org/mpl-probscale/) – atsyplenkov Nov 17 '18 at 07:27

1 Answers1

2

I found an answer in one of the Stack Overflow's questions. To create desired scale we need to apply a qnorm quantile function to all AEP values (or other x values), e.g.

df %>% 
  ggplot(aes(x = qnorm(AEP/100), # transform to quantiles
             y = P)) + 
  geom_point() +
  geom_line() +
  scale_y_continuous(name = "Precipitation (P), mm",
                     labels = scales::comma,
                     breaks = seq(0, 300, 50)) +
  scale_x_continuous(name = "AEP, %",
                     breaks = qnorm(df$AEP/100), #transform
                     labels = df$AEP,
                     expand = c(0.035,0.035)) +
  theme_bw(base_size = 12)

enter image description here

atsyplenkov
  • 1,158
  • 13
  • 25