如何使用 ggplot2 绘制概率热图?
How do I plot a probability heatmap with ggplot2?
假设我有一堆数据,每行都有 x 和 y 坐标和 TRUE
/FALSE
:
library(tidyverse)
set.seed(666) #666 for the devil
x <- rnorm(1000, 50, 10)
y <- sample(1:100, 1000, replace = T)
result <- sample(c(T, F), 1000, prob = c(1, 9),replace = T)
data <- tibble(x, y, result)
现在,我想绘制一个图表,根据该数据显示某个区域 TRUE
的可能性。我可以将数据分组为小方块(或其他)并计算 TRUE
百分比,然后绘制它,但我想知道 ggplot2
中是否有某些东西会自动为我完成。
不完全在 ggplot2
但以下内容产生了我认为你所要求的
library(tidyverse)
library(broom)
set.seed(666) #666 for the devil
data.frame(x = rnorm(1000, 50, 10),
y = sample(1:100, 1000, replace = T),
result = sample(c(T, F), 1000, prob = c(1, 9), replace = T)) %>%
do(augment(glm(result ~ x * y, data = ., family = "binomial"), type.predict = "response")) %>%
ggplot(aes(x, y, color = .fitted)) +
geom_point()
或 geom_hex
而不是 geom_point
看起来很有趣
ggplot(data, aes(x = x, y = y, z = as.numeric(result))) +
stat_summary_2d(bins = 20, color = "grey", fun = mean) +
theme_classic()
这似乎是另一个解决方案:
library(tidyverse)
#Preparing data
set.seed(666) #666 for the devil
data <- tibble(x = rnorm(1000, 50, 10),
y = sample(1:100, 1000, replace = T),
result = sample(c(T, F), 1000,
prob = c(1, 9), replace = T)) %>%
filter(result == TRUE)
#Plotting with ggplot
ggplot(data, aes(x, y)) +
geom_bin2d()
假设我有一堆数据,每行都有 x 和 y 坐标和 TRUE
/FALSE
:
library(tidyverse)
set.seed(666) #666 for the devil
x <- rnorm(1000, 50, 10)
y <- sample(1:100, 1000, replace = T)
result <- sample(c(T, F), 1000, prob = c(1, 9),replace = T)
data <- tibble(x, y, result)
现在,我想绘制一个图表,根据该数据显示某个区域 TRUE
的可能性。我可以将数据分组为小方块(或其他)并计算 TRUE
百分比,然后绘制它,但我想知道 ggplot2
中是否有某些东西会自动为我完成。
不完全在 ggplot2
但以下内容产生了我认为你所要求的
library(tidyverse)
library(broom)
set.seed(666) #666 for the devil
data.frame(x = rnorm(1000, 50, 10),
y = sample(1:100, 1000, replace = T),
result = sample(c(T, F), 1000, prob = c(1, 9), replace = T)) %>%
do(augment(glm(result ~ x * y, data = ., family = "binomial"), type.predict = "response")) %>%
ggplot(aes(x, y, color = .fitted)) +
geom_point()
或 geom_hex
而不是 geom_point
看起来很有趣
ggplot(data, aes(x = x, y = y, z = as.numeric(result))) +
stat_summary_2d(bins = 20, color = "grey", fun = mean) +
theme_classic()
这似乎是另一个解决方案:
library(tidyverse)
#Preparing data
set.seed(666) #666 for the devil
data <- tibble(x = rnorm(1000, 50, 10),
y = sample(1:100, 1000, replace = T),
result = sample(c(T, F), 1000,
prob = c(1, 9), replace = T)) %>%
filter(result == TRUE)
#Plotting with ggplot
ggplot(data, aes(x, y)) +
geom_bin2d()