Home > Enterprise >  How do I categorize legend labels in ggplot?
How do I categorize legend labels in ggplot?

Time:04-22

My legend currently looks like this: enter image description here because I manually assigned colors to each bar. I made 2021 yellow and 2022 blue, but I want the legend to simply show:

Years

(yellow icon) 2021

(blue icon) 2022

I've tried googling everywhere but I can't find the function for ggplot. How do I manually categorize the legend labels in ggplot?

This is my code:

ggplot(dataset3, aes(x= datum, y = transacties, fill = datum))  
     geom_bar(stat="identity")  
        scale_fill_manual(values=c("2021-04" = "#FFBD33",
                                   "2021-05" = "#FFBD33", 
                                   "2021-06" = "#FFBD33",
                                "2021-07" = "#FFBD33",
                                "2021-08" = "#FFBD33",
                                "2021-09" = "#FFBD33",
                                "2021-10" = "#FFBD33",
                                "2021-11" = "#FFBD33",
                                "2021-12" = "#FFBD33",
                                "2022-01" = "#203269",
                                "2022-02" = "#203269",
                                "2022-03" = "#203269",
                                "2022-04" = "#203269"))

This is my data:

dput (dataset3)
structure(list(transacties = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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-09", "2022-03", "2022-03", "2021-11", "2021-08", 
"2021-06", "2022-03", "2021-06", "2022-04", "2021-12", "2021-08", 
"2021-09", "2021-11", "2021-09", "2022-04", "2021-05", "2021-07", 
"2021-11", "2022-01", "2021-10", "2022-03", "2022-04", "2022-02", 
"2021-12", "2021-11", "2021-12", "2021-11", "2021-08", "2022-02", 
"2022-02", "2021-07", "2021-11", "2022-01", "2022-03", "2021-10", 
"2021-10", "2021-09", "2021-10", "2022-04", "2022-01", "2022-03", 
"2021-11", "2021-09", "2021-09", "2021-07", "2021-10", "2021-08", 
"2022-03", "2022-02", "2021-12", "2021-12", "2022-01", "2021-12", 
"2022-04", "2022-01", "2022-03", "2022-03", "2021-06", "2021-09", 
"2021-12", "2021-06", "2021-05", "2021-06", "2021-07", "2021-12", 
"2021-12", "2021-06", "2021-08", "2021-05", "2022-02", "2021-12", 
"2021-10", "2021-07", "2022-01", "2021-09", "2021-09", "2021-09", 
"2021-12", "2021-05", "2021-05", "2021-10", "2021-05", "2021-11", 
"2022-01", "2021-10", "2021-05", "2021-11", "2022-01", "2021-11", 
"2022-03", "2021-11", "2021-10", "2022-02", "2022-02", "2021-10", 
"2021-11", "2021-05", "2021-09", "2021-08", "2022-04", "2021-08", 
"2021-11", "2021-09", "2022-01", "2022-01", "2021-04", "2022-03", 
"2021-12", "2022-03", "2022-03", "2021-05", "2021-06", "2022-03", 
"2021-06", "2021-09", "2022-01", "2021-08", "2021-07", "2022-02", 
"2021-08", "2021-06", "2022-04", "2022-02", "2021-07", "2021-10", 
"2021-10", "2021-10", "2021-08", "2022-03", "2021-07", "2021-11", 
"2021-11", "2022-02", "2022-04", "2021-08", "2021-10", "2021-11", 
"2021-10", "2022-04", "2021-08", "2022-02", "2022-01", "2021-12", 
"2021-12", "2021-05", "2021-04", "2021-09", "2022-02", "2022-04", 
"2021-11", "2021-06", "2021-10", "2021-12", "2021-11", "2021-05", 
"2022-01", "2021-05", "2022-01", "2022-03", "2022-04", "2021-06", 
"2021-09", "2021-06", "2021-08", "2021-10", "2022-03", "2022-04", 
"2021-08", "2021-06", "2021-09", "2022-03", "2021-12", "2021-12", 
"2021-12", "2022-04", "2021-12", "2021-12", "2021-09", "2021-11", 
"2021-07", "2021-07", "2022-03", "2021-08", "2021-10", "2021-07", 
"2022-04", "2022-04", "2022-04", "2021-11", "2021-12", "2022-03", 
"2021-11", "2022-02", "2022-04", "2021-06", "2021-07", "2022-04", 
"2021-10", "2021-10", "2022-01", "2022-02", "2021-07", "2021-09", 
"2021-05", "2021-09", "2022-03", "2021-08", "2021-12", "2021-11", 
"2022-01", "2022-04", "2021-12", "2021-10", "2021-05", "2022-01", 
"2022-01", "2022-04", "2021-06", "2021-05", "2021-11", "2021-05", 
"2021-08", "2022-04", "2022-02", "2021-06", "2021-08", "2022-02", 
"2022-03", "2022-02", "2021-09", "2021-07", "2022-04", "2021-09", 
"2022-04", "2022-01", "2021-07", "2021-12", "2021-06", "2022-03", 
"2021-04", "2021-06", "2022-01", "2022-03", "2022-02", "2022-04", 
"2021-12", "2021-08", "2022-03", "2021-04", "2021-12", "2021-11", 
"2022-03", "2022-03", "2021-12", "2021-06", "2021-06", "2022-03", 
"2021-07", "2022-02", "2021-08", "2021-05", "2021-08", "2021-10", 
"2021-09", "2021-09", "2022-02", "2021-09", "2021-09", "2021-08", 
"2021-06", "2021-07", "2022-02", "2022-02", "2021-07", "2022-04", 
"2022-02", "2021-05", "2021-11", "2022-04", "2021-09", "2022-04", 
"2021-06", "2021-06", "2021-12", "2021-07", "2021-11", "2021-05", 
"2021-12", "2021-11", "2021-05", "2021-11", "2021-05", "2021-11", 
"2021-12", "2021-09", "2022-04", "2022-04", "2022-01", "2021-08", 
"2021-10", "2021-05", "2022-04", "2021-07", "2021-11", "2021-04", 
"2021-12", "2021-06", "2021-06", "2021-09", "2022-03", "2021-05", 
"2021-12", "2022-04", "2021-05", "2021-09", "2021-09", "2021-09", 
"2021-09", "2021-08", "2021-07", "2021-05", "2022-04", "2022-04", 
"2021-12", "2022-02", "2022-04", "2021-10", "2022-03", "2022-04", 
"2022-02", "2021-09", "2021-07", "2021-06", "2021-09", "2022-02", 
"2021-06", "2021-12", "2022-03", "2021-10", "2021-12", "2022-02", 
"2021-07", "2021-07", "2022-03", "2022-01", "2021-11", "2022-02", 
"2021-12", "2021-06", "2022-02", "2021-10", "2021-04", "2021-05", 
"2021-11", "2021-10", "2021-10", "2022-01", "2022-01", "2021-10", 
"2022-01", "2021-07", "2021-10", "2021-05", "2021-07", "2021-11", 
"2021-11", "2021-08", "2021-05", "2021-06", "2022-04", "2021-05", 
"2021-11", "2021-06", "2021-09", "2021-05", "2021-05", "2021-05", 
"2021-08", "2021-10", "2021-12", "2021-10", "2022-01", "2022-03", 
"2021-09", "2021-07", "2021-07", "2022-02", "2021-06", "2021-09", 
"2021-07", "2021-10", "2022-03", "2021-11", "2022-02", "2022-02", 
"2022-01", "2021-08", "2022-04", "2021-05", "2021-07", "2022-03", 
"2021-08", "2021-09", "2021-05", "2021-11", "2021-09", "2022-03", 
"2021-05", "2021-05", "2021-08", "2022-02", "2021-07", "2021-05", 
"2021-11", "2022-04", "2021-12", "2021-09", "2022-04", "2021-12", 
"2022-02", "2021-09", "2022-01", "2022-04", "2021-06", "2021-07", 
"2022-03", "2021-10", "2021-09", "2021-09", "2021-04", "2021-05", 
"2021-08", "2021-09", "2022-04", "2021-08", "2021-11", "2022-02", 
"2021-06", "2021-12", "2021-11", "2021-06", "2022-03", "2021-08", 
"2021-08", "2021-09", "2021-12", "2021-12", "2022-01", "2022-03", 
"2022-04", "2021-08", "2022-04", "2021-05", "2021-07", "2022-02", 
"2021-07", "2021-12", "2021-09", "2021-04", "2021-10", "2022-04", 
"2021-08", "2021-06", "2021-05", "2021-04", "2021-12", "2021-07", 
"2022-01", "2022-01", "2022-01", "2021-11", "2021-08", "2021-06", 
"2021-04", "2021-12", "2021-09", "2021-07", "2021-11", "2021-07", 
"2021-04", "2021-06", "2021-08", "2021-05", "2022-03", "2021-07", 
"2021-11", "2021-11", "2021-09", "2021-10", "2021-10", "2021-09", 
"2021-10", "2021-07", "2022-02", "2021-06", "2021-07")), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -500L))

CodePudding user response:

I would simply recategorise the variable before passing it to ggplot. This has the double positive effect of:

  1. Not having to combine legend labels.
  2. Not having to assign the same colour to many different values.
library(dplyr)
library(ggplot2)

dataset3 <- data.frame(
        datum = c("2021-04", "2021-12", "2022-01"),
        transacties = c(10, 15, 17)
)

dataset3 |> 
        mutate(year = substr(datum, 1, 4)) |> 
        ggplot(aes(x = datum, y = transacties, fill = year))  
        geom_bar(stat = "identity")  
        scale_fill_manual(values= c("2021" = "#FFBD33",
                                    "2022" = "#203269"))

Created on 2022-04-21 by the enter image description here

  • Related