Home > front end >  Scaling dual y axis plot in R using ggplot
Scaling dual y axis plot in R using ggplot

Time:11-08

I am trying to create a depth and temperature vs time plot. The temperature ranged from 14-21C and the depth ranged from 0-7.5m. I am having issues forcing the temperature scale to only be from 14-21 while the depth scale remains at 0-7.5. I am also having problems figuring out how only to reverse the primary axis and keep the secondary axis unreversed.

This picture is what I have so far the plot i currenlty can make

however, i need the secondary scale expanded and the scale of it to be un reversed.

Here is the code I am working with/currently:


coeff <- 3.5

ggplot(dat, aes(x = hour))  
geom_line(aes(y = avg_depth), color = "black")  
geom_line(aes(y = (avg_temp / coeff)), color = "red")  
scale_y_continuous(trans = "reverse", name = "depth m", breaks = seq(0,7.5,1), sec.axis = sec_axis(~.*coeff, name = "temp c", breaks = seq(14,22,1)))  
theme_bw()  
theme( axis.title.y = element_text(color = "black", size=13), axis.title.y.right = element_text(color = "red", size=13) )  
facet_wrap(~month)

Here is a dput() output of what the data look like

dat <- structure(list(hour = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,  14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L,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"August", "September", "October", "November", "December"), class = "factor"), avg_depth = c(0.674104255319149, 1.16141723716381, 0.46781443148688, 0.569350711610487, 0.821086666666667, 1.85221716906946, 3.4268076419214, 5.65440574712644, 0.616312741312741, 1.47207503201024, 0.616066710182768,  0.638740190972222, 0.810777301790281, 2.01457261676264, 3.36415133437991, 6.15696138613861, 0.610769090909091, 1.27732580152672, 0.4530571197411, 0.693855674102813, 0.952273323285607, 1.74055824706694, 3.61179272727273, 5.98195405405405, 0.722235111111111, 1.52565404580153, 0.379650976909414, 0.587217663817664, 0.863865517241379, 1.60190743289745, 3.98509323671498, 7.17103939393939, 1.16354933333333, 1.63964759124088, 0.22209219269103, 0.608081025641026, 0.928734545454545, 1.83999729136164, 3.74237966101695,  5.117916, 2.8523356223176, 3.11033643533123, 1.75334429530201, 1.12571253588517, 1.12463694805195, 1.97661355585831, 4.84568290258449, 4.76447142857143, 2.11757875, 2.40585516483516, 1.09251965517241, 1.60237222222222, 1.83568177710843, 2.75534613702624, 4.04060363128492, 5.78596666666667, 2.63774363636364, 2.29331220930233, 0.77616, 1.10007866184448, 1.42876923076923, 2.01189947368421, 3.84362485714286, 6.71578, 2.13474590163934, 2.17037283018868, 0.694685217391304, 1.43871231060606, 1.32960293308891,  2.01551460423634, 3.34886953488372, 5.59423725490196, 2.29458939393939, 1.92593313432836, 0.441394570135747, 1.14764970588235, 1.39075131164743, 1.69570840707965, 2.38680161290323, 5.72604857142857, 1.24143787878788, 1.71293395061728, 0.408103184713376, 0.694587361111111, 1.16296766169154, 1.42053806881243, 2.05217391304348, 4.45380344827586, 1.44979530201342, 1.30386650246305, 0.276496173469388, 0.409692631578947, 0.796362308478039, 1.40612004175365, 1.63052086614173, 4.69366923076923, 1.42541492537313,  1.26328473282443, 0.53241756097561, 0.441229568788501, 0.737582608695652, 1.1452046692607, 1.79292577903683, 4.49204736842105, 0.91743063583815, 0.831260960960961, 0.503330205949657, 0.26842350877193, 0.644189047619048, 0.910615674255692, 1.22900170731707, 3.54798510638298, 0.571305025125628, 0.129495254237288, 0.206634959349593, 0.21165325, 0.536572630560928, 0.667085635359116, 1.16518847184987, 3.58613655913978, 0.910796721311475, 0.867520072992701, 0.188670698924731, 0.135299527744982, 0.611749167591565,  0.755583778801843, 1.79804401294498, 4.17658985507246, 0.638515668202765, 0.995278481012658, 0.163949529780564, 0.197539821693908, 0.698209720372836, 0.784387046004843, 2.32800274390244, 3.81578292682927, 1.108674, 1.15969727272727, 0.300007692307692, 0.55911566091954, 0.831757255936675, 1.22999745508982, 2.41673333333333, 4.97039180327869, 1.71473367346939, 1.34693986486486, 0.263691570881226, 0.438736363636364, 1.11891881067961, 2.59670830860534, 3.96090025575448, 4.2819350877193, 1.73873260869565,  2.79971208459214, 0.643953846153846, 1.02863309957924, 1.64601155234657, 2.34617211191336, 4.22193452914798, 3.41848, 2.95643957446809, 3.77135458823529, 0.664984664536741, 1.58177615298088, 0.927709062253743, 2.04243081761006, 4.00474120603015, 5.08179565217391, 0.964061157024793, 1.67683, 0.691920124481328, 0.492731824817518, 1.25362104909213, 2.05283324538259, 4.03329810126582, 4.84152526315789, 0.669290188679245, 0.882096637744035, 0.67114227886057, 0.507457641311933, 1.09130144665461, 2.14580438709677,  4.08386100217865, 5.09187647058824, 0.725805681818182, 1.00826231038506, 0.403857034795764, 0.577069146608315, 0.991765551601423, 1.89829438202247, 3.78251082024433, 5.79977216494845), avg_temp = c(16.0316901876571, 18.3219319089409, 17.4305069286691, 19.6999111947049, 18.0953880070785, 19.9364439125611, 15.8835964890026, 14.4286141669613, 15.8520719903777, 17.8671425490923, 16.9792535286348, 19.4983111172915, 18.0384627644668, 19.796581219498, 15.8923616798557, 14.3930096012531, 15.9462249963934,  18.0131168554757, 17.0146644246617, 19.3670299864879, 17.9620555512601, 19.9966781634639, 15.9588883573359, 14.3996170834378, 15.9102787695991, 17.9027418850032, 16.6426989587543, 19.3766862229857, 17.7782370211302, 19.9911596386168, 15.4336383745866, 14.4078720844153, 15.894660182529, 17.7701506997547, 16.5199154112426, 18.9558676499587, 17.8471091423835, 19.8014985569051, 15.7680670420329, 14.2370750045776, 14.8973653122079, 16.7578861254623, 16.0172205739373, 18.5551423442421, 17.7149749217095,  19.5071658581414, 15.4309343656536, 14.2014715330941, 15.3321192443371, 17.431807574597, 16.0510861331019, 17.5805371602376, 17.1988430109369, 19.3268852720455, 14.6497817119407, 14.2654214700063, 15.7534859397195, 17.3414096758347, 15.8644088745117, 17.6910490946571, 17.2494401363226, 19.6997255164699, 15.137482019152, 14.3955909729004, 15.6570347332564, 17.6080672569995, 15.7628102178159, 17.3501061742956, 17.3945586058549, 19.6723900415427, 14.9409176138944, 14.3039927202112, 15.2939747463573,  17.5678617249674, 15.6416156734277, 17.5087952361387, 17.540857042871, 19.5979144995191, 15.673808556731, 14.3753153392247, 16.0425170696143, 17.4133093886905, 15.6507201670588, 18.0531937029627, 17.6412752739826, 19.72914738925, 15.6248681115808, 14.2064579273092, 15.5793134126087, 17.1109770460082, 16.1793584239726, 18.0646989207519, 17.6148197809206, 19.7433430785177, 15.1988610282658, 14.2541516010578, 15.248302409898, 17.178797357865, 16.6500379143692, 18.2335877908084, 17.8113581964621, 19.8184388639398,  15.7196363848937, 14.2767914387218, 15.8123910992132, 17.1790603231023, 16.9620295138326, 18.590006396645, 18.0490843963623, 20.0932535064617, 16.4582798283275, 14.4041636751053, 16.3688073278072, 16.4052807274511, 17.343876319203, 19.1630856955051, 18.4148493790765, 20.299976194322, 16.3718331953156, 14.4724797074513, 16.3307339011646, 16.401790041123, 17.6226145170068, 19.5913128993587, 18.4194232483418, 20.4276151788949, 16.053676228693, 14.5162285099859, 16.3487099634338, 16.5845499682527, 17.5119909567519,  19.6658194174915, 18.5797102949749, 20.5213436422279, 14.9494875146121, 14.4916801568938, 17.3763293075562, 18.8124839826064, 18.1155957875552, 20.5523534040341, 18.9863680233112, 20.2543153991242, 15.3072674700603, 14.4353803103087, 16.7469552876998, 18.8632311047734, 18.1694983851407, 20.6087210666858, 18.8351857963118, 19.6952620882663, 15.4738927533864, 14.4828521996214, 16.0209554838098, 17.2570463307314, 17.534380493001, 19.6371749353609, 18.6765488419148, 19.9238736603665, 15.0897291594022,  14.5432929420471, 15.2783992807916, 16.8539676217472, 17.2031726410595, 19.0886267360606, 18.8249777717981, 20.1279337541112, 15.3480841190971, 14.440301252448, 15.7159928014456, 18.0787125337124, 17.3524328821428, 20.0888744604849, 18.6865335841586, 20.0968832655129, 15.2440366946192, 14.3961863768728, 15.8580124621121, 18.3873531296041, 17.4143823683709, 19.9601666363319, 18.5878530787444, 19.9652748415547, 15.4271978095726, 14.4166956228368, 15.9985944682902, 18.3746790468623, 17.6779030137632,  19.7467874468547, 18.2866756479935, 20.0260849490673, 15.6337777848435, 14.3993138480432)), class = c("grouped_df", "tbl_df", "tbl", "data.frame"), row.names = c(NA, -192L), groups = structure(list(hour = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L,  9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L,  22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L, 23L, 23L, 23L), month = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L,  5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L), .Label = c("May", "June", "July", "August", "September", "October", "November", "December"), class = "factor"), .rows = structure(list(     1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 29L, 30L, 31L, 32L, 33L, 34L, 35L, 36L, 37L, 38L, 39L, 40L, 41L, 42L, 43L, 44L, 45L, 46L, 47L, 48L, 49L, 50L, 51L, 52L, 53L, 54L, 55L, 56L, 57L, 58L, 59L, 60L, 61L, 62L, 63L, 64L, 65L, 66L, 67L, 68L, 69L, 70L, 71L, 72L, 73L, 74L, 75L, 76L, 77L, 78L, 79L, 80L, 81L, 82L, 83L, 84L, 85L, 86L, 87L, 88L, 89L, 90L, 91L, 92L, 93L, 94L, 95L, 96L, 97L, 98L, 99L, 100L, 101L,      102L, 103L, 104L, 105L, 106L, 107L, 108L, 109L, 110L, 111L, 112L, 113L, 114L, 115L, 116L, 117L, 118L, 119L, 120L, 121L, 122L, 123L, 124L, 125L, 126L, 127L, 128L, 129L, 130L, 131L, 132L, 133L, 134L, 135L, 136L, 137L, 138L, 139L, 140L, 141L, 142L, 143L, 144L, 145L, 146L, 147L, 148L, 149L, 150L, 151L, 152L, 153L, 154L, 155L, 156L, 157L, 158L, 159L, 160L, 161L, 162L, 163L, 164L, 165L, 166L, 167L, 168L, 169L, 170L, 171L, 172L, 173L, 174L, 175L, 176L, 177L, 178L, 179L, 180L, 181L, 182L, 183L, 184L,      185L, 186L, 187L, 188L, 189L, 190L, 191L, 192L), ptype = integer(0), class = c("vctrs_list_of", "vctrs_vctr", "list"))), row.names = c(NA, -192L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))

