0

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, 1, 1, 1, 
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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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))
Phil
  • 7,287
  • 3
  • 36
  • 66
caxap
  • 13
  • 3
  • Please share your data using `dput(df)`? – Quinten Apr 21 '22 at 16:02
  • 1
    How about `ggplot(dataset3, aes(x = datum, y = transacties, fill = stringr::str_extract(datum,"^\\d+"))) + geom_bar(stat="identity") + scale_fill_manual(values = c("2021" = "#FFBD33", "2022" = "#203269"))`? – Ian Campbell Apr 21 '22 at 16:03
  • You can share your data by copying the output of dput(dataset3) in your console and paste that in your question above. – Quinten Apr 21 '22 at 18:57
  • @Quinten Thank you, I've added it – caxap Apr 21 '22 at 19:00

2 Answers2

1

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 reprex package (v2.0.1)

PS please post sample data in the future so we don't have to recreate it! See here for more info on how to do that.

Andrea M
  • 2,314
  • 1
  • 9
  • 27
  • Hi, thank you, I want to try this out but I don't have a data frame. I just imported a generated mock data Excel file and loaded it. It has 500 rows with 1 column "datum" and 1 column "transaction," so if I put that under data.frame() code like you did it will be a lot of work, no? – caxap Apr 21 '22 at 18:49
  • I only used `data.frame()` to create a dataframe because I didn't have your data. You do have a dataframe - it's called `dataset3`, and possibly this is what you imported from Excel. You can use the second half of my code directly, without first creating a dataframe. – Andrea M Apr 21 '22 at 19:47
0

First I create a new column called year which is the string before your first - in your case. Than I use that column to fill the data which will create the legend you want. You can use the following code:

library(tidyverse)
dataset3 %>%
  mutate(year = sub("\\-.*", "", datum)) %>%
  ggplot(aes(x= datum, y = transacties, fill = year)) +
  geom_bar(stat="identity") +
  scale_fill_manual(values=c("2021" = "#FFBD33", "2022" = "#203269"))

Output:

enter image description here

Quinten
  • 35,235
  • 5
  • 20
  • 53