简体   繁体   中英

R: Discrete heatmap with ggplot2

I need a heatmap with:

  • "No" in red
  • "Yes" in green.
  • City in x axis
  • Sport in y axis
library(ggplot2)
df = data.frame(City= c(Boston, Caracas, Madrid, Tokio),
                Val = c(Yes, No, No, Yes),
                Sport = c("Soccer","Soccer","Soccer","Soccer"))

Your example only has a single sport, so it doesn't make a great heatmap. Here's an expanded example to give a better impression of the aesthetic:

library(ggplot2)

set.seed(69)

df <- data.frame(City= rep(c("Boston", "Caracas", "Madrid", "Tokio"), 4),
                 Val = sample(c("Yes", "No"), 16, TRUE),
                 Sport = rep(c("Soccer","Hockey", "Tennis", "Darts"), each = 4))

ggplot(df, aes(City, Sport, fill = Val)) + 
  geom_tile(color = "#00000022") +
  scale_fill_manual(values = c("red", "forestgreen")) +
  coord_equal() +
  theme_bw() +
  labs(fill = "Sport popular?")

Created on 2020-10-23 by the reprex package (v0.3.0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM