0

Fairly new to R so forgive me if this is a simple question. I have created a graph which tracks a loss ratio percentage by data provider. I assign the "data provider" lines colour using scale_colour_brewer 'blue'. In a previous iteration I had used scale_colour_manual to assign a colour to a data provider, however this can no longer be used as the data providers are now variables in the code using an IF statement to only include in the Shiny report if assigned a 1 earlier in the code.

I have tried to put a Horizontal line at .85 which I am trying to get the colour to be red. The scale_colour_brewer 'blue' portion of the code seems to overwrite the 'red' line, meaning it is a thick blue line and has the name 'red' in the legend. How do I correct this? Example code below:

LR_Graph_Plot  <- ggplot() +
          geom_hline(aes(yintercept = 0.85, color ="red"), size = 1.25) +
          geom_line(data = LR_Graph(), aes(x = YM1_DATE, y = ELR, group = Data.Provider, color = Data.Provider), size = 0.75) +
          # geom_hline(aes(yintercept = 0)) +
          scale_colour_brewer("Blues") +
          labs(y = "Loss Ratio (%)") +
          ggtitle("Earned Loss Ratio: Loss Ratio Monthly split by Data Provider") + 
          scale_y_continuous(labels = scales ::percent) + 
          scale_x_date(breaks = "months", date_labels = "%Y-%m") + 
          theme_bw() +
          theme(axis.title.x = element_blank(), legend.title = element_blank(), panel.border = element_blank(),
                axis.line.y = element_line(color='black'), axis.line.x = element_line(color='black'),
                legend.position = "right" , axis.text.x = element_text(angle = 90)) +
          theme(
            panel.background = element_rect(fill="#cccccc", color="#000000",size = 2, linetype = "solid"),
            panel.grid.major = element_line(size = 0.5, linetype = "solid", colour = "white"),
            panel.grid.minor = element_line(size = 0.5,linetype = "solid", colour = "white")
          )

        ELR_Graph_Plot <- ggplotly(LR_Graph_Plot)

Apologies if this is not clear or has been asked before. I'm fairly new to coding.

1 Answers1

0

Move the color assignment out of the aesthetic mapping aes(). Then the line uses a fixed color and not the provided color scale.

Your line geom_hline(aes(yintercept = 0.85, color ="red"), size = 1.25) should be geom_hline(aes(yintercept = 0.85), color ="red", size = 1.25)

Mojoesque
  • 1,166
  • 8
  • 15