Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to format legend for dual axis plot in ggplot2

I have the following code in R to make a slightly complex dual axis plot using ggplot. My plot is almost complete, however I’m having an issue with my legend. I’ve noticed that ggplot creates two seperate legends if one combines two different geoms into one chart – is there a way in which I can merge the two legends into one? Also, my second legend has two entries in it and I only need the first one (Overall Balance (% of GDP, RHS)), is there a way to remove the second Overall Balance (N$ Million)?

dat <- structure(list(Category = c("Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", 
"Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", 
"Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", 
"Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", 
"Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", 
"Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", 
"Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", 
"Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", 
"Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", 
"Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", 
"Deficit Target (% of GDP, RHS)"), Year = c("2018/19", "2018/19", 
"2018/19", "2019/20", "2019/20", "2019/20", "2020/21", "2020/21", 
"2020/21", "2021/22 (est)", "2021/22 (est)", "2021/22 (est)", 
"2022/23 (proj)", "2022/23 (proj)", "2022/23 (proj)", "2023/24 (proj)", 
"2023/24 (proj)", "2023/24 (proj)", "2024/25 (proj)", "2024/25 (proj)", 
"2024/25 (proj)"), Value = c(-9226, -5.09893041491584, -3, -8918, 
-4.97862890962981, -3, -14197, -8.04324429255841, -3, -16138, 
-8.8, -3, -12658, -6.5, -3, -10418, -5, -3, -10281, -4.7, -3)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -21L))

library(tidyverse)

ggplot() +
  geom_col(data = subset(dat, Category %in% c("Overall Balance (N$ Million)")),
           mapping = aes(x = Year, y = Value, fill = Category, col = Category),
           position = position_dodge()) +
  geom_line(data = subset(dat, Category == "Overall Balance (% of GDP, RHS)"),
            mapping = aes(x = Year, y = (Value*1500), group = Category,color = Category), size = 2) +
  scale_fill_manual(values = c('#7c3042','#6d1d26')) +
  scale_color_manual(values = c('#c7af76','#c7af76')) +
  theme_light() +
  labs(x = '') +
  scale_y_continuous(breaks = seq(-22000,3000,by = 5000),
                     name = 'Overall Balance (N$ Million)',sec.axis= sec_axis(~./1500, name="Overall Balance (% of GDP)", breaks = seq(-10,0, by = 2))) +
  theme(legend.title = element_blank()) 

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This issue is independent of the secondary y-axis. The issue is that in geom_col() you map the colour aesthetic, whereas this layer should not get an entry in the colour legend (just the fill legend only). You can fix it by defining the colour outside the aes() mapping of the layer.

dat <- structure(list(Category = c("Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", 
                                   "Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", 
                                   "Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", 
                                   "Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", 
                                   "Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", 
                                   "Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", 
                                   "Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", 
                                   "Deficit Target (% of GDP, RHS)", "Overall Balance (N$ Million)", 
                                   "Overall Balance (% of GDP, RHS)", "Deficit Target (% of GDP, RHS)", 
                                   "Overall Balance (N$ Million)", "Overall Balance (% of GDP, RHS)", 
                                   "Deficit Target (% of GDP, RHS)"), Year = c("2018/19", "2018/19", 
                                                                               "2018/19", "2019/20", "2019/20", "2019/20", "2020/21", "2020/21", 
                                                                               "2020/21", "2021/22 (est)", "2021/22 (est)", "2021/22 (est)", 
                                                                               "2022/23 (proj)", "2022/23 (proj)", "2022/23 (proj)", "2023/24 (proj)", 
                                                                               "2023/24 (proj)", "2023/24 (proj)", "2024/25 (proj)", "2024/25 (proj)", 
                                                                               "2024/25 (proj)"), Value = c(-9226, -5.09893041491584, -3, -8918, 
                                                                                                            -4.97862890962981, -3, -14197, -8.04324429255841, -3, -16138, 
                                                                                                            -8.8, -3, -12658, -6.5, -3, -10418, -5, -3, -10281, -4.7, -3)), class = c("tbl_df", 
                                                                                                                                                                                      "tbl", "data.frame"), row.names = c(NA, -21L))

library(tidyverse)

ggplot() +
  geom_col(data = subset(dat, Category %in% c("Overall Balance (N$ Million)")),
           mapping = aes(x = Year, y = Value, fill = Category),
           colour = "#c7af76",
           position = position_dodge()) +
  geom_line(data = subset(dat, Category == "Overall Balance (% of GDP, RHS)"),
            mapping = aes(x = Year, y = (Value*1500), group = Category,color = Category), size = 2) +
  scale_fill_manual(values = c('#7c3042','#6d1d26')) +
  scale_color_manual(values = c('#c7af76','#c7af76')) +
  theme_light() +
  labs(x = '') +
  scale_y_continuous(breaks = seq(-22000,3000,by = 5000),
                     name = 'Overall Balance (N$ Million)',sec.axis= sec_axis(~./1500, name="Overall Balance (% of GDP)", breaks = seq(-10,0, by = 2))) +
  theme(legend.title = element_blank()) 

Created on 2022-10-13 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading