nhs-r-community / demos-and-how-tos

A repo for community contributed demos and how-tos to get common stuff done in the R language
https://nhs-r-community.github.io/demos-and-how-tos/
MIT License
29 stars 13 forks source link

3 axis on a plot #52

Closed Lextuga007 closed 1 year ago

Lextuga007 commented 1 year ago

Question posed on the Slack. Code was suggested but it also led to a question of when this can be misleading:

mtcars |>
  dplyr::mutate(ID = seq_along(mpg), .before = 1) |>
  ggplot2::ggplot() +
  ggplot2::geom_point(ggplot2::aes(x = ID, y = mpg, 
                                   col = "Miles/(US) gallon")) +
  ggplot2::geom_point(ggplot2::aes(x = ID, y = hp / 10, 
                                   col = "Gross horsepower")) +
  ggplot2::scale_y_continuous(
    name = "Miles/(US) gallon",
    breaks = scales::pretty_breaks(10),
    sec.axis = ggplot2::sec_axis(~.*10,
                                 name = "Gross horsepower",
                                 breaks = scales::pretty_breaks(10))
  ) +
  ggplot2::scale_colour_manual(name = NULL, 
                               values = c("Miles/(US) gallon" = "#CC2200",
                                          "Gross horsepower" = "black")
  )

Just note that in my example code, I “scaled” the axis by dividing the original values of the 2ndary variable by 10, ggplot2::aes(x = ID, y = hp / 10, then I multiplied the scale by 10 (so the axis labels have the original scale), ggplot2::sec_axis(~.*10. Try removing those, so you will see the difference and try playing with different scaling values.


If you mean 2 y-axes it's possible now https://ggplot2.tidyverse.org/reference/sec_axis.html but I'd caution using it. I had a chart I showed people for years with 2 axes comparing the Trust deaths with the Region to show the seasonal pattern of deaths were the same - but not comparing numbers. I always mentioned it in meetings as it looked like the numbers of Trust deaths were higher than Region - which is impossible as we are part of the region. Turned out it had was still being read like that.

Same data but different types of charts: https://blog.datawrapper.de/dualaxis/

ChrisBeeley commented 1 year ago

I don't really think we should have this on here to be honest. This blog post convinced me that they're just best avoided entirely https://blog.datawrapper.de/dualaxis/

Lextuga007 commented 1 year ago

Yeah, I agree it's not something we should do but people do so I would like something, somewhere saying it's a thing but it's a thing you really should consider twice about doing. It's possibly better as a blog?

ChrisBeeley commented 1 year ago

Yes, a blog would be better

On Thu, Feb 23, 2023 at 9:14 AM Zoë Turner @.***> wrote:

Yeah, I agree it's not something we should do but people do so I would like something, somewhere saying it's a thing but it's a thing you really should consider twice about doing. It's possibly better as a blog?

— Reply to this email directly, view it on GitHub https://github.com/nhs-r-community/demos-and-how-tos/issues/52#issuecomment-1441420089, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJTSW5B6DJOGPI3VCOB7O3WY4TBBANCNFSM6AAAAAAVETXOXY . You are receiving this because you commented.Message ID: @.***>

Lextuga007 commented 1 year ago

Done https://nhsrcommunity.com/code-snippets-2-scale-y-axes-in-ggplot2/ and the code used to produce the html which was copied to the website https://github.com/nhs-r-community/NHSRblogs/blob/main/20230328_3-axes-ggplot/3-axes-ggplot.qmd