scale_color_manual 无法分配自定义颜色

scale_color_manual not working for assigning custermized color

我正在努力为相同的操作代码分配特定颜色,但 scale_color_manual 对我不起作用。拾取的颜色仍然是默认的。有什么建议么?那真的很有帮助!!使用颜色列分配操作代码或选择颜色 '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

因此修复非常简单,将 scale_color_manual 更改为 scale_fill_manual

如果您希望颜色成为 color 变量中的颜色,只需这样做:

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