MYaseen208
MYaseen208

Reputation: 23898

Manual Colours in scale_colour_manual are not rendering properly

Could not figured out why manual colours are not rendering properly. Any hints!!

library(tidyverse)

tb1 <- 
  tibble(
    X = seq(1:50)
  , A = rnorm(n = 50, mean = 100, sd = 8)
  , B = rnorm(n = 50, mean = 150, sd = 6)
  , C = rnorm(n = 50, mean = 175, sd = 5)
  , D = rnorm(n = 50, mean = 200, sd = 5)
  )

tb1

Plot1 <- 
  ggplot(tb1, aes(x = X)) +
  geom_point(aes(y = A, colour = "orange")) +
  geom_line(aes(y = B, colour = "black")) +
  geom_line(aes(y = C, colour = "green")) +
  geom_line(aes(y = D, colour = "red")) +
  scale_colour_manual(
      name   = ''
    , values = c("orange" = "orange", "black" = "black", "green" = "green", "red" = "red")
    , labels = c("Obs", "Fit1", "Fit2", "Fit3")
  )

Plot1

enter image description here

Upvotes: 0

Views: 35

Answers (1)

StupidWolf
StupidWolf

Reputation: 46908

If you don't add the labels, you see that they are ordered alphabetically:

ggplot(tb1, aes(x = X)) +
  geom_point(aes(y = A, colour = "orange")) +
  geom_line(aes(y = B, colour = "black")) +
  geom_line(aes(y = C, colour = "green")) +
  geom_line(aes(y = D, colour = "red")) +
  scale_colour_manual(
      name   = ''
    , values = c("orange" = "orange", "black" = "black", "green" = "green", "red" = "red")

enter image description here

So you needa arrange the order using breaks:

ggplot(tb1, aes(x = X)) +
  geom_point(aes(y = A, colour = "orange")) +
  geom_line(aes(y = B, colour = "black")) +
  geom_line(aes(y = C, colour = "green")) +
  geom_line(aes(y = D, colour = "red")) +
  scale_colour_manual(
      name   = ''
    , values = c("orange" = "orange", "black" = "black", "green" = "green", "red" = "red"),
      breaks = c("orange","black","green","red"),
    , labels = c("Obs", "Fit1", "Fit2", "Fit3")
  )

enter image description here

Upvotes: 1

Related Questions