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))