【问题标题】:R: Discrete heatmap with ggplot2R:使用 ggplot2 的离散热图
【发布时间】:2021-02-06 22:18:03
【问题描述】:

我需要一张热图:

  • 红色的“否”
  • “是”为绿色。
  • x 轴上的城市
  • y 轴运动
library(ggplot2)
df = data.frame(City= c(Boston, Caracas, Madrid, Tokio),
                Val = c(Yes, No, No, Yes),
                Sport = c("Soccer","Soccer","Soccer","Soccer"))

【问题讨论】:

  • 试试ggplot(df, aes(x = City, y = Sport, fill = Val)) + geom_tile()

标签: r ggplot2 plotly heatmap data-manipulation


【解决方案1】:

您的示例只有一项运动,因此它不能制作出很好的热图。这是一个扩展示例,可以更好地了解美学:

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?")

reprex package (v0.3.0) 于 2020 年 10 月 23 日创建

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-25
    • 2017-04-12
    • 2016-12-14
    • 2021-11-29
    • 2022-11-25
    • 2012-12-24
    • 2013-12-16
    相关资源
    最近更新 更多