'geom_tile color for each row and facet
I have this code
ggplot(
data.frame(
inh = c(
1, 1, 1, 1,
2, 2, 2, 2,
3, 3, 3, 3,
4, 4, 4, 4
),
buck = c(
1, 2, 3, 4,
1, 2, 3, 4,
1, 2, 3, 4,
1, 2, 3, 4
),
gender =
c(
"m", "m", "m", "m",
"m", "m", "m", "m",
"m", "m", "m", "m",
"m", "m", "m", "m",
"f", "f", "f", "f",
"f", "f", "f", "f",
"f", "f", "f", "f",
"f", "f", "f", "f"
),
share =
c(51, 34, 10, 3,
42, 34, 13, 8,
33, 26, 19, 13,
38, 24, 19, 16,
49, 20, 20, 10,
30, 30, 30, 10,
10, 10, 10, 70,
25, 25, 25, 25)
),
aes(x = inh, y = buck, fill = share)) +
geom_tile() +
geom_text(aes(label = share)) +
scale_fill_distiller(direction = 1, type = "seq") +
facet_grid(cols = vars(gender))
It creates heatmap and it works fine, except that I would like assign color for each row and column. For example facet “f” and x axis “4”, value 25 looks the same for each “buck” because my code took highest value - 75 and lowest one - 3 and created gradient for values between.
But for each row and facet I would like to achieve something like this.
ggplot(
data.frame(
inh = c(
1, 2, 3, 4
),
buck = c(
3, 3, 3, 3
),
gender =
c(
"f", "f", "f", "f"
),
share =
c(20, 30, 10, 25)
),
aes(x = inh, y = buck, fill = share)) +
geom_tile() +
geom_text(aes(label = share)) +
scale_fill_distiller(direction = 1, type = "seq") +
facet_grid(cols = vars(gender))
Solution 1:[1]
It's difficult to know exactly what you're asking here. It's certainly possible to get a colour for each row and facet, as the question title suggests. Are you looking for something like this?
ggplot(data,
aes(x = inh, y = buck, fill = as.factor(interaction(buck, gender)))) +
geom_tile() +
geom_text(aes(label = share)) +
scale_fill_brewer() +
facet_grid(cols = vars(gender)) +
theme(legend.position = "none")
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Allan Cameron |

