05 May 2021

Posted on:

26 Apr 2021

0

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

Hello,

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

The code shows..
------------------------
library(ggplot2)

combo <- ggplot(df,
aes(x=Year, y=Participants, Python.Users)) +
geom_bar(aes(y=Participants),
stat='identity',
fill = 'darkblue') +
geom_line(aes(y=Python.Users*max(Participants)),
stat='identity',
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)")

combo
----------------------

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.

Posted on:

05 May 2021

0

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!

Best,
365 Eli