Last answered:

05 May 2021

Posted on:

26 Apr 2021


secondary axis in R, sec_axis(~./max(df$Participants)


I have a question regarding plotting a secondary axis in R.

The code shows..

df <- read.csv("bar_line_chart_data.csv")

combo <- ggplot(df,
                aes(x=Year, y=Participants, Python.Users)) +
                  fill = 'darkblue') +
                   color = 'red', size = 2) +
         scale_y_continuous(sec.axis = sec_axis(~./max(df$Participants)*100,
                            name="Python Users in %")) +
         ggtitle("KD Nuggets Survey Python Users (2012-2019)")


From this code, I'd like to know what ~./max means and why you used df$Participants for the parameter.
It seems strange that df$Participants is there instead of df$Python.Users.

Thanks in advance!

1 answers ( 0 marked as helpful)
Posted on:

05 May 2021


Hi Minhee,
thanks for reaching out!
The particular line of code is to rescale the secondary Y axis and to be able to represent it in percentages.
The bar chart which show Participants has values in the 3000, whereas the secondary Y axis of Python Users takes values between 0 and 100 - as it shows percentages.
So we're rescaling the secondary Y axis to have the same magnitude, as the primary y-axis or the participants. That's why we're taking the max value of the Participants column and we multiply by 100 to get the percentages.
That's somewhat complicated in terms of code and logic - there might well be an easier way to achieve this in R and ggplot, this is just the way I'm used to creating this type of chart.
Hope this helps!

365 Eli

Submit an answer