Home > Back-end >  Remove legend background for geom_point in combined ggplot
Remove legend background for geom_point in combined ggplot

Time:01-25

I want the legend to only show the black point for Proporció sobre la despesa total, without the purple background.

Here a sample of my dataset:

structure(list(decils_renda = structure(1:10, .Label = c("1r", 
"2n", "3r", "4t", "5è", "6è", "7è", "8è", "9è", "10è"), class = "factor"), 
    despesatotal = c(2681.8, 3543.7, 3960.3, 4356.8, 4767.3, 
    5229.9, 5057.8, 5799.5, 6096.8, 7228.4), despesamonetaria = c(2654, 
    3516, 3918.1, 4308.8, 4722.8, 5160.9, 5027, 5746.5, 6068.3, 
    7181.6), proporcio_despesatotal = c(18.6, 19, 18.4, 17.8, 
    17.1, 17.2, 16.6, 16, 15.2, 13.4), proporcio_despesamonetaria = c(26.7, 
    26.2, 24.2, 23.4, 21.9, 21.5, 20.9, 20.1, 19.2, 17.1), proporcio_despesatotal_ingressos = c(32.7, 
    26.1, 23.4, 20.1, 18.8, 17.8, 14.7, 15.2, 13.2, 10.4), proporcio_despesamonetaria_ingressos = c(32.3, 
    25.9, 23.2, 19.9, 18.6, 17.6, 14.6, 15.1, 13.1, 10.4)), row.names = c(NA, 
-10L), class = c("tbl_df", "tbl", "data.frame"))

This is the code I used for the graph:

plotalimentacio_2021_decils <- ggplot(alimentacio_2021_decils)  
  geom_col(aes(fct_inorder(decils_renda), despesamonetaria, color="despesamonetaria"), fill="#9A6584", width=0.5, alpha=1)                  
  geom_point(aes(fct_inorder(decils_renda), proporcio_despesamonetaria_ingressos*100, color="Proporció sobre els ingressos anuals"), size=3)  
  scale_color_manual(name="", labels=c("Despesa monetària", "Proporció sobre els ingressos anuals (eix dret)"), values=c("#9a6584","#000000"))  
  scale_y_continuous(labels=function(x) format(x, big.mark = ".", scientific = FALSE), breaks=seq(from=0, to=8000, by=1000),
                     expand = expansion(mult = c(0, .05)), 
                     sec.axis=sec_axis(~ ./100 ,  breaks = seq(0, 100, by = 10), name = "% sobre el total d'ingressos"))    
  geom_hline(yintercept=0, color="black") 
  labs(
    title="Despesa en alimentació per decils de renda a Espanya (2021)",
    subtitle="En euros i proporció sobre els ingressos anuals",
    x = "Decils de renda", 
    y="",
    caption="Font: Elaboració pròpia a partir de dades de l'Enquesta de pressupostos familiars del 2021, base 2016, de l'INE.")  
  theme(
    legend.position = "bottom",
    legend.justification = "left",
    legend.direction="horizontal",
    legend.margin=margin(t=-25),
    legend.text=element_text(size=12),
    legend.box.margin = margin(0,1,0.5,-1,"cm"))

enter image description here

CodePudding user response:

The issue is that you're trying to make a single legend work for both fill and color elements but without wanting to combine them. The solution is to have two separate legends, one for fill and one for color.

ggplot(alimentacio_2021_decils)  
# Notice I've gotten rid of color for the geom_col
  geom_col(aes(fct_inorder(decils_renda), despesamonetaria, fill = 'despesamonetaria'), width=0.5, alpha=1)                 
  geom_point(aes(fct_inorder(decils_renda), proporcio_despesamonetaria_ingressos*100, color="Proporció sobre els ingressos anuals"), size=3) 
# I've added scale_fill_manual just referring to the bar
  scale_fill_manual(name = '', labels = "Despesa monetària", values = "#9a6584")  
# and scale_color_manual is edited to just refer to the point
  scale_color_manual(name="", labels="Proporció sobre els ingressos anuals (eix dret)", values="#000000")  
  scale_y_continuous(labels=function(x) format(x, big.mark = ".", scientific = FALSE), breaks=seq(from=0, to=8000, by=1000),
                     expand = expansion(mult = c(0, .05)), 
                     sec.axis=sec_axis(~ ./100 ,  breaks = seq(0, 100, by = 10), name = "% sobre el total d'ingressos"))    
  geom_hline(yintercept=0, color="black") 
  labs(
    title="Despesa en alimentació per decils de renda a Espanya (2021)",
    subtitle="En euros i proporció sobre els ingressos anuals",
    x = "Decils de renda", 
    y="",
    caption="Font: Elaboració pròpia a partir de dades de l'Enquesta de pressupostos familiars del 2021, base 2016, de l'INE.")  
  theme(
    legend.position = "bottom",
    legend.justification = "left",
    legend.direction="horizontal",
    legend.margin=margin(t=-25),
    legend.text=element_text(size=12),
    legend.box.margin = margin(0,1,0.5,-1,"cm"))

The code example you gave causes the legend to overlap with the x-axis title, and I didn't change that, but you can see here the legend looks right

Graph with two separate legends, one for fill and one for color

CodePudding user response:

One option to fix your issue would be to use the override.aes argument of guide_legend to get rid of the fill for the "black" category using

guides(color = guide_legend(override.aes = list(fill = c("#9a6584", NA)))

Additionally, in the code below I removed the outline around the legend key using linetype="blank" and finally removed the fill color for the legend key background via theme options.

library(ggplot2)
library(forcats)

ggplot(alimentacio_2021_decils)  
  geom_col(aes(fct_inorder(decils_renda), despesamonetaria, color = "despesamonetaria"),
    fill = "#9A6584", width = 0.5, alpha = 1
  )  
  geom_point(aes(fct_inorder(decils_renda), proporcio_despesamonetaria_ingressos * 100,
    color = "Proporció sobre els ingressos anuals"
  ), size = 3)  
  scale_color_manual(
    name = "", labels = c("Despesa monetària", "Proporció sobre els ingressos anuals (eix dret)"),
    values = c("#9a6584", "#000000")
  )  
  scale_y_continuous(
    labels = scales::label_number(big.mark = ".", decimal.mark = ","),
    breaks = seq(from = 0, to = 8000, by = 1000),
    expand = expansion(mult = c(0, .05)),
    sec.axis = sec_axis(~ . / 100, breaks = seq(0, 100, by = 10), name = "% sobre el total d'ingressos")
  )  
  geom_hline(yintercept = 0, color = "black")  
  labs(
    title = "Despesa en alimentació per decils de renda a Espanya (2021)",
    subtitle = "En euros i proporció sobre els ingressos anuals",
    x = "Decils de renda",
    y = "",
    caption = "Font: Elaboració pròpia a partir de dades de l'Enquesta de pressupostos familiars del 2021, base 2016, de l'INE."
  )  
  theme(
    legend.position = "bottom",
    legend.justification = "left",
    legend.direction = "horizontal",
    legend.key = element_rect(fill = NA),
    # legend.margin = margin(t = -25),
    legend.text = element_text(size = 12),
    # legend.box.margin = margin(0, 1, 0.5, 0, "cm")
  )  
  guides(color = guide_legend(override.aes = list(fill = c("#9a6584", NA), linetype = "blank")))

enter image description here

  • Related