Styling Legend in ggplot2

Hello everyone!

I'm having a lot of problems creating a legend for a bar plot in ggplot2.
The plot is fine, but the legend writes all the labels as if they were Python's tuples (i.e. (id1, 1, NA) ). I cannot explain why this is happening since that's not how they are written in the data and I'm having a hard time modifying them.

Has anybody seen this behavior before? How could I fix it?

data_filtered <- data.table(date = seq(as.Date('01-01-2020'), as.Date('10-01-2020'), by = '1 day'), 
                                                       filling = 1:10, 
                                                      idForLegend = c('id1', 'id2', 'id3', 'id4', 'id5', 'id6', 'id7', 'id8', 'id9', 'id10'))

p <- data_filtered %>%
            ggplot(aes(x = date, fill = idForLegend, y = filling)) +
            geom_bar(position = "dodge", stat = 'identity') +
            geom_hline(aes(yintercept = 5, linetype = 'Filling Threshold'), color='red', size=0.4) +
            theme_ipsum(
              grid = FALSE,
              caption_margin = 1,
              subtitle_margin = 1,
              plot_title_margin = 1,
              plot_margin = margin(1, 1, 1, 1)
            ) + 
            scale_color_manual(values = data_filtered$idForLegend) +
            #scale_color_manual(name = "Legend", labels = c('Filling Threshold', unique(data_filtered$idForLegend))) +
            scale_fill_brewer(palette="Paired")

I can't reproduce your issue with the code you have provided, Can you try again and provide a proper REPRoducible EXample (reprex)?

library(tidyverse)
library(data.table)
library(hrbrthemes)

data_filtered <- data.table(date = seq(as.Date('01-01-2020'), as.Date('10-01-2020'), by = '1 day'), 
                            filling = 1:10, 
                            idForLegend = c('id1', 'id2', 'id3', 'id4', 'id5', 'id6', 'id7', 'id8', 'id9', 'id10'))

data_filtered %>%
    ggplot(aes(x = date, fill = idForLegend, y = filling)) +
    geom_bar(position = "dodge", stat = 'identity') +
    geom_hline(aes(yintercept = 5, linetype = 'Filling Threshold'), color='red', size=0.4) +
    theme_ipsum(
        grid = FALSE,
        caption_margin = 1,
        subtitle_margin = 1,
        plot_title_margin = 1,
        plot_margin = margin(1, 1, 1, 1)
    ) +
    scale_color_manual(values = data_filtered$idForLegend) +
    scale_fill_brewer(palette="Paired")

Created on 2020-11-18 by the reprex package (v0.3.0)

I am sorry I couldn't provide a reprex, the actual data I'm using contains sensitive information that I can't share.

However, you are right, the code I provided is correct. My problem occurred because I was trying to render a ggplot using the renderPlotly function in Shiny, which I never considered as possible cause of the problem.
Changing it to renderPlot (and accordingly the plotlyOutput element to plotOutput) solved the problem.

For future reference, its sufficient for a reprex to be representative, it needn't be actual and rarely is.

Yes it can take some effort to construct, but that's often a reasonable price to pay for a higher level of support from other forum users.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.