ggplot dual axis showing metrics on different scales

Some data:

webdata <- structure(list(date = structure(c(18628, 18629, 18630, 18631, 
18632), class = "Date"), sessions = c(9413.09630501196, 10444.2086439549, 
10364.6407811647, 10746.5345515247, 10354.2723230473), conversions = c(209.25779246474, 
282.459855790002, 111.936300864853, 145.978069189652, 300.705073793032
), conversion_rate = c(0.0222304952253937, 0.0270446393230081, 
0.0107998244443041, 0.0135837342251824, 0.0290416423685998)), class = "data.frame", row.names = c(NA, 
-5L))

Looks like this:

webdata
        date  sessions conversions conversion_rate
1 2021-01-01  9413.096    209.2578      0.02223050
2 2021-01-02 10444.209    282.4599      0.02704464
3 2021-01-03 10364.641    111.9363      0.01079982
4 2021-01-04 10746.535    145.9781      0.01358373
5 2021-01-05 10354.272    300.7051      0.02904164

I'd like a plot with sessions as columns using the left hand y axis and then a line for conversion rate using the right hand axis.

I can make the sessions column chart:

webdata %>% ggplot(aes(date, sessions)) +
  geom_col(fill = 'steelblue')

Looks like this:

Here's an example using GSheets of what I'd like to achieve with ggplot:

The blue columns represent sessions and are measured against the left hand y axis, while the red line represents conversion rate and is measured against the right side y axis.

Is this possible with ggplot? Either with ggplot directly or by transforming my data in some way?

You can scale your data

library(ggplot2)

webdata <- structure(list(date = structure(c(18628, 18629, 18630, 18631, 
                                             18632), class = "Date"), sessions = c(9413.09630501196, 10444.2086439549, 
                                                                                   10364.6407811647, 10746.5345515247, 10354.2723230473), conversions = c(209.25779246474, 
                                                                                                                                                          282.459855790002, 111.936300864853, 145.978069189652, 300.705073793032
                                                                                   ), conversion_rate = c(0.0222304952253937, 0.0270446393230081, 
                                                                                                          0.0107998244443041, 0.0135837342251824, 0.0290416423685998)), class = "data.frame", row.names = c(NA, 
                                                                                                                                                                                                            -5L))

ggplot(webdata, aes(date, sessions)) +
    geom_col(fill = 'steelblue') +
    geom_line(aes(y = conversion_rate * 4E5), color = "red") +
    scale_y_continuous(sec.axis = ~ . / 4E5)

Created on 2021-06-26 by the reprex package (v2.0.0)

Hey it works! Thank you. But I do not understand it. What does 4E5 do? You said you scaled it. How is this scaled?

If you multiply or divide your variable by a number you are changing the scale, I have chosen that value because it makes it look closer to your reference image.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.