简体   繁体   中英

Rescale y-axis with geom_col and position=fill in ggplot2 to combine with line plot

I am trying to overlay a line plot and column plot. I am using geom_col with position=fill , such that its y-axis goes from 0 to 1. However, the line plot's y-scale is quite different (approx. 20-50) so when combined the column plot is practically invisible, since it's automatically scaled to max of 1:

在此处输入图像描述

Following this approach , I can scale the line by its max value:

geom_line(aes(x = year, y = total_rate/max(total_rate)))

and add a secondary y-axis similarly scaled by its max value:

scale_y_continuous(sec.axis = sec_axis(~ . *max(total_rate)))

such that the geom_col correctly fills the space and the line is correct relative to the secondary axis:

在此处输入图像描述

Seems good, right? but I'd like to solve the problem a different way. Specifically, instead of scaling down the line, I'd like to scale up the columns, such that they'd fill up to the max of total_rate , the left y-axis would show 0-40+, and there would be no right y-axis.

Two reasons for that: 1) The scale on geom_col with position=fill is intuitive and you don't need to explicitly mark 0.00-1.00 for it, so it would be visually cleaner and no less understandable; and 2) I'm trying to work with geofacet and it doesn't work well with secondary y-axes as far as I've figured out.

So, is there a way to rescale geom_col with position=fill based on max(total_rate) , similar to (but inverse of) what I'm doing with geom_line above?

EDIT: Data used for above plots:

year = 2010 2010 2010 2010 2010 2011 2011 2011 2011 2011 2011 2012 2012 2012 2012 2012 2012 2013 2013 2013 2013 2013 2013 2014 2014 2014 2014 2014 2014 2015 2015 2015 2015 2015 2015 2016 2016 2016 2016 2016 2016 2017 2017 2017 2017 2017 2017 2018 2018 2018 2018 2018 2018 2019 2019 2019 2019 2019 2019

group = 1 2 3 4 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 1 2 3 4 5 6 

count = 183  188  221   67   67  201  244  204  141    3   62  160  187  199  244    2   64  197  303  217  272   13   88  143  204  162  313  137  305  174  129  149  314  310  647  220   80  155  191  429 1123  242   37  120   94  366 1279  242    8  130   45  439 1374  238   13   74   14  363 1513

total_rate = 11.05644 11.05644 11.05644 11.05644 11.05644 12.92794 12.92794 12.92794 12.92794 12.92794 12.92794 12.84706 12.84706 12.84706 12.84706 12.84706 12.84706 16.23639 16.23639 16.23639 16.23639 16.23639 16.23639 18.69105 18.69105 18.69105 18.69105 18.69105 18.69105 25.35976 25.35976 25.35976 25.35976 25.35976 25.35976 32.21170 32.21170 32.21170 32.21170 32.21170 32.21170 31.16714 31.16714 31.16714 31.16714 31.16714 31.16714 32.51662 32.51662 32.51662 32.51662 32.51662 32.51662 32.13637 32.13637 32.13637 32.13637 32.13637 32.13637

Might be simplest to calculate the scaled heights before ggplot:

df1 <- data.frame(year, group, count, total_rate)

library(dplyr)
df1 %>%
  group_by(year) %>%
  mutate(scaled_count = 34 * count / sum(count)) %>%
  ungroup() %>%
  
  ggplot(aes(year, scaled_count, fill = as.character(group))) +
  geom_col() +
  geom_line(aes(y = total_rate))

在此处输入图像描述


data used

year = c(2010L, 2010L, 2010L, 2010L, 2010L, 2011L, 2011L, 2011L, 2011L, 2011L, 2011L, 2012L, 2012L, 2012L, 2012L, 2012L, 2012L, 2013L, 2013L, 2013L, 2013L, 2013L, 2013L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2015L, 2015L, 2015L, 2015L, 2015L, 2015L, 2016L, 2016L, 2016L, 2016L, 2016L, 2016L, 2017L, 2017L, 2017L, 2017L, 2017L, 2017L, 2018L, 2018L, 2018L, 2018L, 2018L, 2018L, 2019L, 2019L, 2019L, 2019L, 2019L, 2019L)

group = c(1L, 2L, 3L, 4L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L, 1L, 2L, 3L, 4L, 5L, 6L)

count = c(183L, 188L, 221L, 67L, 67L, 201L, 244L, 204L, 141L, 3L, 62L, 160L, 187L, 199L, 244L, 2L, 64L, 197L, 303L, 217L, 272L, 13L, 88L, 143L, 204L, 162L, 313L, 137L, 305L, 174L, 129L, 149L, 314L, 310L, 647L, 220L, 80L, 155L, 191L, 429L, 1123L, 242L, 37L, 120L, 94L, 366L, 1279L, 242L, 8L, 130L, 45L, 439L, 1374L, 238L, 13L, 74L, 14L, 363L, 1513L)

total_rate = c(11.05644, 11.05644, 11.05644, 11.05644, 11.05644, 12.92794, 12.92794, 12.92794, 12.92794, 12.92794, 12.92794, 12.84706, 12.84706, 12.84706, 12.84706, 12.84706, 12.84706, 16.23639, 16.23639, 16.23639, 16.23639, 16.23639, 16.23639, 18.69105, 18.69105, 18.69105, 18.69105, 18.69105, 18.69105, 25.35976, 25.35976, 25.35976, 25.35976, 25.35976, 25.35976, 32.2117, 32.2117, 32.2117, 32.2117, 32.2117, 32.2117, 31.16714, 31.16714, 31.16714, 31.16714, 31.16714, 31.16714, 32.51662, 32.51662, 32.51662, 32.51662, 32.51662, 32.51662, 32.13637, 32.13637, 32.13637, 32.13637, 32.13637, 32.13637)

df1 <- data.frame(year, group, count, total_rate)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM