ggplot:当它们接近特定值时按密度着色点?

ggplot: color points by density as they approach a specific value?

我有一个包含 1,000 个模型值的数据集,这些值都在同一范围内 (y=40-70),因此这些点重叠很多。我感兴趣的是使用颜色来显示汇聚在单个值 (y=56.72) 上的点的密度,我在下图中用水平虚线表示了该值。我如何给这些点上色以显示这一点?

ggplot(data, aes(x=model, y=value))+ 
geom_point(size=1) + 
geom_hline(yintercept=56.72, 
           linetype="dashed", 
            color = "black")

我建议在 geom_point 中使用 alpha 参数。您应该使用接近 0 的值。

ggplot(data, aes(x=model, y=value)) + 
  geom_point(size=1, alpha = .1) + 
  geom_hline(yintercept=56.72, linetype="dashed", color = "black")

我认为您应该选择直方图或密度图:

n <- 500
data <- data.frame(model= rep("model",n),value =  rnorm(n,56.72,10))

ggplot(data, aes(x = value, y = after_stat(count))) +
  geom_histogram(binwidth = 1)+
  geom_density(size = 1)+
  geom_vline(xintercept = 56.72, linetype = "dashed", color = "black")+
  theme_bw()

这是具有相同数据的图:

ggplot(data, aes(x = model, y = value))+ 
  geom_point(size = 1) + 
  geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")

如果您的模型是迭代的并且确实收敛到该值,我建议您绘制为迭代的函数以显示收敛。另一种选择,保持与你相似的情节,是避开点的位置:

ggplot(data, aes(x = model, y = value))+ 
  geom_point(position = position_dodge2(width = 0.2),
             shape = 1,
             size = 2,
             stroke = 1,
             alpha = 0.5) + 
  geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")

这是您要求的颜色密度图:

library(dplyr)
library(ggplot2)
data %>%
  mutate(bin = cut(value, breaks = 10:120)) %>%
  dplyr::group_by(bin) %>%
  mutate(density = dplyr::n()) %>%
  ggplot(aes(x = model, y = value, color = density))+ 
  geom_point(size = 1) + 
  geom_hline(yintercept = 56.72, linetype = "dashed", color = "black")+
  scale_colour_viridis_c(option = "A")