Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot scale_color_manual with breaks does not match expected order

Tags:

r

ggplot2

I'm making a scatter plot and I'd like to specify the order of the text in the legend, as well as the colors. Using ggplot, the color order does not match the text order as written.

My Code:

MyX<-c(1,2,3,4,5)
MyY<-c(3,4,5,2,7)
MyCat<-c("Dark","Medium","DarkLight","LightDark","Light")
MyDF<-data.frame(MyX,MyY,MyCat)


ggplot(MyDF, aes(x=MyX, y=MyY, color=MyCat)) +
  geom_point()+
  xlab("Time") + 
  ylab("Num") +
  scale_color_manual(breaks =     c("Dark","DarkLight","Medium","LightDark","Light"),
                     values=c("red", "orange","yellow","cadetblue2","dodgerblue"))

I don't want the default order of the text in the legend, so I'd like to change the order to be:

"Dark","DarkLight","Medium","LightDark","Light"

I also want the colors to be in the gradient:

"red", "orange","yellow","cadetblue2","dodgerblue"

So that Dark = Red, DarkLight = orange, Medium = yellow, LightDark = cadetblue2, Light = blue.

Which is what I would assume would happen with above code. But as plotted the text does not match the color and I have:

Dark = Red, DarkLight = orange, Medium = Blue, LightDark = cadetblue2, Light = yellow.

Note- I have the same issue when using a color ramp:

  gs.pal <- colorRampPalette(c("red","blue"),bias=.1,space="rgb")
+scale_color_manual(breaks = c("Dark","DarkLight","Medium","LightDark","Light"),
                       values=gs.pal(5))
like image 336
Vint Avatar asked Jan 07 '19 17:01

Vint


2 Answers

ggplot is arranging the MyCat variable in alphabetical order in order to avoid that, one needs to define that variable as a factor in the desired order:

library(ggplot2)

MyDF<-data.frame(MyX,MyY,MyCat)

#define as factor in the desired order
MyDF$MyCat<-factor(MyDF$MyCat, levels=c("Dark","DarkLight","Medium","LightDark","Light"))

ggplot(MyDF, aes(x=MyX, y=MyY, color=MyCat)) +
  geom_point()+
  xlab("Time") + 
  ylab("Num") +
  scale_color_manual(breaks = c("Dark","DarkLight","Medium","LightDark","Light"),
                     values=c("red", "orange","yellow","cadetblue2","dodgerblue"))

This should provide the desire order
enter image description here

like image 108
Dave2e Avatar answered Nov 10 '22 10:11

Dave2e


This sets legend names, colors, and order at the same time.

p <- ggplot(dd) + 
      geom_line(aes(x=num_round, y = goods, color = "Good"), size=1) +
      geom_line(aes(x=num_round, y = mediums, color = "Medium"), size=1) + 
      geom_line(aes(x=num_round, y = bads, color = "Bad"), size=1) + 
      scale_color_manual("State", breaks=c("Good", "Medium", "Bad"),
                         values=c("Good"="green","Medium"="orange","Bad"="red")) 
print(p)

enter image description here

like image 40
Ahmed Laatabi Avatar answered Nov 10 '22 10:11

Ahmed Laatabi