R:使用 ggplot2 的离散热图
R: Discrete heatmap with ggplot2
我需要一个热图:
- 红色的“否”
- “是”为绿色。
- 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"))
你的例子只有一项运动,所以它不是很好的热图。下面是一个扩展示例,以提供更好的审美印象:
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 创建
我需要一个热图:
- 红色的“否”
- “是”为绿色。
- 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"))
你的例子只有一项运动,所以它不是很好的热图。下面是一个扩展示例,以提供更好的审美印象:
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 创建