Reputation: 356
I have this dataframe:
var1 var2 value
1 A X 0
2 B X 1
3 C X 1
4 A Y 1
5 B Y 0
6 C Y 0
and I would like to plot var1 in the X-axis and var2 in the Y-axis, and plot the value as a red if it is a 1, and white if it is zero. In this case, kind of:
A B C
X white red red
Y red white white
So far, I have tried with:
ggplot(dataframe, aes(var1, var2, fill = value)) +
geom_raster() +
coord_fixed(ratio = 1) +
scale_fill_gradientn(colours = c("white", "red"))
but the plot is wrong:
Upvotes: 1
Views: 551
Reputation: 8127
df <- tribble(
~var1, ~var2, ~value,
"A", "X", 0,
"B", "X", 5,
"C", "X", 4,
"A", "Y", 1,
"B", "Y", 2,
"C", "Y", 0.5,
)
df %>%
ggplot(aes(var1, var2, fill = value)) +
geom_raster() +
scale_fill_gradient(low = "white", high = "red")
Upvotes: 2