0

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

Maria
  • 173
  • 7
  • Have you tried +guides(fill = 'none')? – NickCHK Jan 24 '23 at 08:50
  • I get this error `Error in as.unit(e2) : object is not coercible to a unit` – Maria Jan 24 '23 at 08:55
  • I don't think I can help further without seeing the actual graph code/data itself. I don't think the purple background in the legend there can be removed with the theme() function, it would be elsewhere in the code. – NickCHK Jan 24 '23 at 09:05
  • Alternately, a potential fix here is to specify fill = 'purple' (or whatever color that is) *outside* of an aes() function, in your geom_bar()/geom_col() call directly, rather than fill = varname inside of an aes() function. This will also keep the fill color out of your legend. – NickCHK Jan 24 '23 at 09:08
  • I've just updated my code in the description. – Maria Jan 24 '23 at 09:13
  • I've tried what you said, but it isn't working either. – Maria Jan 24 '23 at 09:29

2 Answers2

1

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

NickCHK
  • 1,093
  • 7
  • 17
  • I'm getting this error `Error: Insufficient values in manual scale. 1 needed but only 0 provided.` – Maria Jan 24 '23 at 09:36
  • Hmm, odd, I don't get that error when running the code. Also, both manual scales have 1 value provided. – NickCHK Jan 24 '23 at 22:42
1

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

stefan
  • 90,330
  • 6
  • 25
  • 51