I am having a hard time getting different colours for my 2 different facet_grids.
Here is my simple example:
df <- data.frame(x = rep(1:20, each=20), y= rep(1:20, 20),
val = sample(0:1,400, replace=TRUE),
facet_grid = c(rep("A", 200), rep("B", 200)))
colors <- c(1,2)
ggplot(df, aes(x = x, y = y)) +
geom_tile(aes(fill = val)) +
facet_grid(. ~ facet_grid, scales = "free") +
scale_fill_gradient(limits=c(0,1), low=brewer.pal(11, "RdYlGn")[colors][1],
high="grey30")
I am using the scale_fill_gradient function with the brewer.pal function to override the standard colours used. However, what I am trying to achieve is set in the two different facet_grids for values that are 1 the same colour (grey30) should be used. For values that are 0 in the first facet_grig, for instance, red should be used and in the second facet_grid green should be used.
Can anybody give me a hint on how to achieve that?
I created another example which is now closer to want I want but I do not understand why scale_fill_identity does not work. For that I slightly changed the data frame so that value has now 4 different values:
df <- data.frame(x = rep(1:20, each=20), y= rep(1:20, 20),
val = c(sample(0:1,200, replace=TRUE), sample(2:3,200, replace=TRUE)),
facet_grid = c(rep("A", 200), rep("B", 200)))
ggplot(df, aes(x = x, y = y)) +
geom_tile(aes(fill = val)) +
facet_grid(. ~ facet_grid, scales = "free")
ggplot(df, aes(x = x, y = y)) +
geom_tile(aes(fill = val)) +
facet_grid(. ~ facet_grid, scales = "free")
scale_fill_identity(labels=letters[1:4], breaks=c("red","blue","green","brown")) +
The first ggplot gives me almost what I want. 4 different colours in the two different facet_grids. I tried to use scale_fill_identity to change the 4 colours but what I get in my plot are 4 different colours black/white and red/green and not the ones specified above.
try this