CodePudding user response:

A few things to make this happen:

  1. We can reverse the formula in sec_axis;
  2. I think we need not just a coefficient, also an intercept;
  3. Since the primary axis is inverted, we also need to invert the secondary data itself (so that the reversed-reversal is upright).

I'm going to suggest using scales::rescale to scale the avg_temp to the same range as avg_depth, and then invert it (max(.) - .). (I also suggest coloring the axis labels as well, to further disambiguate which axes apply to which line/data.)

library(dplyr) # assuming based on the data, but this can easily be done without dplyr
dat2 <- dat %>%
  ungroup() %>% # important, as rescaling within each month/hour will skew things
  mutate(
    avg_temp_adj = scales::rescale(avg_temp, to = rev(range(avg_depth)))
  )

coefs <- c(
  diff(range(dat$avg_temp)) / diff(range(dat$avg_depth)),
  min(dat$avg_temp) - min(dat$avg_depth)
)

ggplot(dat2, aes(x = hour))  
  facet_wrap(~ month)  
  geom_line(aes(y = avg_depth), color = "black")  
  geom_line(aes(y = avg_temp_adj), color = "red")  
  scale_y_continuous(
    trans = "reverse", name = "depth m",
    breaks = seq(0, 7.5, 1),
    sec.axis = sec_axis(~rev(.*coefs[1]   coefs[2]), name = "temp c", breaks = seq(14,22,1))
  )  
  theme_bw()  
  theme(
    axis.title.y = element_text(color = "black", size=13),
    axis.title.y.right = element_text(color = "red", size=13),
    axis.text.y.right = element_text(color = "red"))

ggplot2 with two axes, one flipped

  • Related