0

I’m trying to plot 3 variables on a line chart in ggplot. I want two y axes. The y axis on the left is in Million Metric Tons(MMT) and the y axis on the right is in %. The variables in the dataframe are already scaled. The percentage variable is called STURatio. The Year variable is annual. I’m unable to get the scaling for the y axis on the right to be in percentage terms.

WorldSupplyDemand %>%
 select(Year,`TotalDisappearance(MMT)`, `TotalSupply(MMT)`, STURatio) %>%
 ggplot(aes(x = Year)) +
 geom_line(mapping = aes(y = `TotalDisappearance(MMT)`), color = "darkred") +
 geom_line(mapping = aes(y = `TotalSupply(MMT)`), color = "steelblue") +
 geom_line(mapping = aes(x = Year, y = STURatio)) +
 scale_y_continuous(name = "MMT",
           sec.axis = sec_axis(~., name = "Stocks to Use Ratio (%)"))

The data:

 structure(list(Year = c(1960, 1961, 1962, 1963, 1964, 1965, 1966, 
1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 
1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 
1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 
2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 
2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, 2020), 
    `TotalDisappearance(MMT)` = c(274.737, 280.09, 287.011, 294.171, 
    311.628, 338.123, 332.167, 335.457, 350.395, 377.606, 386.024, 
    391.464, 416.603, 424.197, 418.174, 414.172, 437.549, 462.898, 
    490.064, 517.54, 533.804, 545.381, 556.189, 570.596, 589.929, 
    567.224, 600.87, 642.332, 624.454, 634.695, 658.341, 662.985, 
    656.96, 656.34, 640.078, 644.83, 679.626, 681.05, 680.41, 
    701.107, 687.003, 692.408, 709.196, 697.405, 717.313, 739.369, 
    728.036, 733.634, 787.318, 790.928, 788.569, 856.142, 818.352, 
    864.356, 869.573, 888.485, 921.807, 924.405, 908.781, 935.588, 
    939.629), `TotalSupply(MMT)` = c(357.586, 349.94, 362.857, 
    364.498, 390.107, 398.857, 419.789, 433.121, 471.706, 481.149, 
    466.553, 480.708, 491.531, 506.864, 499.527, 500.891, 564.901, 
    572.105, 624.912, 637.993, 646.446, 658.013, 686.32, 716.376, 
    758.334, 745.659, 791.752, 801.121, 759.204, 771.279, 829.225, 
    824.269, 833.691, 839.003, 803.999, 800.616, 844.015, 878.965, 
    889.704, 910.179, 893.054, 895.705, 878.305, 833.317, 873.74, 
    892.649, 861.527, 862.109, 957.38, 995.006, 987.78, 1055.56, 
    997.886, 1062.059, 1092.333, 1133.479, 1184.795, 1208.511, 
    1188.582, 1232.708, 1254.469), STURatio = c(30.1557489526347, 
    24.9384126530758, 26.4261648508245, 23.9068432986256, 25.1835521840143, 
    17.9621025484809, 26.3788997702963, 29.1137165121014, 34.6212132022432, 
    27.4209096253767, 20.8611381675751, 22.7974986205628, 17.9854681795378, 
    19.4878794522356, 19.4543419724804, 20.9379195117004, 29.1057687253313, 
    23.5920224325877, 27.5164060204382, 23.2741430614059, 21.1017527032394, 
    20.6519845759203, 23.3969028513689, 25.5487244915842, 28.5466556144892, 
    31.4575899468288, 31.7676036413867, 24.7207051804986, 21.5788512844821, 
    21.5196275376362, 25.9567610098718, 24.3269455568376, 26.9013334145153, 
    27.830545144285, 25.6095350879112, 24.1592357675666, 24.1881564272114, 
    29.0602745760223, 30.7599829514557, 29.8202699445306, 29.9927365673803, 
    29.3608681586579, 23.8451711515575, 19.4882457108854, 21.8073560635315, 
    20.7311910561573, 18.3357691103187, 17.5121382051541, 21.6001666416874, 
    25.8023486335039, 25.2623422934455, 23.2926313625543, 21.9384812403465, 
    22.8728671982378, 25.617170726322, 27.5743540971429, 28.5296162862725, 
    30.7339315559738, 30.7886058357294, 31.757568502375, 33.5068415300081
    )), row.names = c(NA, -61L), class = "data.frame")
gm007
  • 547
  • 4
  • 11
  • Can you make your code reproducible? See guidelines [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Also, have you checked [this](https://stackoverflow.com/questions/3099219/ggplot-with-2-y-axes-on-each-side-and-different-scales) post and [this](https://www.r-graph-gallery.com/line-chart-dual-Y-axis-ggplot2.html) website? Also consider that it is not a good idea to have 2 y axis as it often mislead conclusions, in fact `ggplot` is "designed" to avoid you doing this ^^ – Paul Aug 11 '20 at 11:29
  • Thanks but this is not usable, posted like this it is not reproducible. Could you use `dput()` for example? Some other ways are also mentioned in the link above. – Paul Aug 11 '20 at 11:34
  • 1
    I've updated the data with dput() – gm007 Aug 11 '20 at 11:36

1 Answers1

1

Within your sex.axis, use labels = scales::label_percent().

 sec.axis = sec_axis(~., name = "Stocks to Use Ratio (%)", 
                     labels = scales::label_percent())
Ben Norris
  • 5,639
  • 2
  • 6
  • 15