在两个y轴上的条形图和折线图中添加图例

时间:2020-08-11 12:44:52

标签: r ggplot2 data-visualization

我绘制了带有两个y轴的条形图和折线图。我正在努力为条形图添加图例。我缩放了第二个y轴,并将比例转换为百分比。我不确定将条形图包含在图例中需要更改什么。另外,我希望从图例名称中删除“颜色”一词。

stockscol <- "#69b3a2"
STUColor <- rgb(0.2, 0.6, 0.9, 1)
WorldSupplyDemand %>%
  select(Year, `EndingStocks(MMT)`, STURatio) %>%
  ggplot() +
  geom_bar(aes(x = Year, y =`EndingStocks(MMT)`), stat = "identity", fill = stockscol, color = "black", alpha = .4)+
  geom_line(aes(x = Year, y = STURatio*max(WorldSupplyDemand$`EndingStocks(MMT)`), color = "Stocks-To-Use Ratio"), stat = "identity") +
  scale_y_continuous(name= "Ending Stocks (MMT)",
                     sec.axis = sec_axis(trans = ~./max(WorldSupplyDemand$`EndingStocks(MMT)`), name = "Stocks-To-Use Ratio (%)",
                                         labels = scales::label_percent())) +
  scale_x_continuous(breaks = seq(1960, 2020, 10)) +
  theme_linedraw() +
  theme(legend.position = "bottom")

数据:

    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), 
    `EndingStocks(MMT)` = c(82.849, 69.85, 75.846, 70.327, 78.479, 
    60.734, 87.622, 97.664, 121.311, 103.543, 80.529, 89.244, 
    74.928, 82.667, 81.353, 86.719, 127.352, 109.207, 134.848, 
    120.453, 112.642, 112.632, 130.131, 145.78, 168.405, 178.435, 
    190.882, 158.789, 134.75, 136.584, 170.884, 161.284, 176.731, 
    182.663, 163.921, 155.786, 164.389, 197.915, 209.294, 209.072, 
    206.051, 203.297, 169.109, 135.912, 156.427, 153.28, 133.491, 
    128.475, 170.062, 204.078, 199.211, 199.418, 179.534, 197.703, 
    222.76, 244.994, 262.988, 284.106, 279.801, 297.12, 314.84
    ), STURatio = c(0.301557489526347, 0.249384126530758, 0.264261648508245, 
    0.239068432986256, 0.251835521840143, 0.179621025484809, 
    0.263788997702963, 0.291137165121014, 0.346212132022432, 
    0.274209096253767, 0.208611381675751, 0.227974986205628, 
    0.179854681795378, 0.194878794522356, 0.194543419724804, 
    0.209379195117005, 0.291057687253313, 0.235920224325877, 
    0.275164060204381, 0.232741430614059, 0.211017527032394, 
    0.206519845759203, 0.233969028513689, 0.255487244915842, 
    0.285466556144892, 0.314575899468288, 0.317676036413867, 
    0.247207051804986, 0.215788512844821, 0.215196275376362, 
    0.259567610098718, 0.243269455568376, 0.269013334145153, 
    0.27830545144285, 0.256095350879112, 0.241592357675666, 0.241881564272114, 
    0.290602745760223, 0.307599829514557, 0.298202699445306, 
    0.299927365673803, 0.293608681586579, 0.238451711515575, 
    0.194882457108854, 0.218073560635315, 0.207311910561573, 
    0.183357691103187, 0.175121382051541, 0.216001666416874, 
    0.258023486335039, 0.252623422934455, 0.232926313625543, 
    0.219384812403465, 0.228728671982378, 0.25617170726322, 0.275743540971429, 
    0.285296162862725, 0.307339315559738, 0.307886058357294, 
    0.31757568502375, 0.335068415300081)), row.names = c(NA, 
-61L), class = "data.frame")

1 个答案:

答案 0 :(得分:1)

我不知道您需要STUColor用于...

除此之外。您可以通过将fill内的aes设置为您喜欢的字符串来将其添加到图例中。该字符串将在图例中用作标签。

要设置fill,您需要使用scale_fill_manual。在value参数中,指定您在[Ending Stocks]之前设置的标签等于在这种情况下选择的[stockcol]的颜色。

只需设置""相应的实验室即可删除不需要的名称。

library(dplyr)
library(ggplot2)
stockscol <- "#69b3a2"
STUColor <- rgb(0.2, 0.6, 0.9, 1) # unused!

WorldSupplyDemand %>%
    select(Year, `EndingStocks(MMT)`, STURatio) %>%
    ggplot() +
    geom_bar (aes(x = Year, y =`EndingStocks(MMT)`, fill = "Ending Stocks"), stat = "identity", color = "black", alpha = .4)+
    geom_line(aes(x = Year, y = STURatio*max(WorldSupplyDemand$`EndingStocks(MMT)`), color = "Stocks-To-Use Ratio"), stat = "identity") +
    scale_y_continuous(name = "Ending Stocks (MMT)",
                       sec.axis = sec_axis(trans = ~./max(WorldSupplyDemand$`EndingStocks(MMT)`), name = "Stocks-To-Use Ratio (%)",
                                                                                labels = scales::label_percent())) +
    scale_x_continuous(breaks = seq(1960, 2020, 10)) +
    scale_fill_manual(values = c("Ending Stocks" = stockscol)) +
    theme_linedraw() +
    theme(legend.position = "bottom") +
    labs(colour = "", fill = "")

enter image description here