scale_color_manual not working for assigning custermized color

1.6k Views Asked by At

I am working on assigning the same action code with a specific color but the scale_color_manual does not works for me. The color picked up is still by default. Any suggestions? That will be really helpful!! Either assign the action code using the color column or picked color like 'red'/'black'

    library(ggplot2)
    library(stringr)
    library(librudate)
    category <- c('task1', 'task2', 'task2','task1','task1')
    start_min <- c(0,   0,  16,   20,   40)
    stop_min <- c(14.9,10, 30, 35, 70)
    color <- c('#FFFF00','#FFFF00','#3581B8','#3581B8', '#FFFFFF')
    action <- c('A', 'A', 'B', 'B', 'C')
    data <- data.frame(category,start_min,stop_min, color)
    
    bars <- ggplot(data, mapping=aes(ymin=0, ymax=2,
                                                  xmin=start_min, xmax=stop_min,
                                                  group=category,
                                                  fill=action,
                                                  text=paste("Task:", str_wrap(string = action, width = 70,),
                                                            
                                                             "<br>category: ", format(category, digits=1), 
                                                             "<br>Duration: ", format(action, digits=1)) 
    )) + 
      facet_grid(category ~ .)+
      geom_rect(alpha=0.8) +
      labs(title='System Tag') +
      theme_minimal()+
      theme(plot.title = element_text(hjust=0.5, color="white"), #legend.position = 'none',
            plot.subtitle=element_text(hjust=1, vjust=1, color="white"),
            axis.title.x=element_text(color="white"), axis.text.x=element_text(color="white"),
            axis.text.y=element_blank(), axis.ticks.y=element_blank(),
            panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
            panel.border = element_blank(), panel.background = element_blank()) +
      scale_color_manual(
                         breaks = c("A", "B", "C"),
                         values = c("A" = "blue", "B" = "red", "C" = "black") )
    
    
    bars <- plotly::ggplotly(bars, tooltip="text") %>%
      plotly::ggplotly(bars, tooltip="text") %>%
      plotly::config(displayModeBar = TRUE) %>% 
      plotly::layout(plot_bgcolor='white', paper_bgcolor='white', margin = list(b=30, l=0, r=10, t=30))
    
    bars
1

There are 1 best solutions below

0
On BEST ANSWER

so the fix is very easy, change scale_color_manual to scale_fill_manual

If you want colors to be the color in your color variable, just do this:

scale_fill_manual(
        breaks = c("A", "B", "C"),
        values = color)