Reputation: 2584
Here is my data
df<- structure(list(name = structure(c(2L, 12L, 1L, 16L, 14L, 10L,
9L, 5L, 15L, 4L, 8L, 13L, 7L, 6L, 3L, 11L), .Label = c("All",
"Bab", "boro", "bra", "charli", "delta", "few", "hora", "Howe",
"ist", "kind", "Kiss", "myr", "No", "TT", "where"), class = "factor"),
value = c(1.251, -1.018, -1.074, -1.137, 1.018, 1.293, 1.022,
-1.008, 1.022, 1.252, -1.005, 1.694, -1.068, 1.396, 1.646,
1.016)), .Names = c("name", "value"), class = "data.frame", row.names = c(NA,
-16L))
I want to have very clear shade separation. if I want to make a heatmap with specific color, I do like this
ggplot(df, aes(x = 1,y = name, fill = value)) +
geom_tile() +
ylab("") +
scale_fill_gradient(low='red', high='blue')
It makes it red to blue. what I want is to make the first 7 rows for example red to blue and the rest pink to black.
Is this possible ?
Upvotes: 0
Views: 1736
Reputation: 11957
ggplot generally encourages one scale per data series, but if you really wanted different scales for different subsets of the values, here's one way to do it.
You say you want a different scale for the first 7 rows. Again, tying presentation to the data's position isn't a great practice, but let's suppose we create a "group" column. It's important that this column be numerical, since we'll be using it to "cheat" the values in the heatmap. So a set of group values that separate your distribution would be good:
library(dplyr)
df <- arrange(df, name) %>%
mutate(group = c(rep(-3, 7), rep(3, 9)))
The fill
aesthetic then becomes value - group
, and we use scale_fill_gradientn
to assign colors:
ggplot(df, aes(x = 1,y = name, fill = value - group)) +
geom_tile() +
ylab("") +
scale_fill_gradientn(colours = c('black', 'pink', 'red', 'blue'))
Upvotes: 1