3

I ask R to make a series of plots. However, the colors assigned to the different variables by ggplot2 vary depending on the actual data. I need more consistency In particular I want:

FOUR to be red THREE to be green TWO to be yellow ONE to be white

Based on previous answers I suspect I need to order the levels. Can someone help me out?

Here is some sample data:

df<-structure(list(`id` = structure(c(3L, 3L, 3L, 3L, 3L, 2L, 
                                  3L, 3L, 1L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 2L, 4L, 3L, 3L, 2L, 3L, 
                                  2L, 4L, 2L, 4L, 3L, 3L, 2L, 3L, 4L, 3L, 3L, 2L, 3L, 3L, 4L, 3L, 
                                  1L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("ONE", 
                                                                                              "TWO", "THREE", "FOUR"), class = "factor"), NAME = c("0", "0.25", "0.5", 
                                                                                                                                          "0.75", "1", "1.25", "1.5", "1.75", "2", "2.25", "2.5", "2.75", 
                                                                                                                                          "3", "3.25", "3.5", "3.75", "4", "4.25", "4.5", "4.75", "5", 
                                                                                                                                          "5.25", "5.5", "5.75", "6", "6.25", "6.5", "6.75", "7", "7.25", 
                                                                                                                                          "7.5", "7.75", "8", "8.25", "8.5", "8.75", "9", "9.25", "9.5", 
                                                                                                                                          "9.75", "10", "10.25", "10.5", "10.75", "11", "11.25", "11.5", 
                                                                                                                                          "11.75", "12", "12.25")), .Names = c("id", "NAME"), row.names = c("0", 
                                                                                                                                                                                                                 "0.25", "0.5", "0.75", "1", "1.25", "1.5", "1.75", "2", "2.25", 
                                                                                                                                                                                                                 "2.5", "2.75", "3", "3.25", "3.5", "3.75", "4", "4.25", "4.5", 
                                                                                                                                                                                                                 "4.75", "5", "5.25", "5.5", "5.75", "6", "6.25", "6.5", "6.75", 
                                                                                                                                                                                                                 "7", "7.25", "7.5", "7.75", "8", "8.25", "8.5", "8.75", "9", 
                                                                                                                                                                                                                 "9.25", "9.5", "9.75", "10", "10.25", "10.5", "10.75", "11", 
                                                                                                                                                                                                                 "11.25", "11.5", "11.75", "12", "12.25"), class = c("tbl_df", 
                                                                                                                                                                                                                                                                     "tbl", "data.frame"))

Here is the code:

library(ggplot2) 
library(tidyr)

colors <- c("red","white","yellow","green")


df$NAME <- rownames(df)

x<-gather(df,NAME)
colnames(x)<-c("Name", "variable","value")

ggplot(x, 
   aes(x = Name, y = variable, fill = factor(value))) + 
geom_tile() + 
scale_fill_manual(values=colors)+
scale_x_discrete(name="Time Period", limits= rownames(df))
Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294
  • 2
    The only packages your code is using are `ggplot2` and `tidyr` (edited). In the future, instead of "`dput(df)` ... code to transform df into x ... question about how to plot `x`", just `dput(x)` since the transformation is irrelevant. – Gregor Thomas Jun 11 '15 at 19:37
  • Gregor. Got it. Thanks! – Matthew David Jankowski Jun 11 '15 at 19:47
  • I'll have to investigate why this one was marked duplicate. Before posting I looked at the referred to solution. It seemed close but didn't help me see what I needed to understand. I think Gregor's answer helped me achieve my goal base on my current level of knowledge.... – Matthew David Jankowski Jun 12 '15 at 22:07

1 Answers1

8

You need, as you say, to specify the order of your factor levels:

x$value = factor(x$value, levels = c("ONE", "TWO", "THREE", "FOUR"))
# the order of the vector you pass to levels defines the order of the factor

Then you need to define your color vector in the same order.

# "FOUR to be red THREE to be green TWO to be yellow ONE to be white"
colors <- c("white","yellow","green","red")

Another approach is to name the color vector (below), but I prefer the first way.

colors <- c("red","white","yellow","green")
names(colors) = c("FOUR", "ONE", "TWO", "THREE")

colors
#  FOUR      ONE      TWO    THREE 
# "red"  "white" "yellow"  "green" 

Either way, your plotting code should then work just fine.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294