我有一个包含1,000个模型值的数据集,这些值都在同一范围内(y = 40-70),因此这些点重叠一吨。我对使用颜色显示收敛于单个值(y = 56.72)的点的密度感兴趣,该值已在下图中用水平虚线表示。我该如何为这些点着色以显示呢?
header("Location: ...")
我认为您应该选择直方图或密度图:
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,color = "black")
如果您的模型是迭代的并且确实收敛到该值,建议您将其绘制为迭代函数以显示收敛。保持与您相似的情节的另一种方法是避开点的位置:
ggplot(data,y = value))+
geom_point(position = position_dodge2(width = 0.2),shape = 1,size = 2,stroke = 1,alpha = 0.5) +
geom_hline(yintercept = 56.72,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,color = "black")+
scale_colour_viridis_c(option = "A")
,
我建议在alpha
中使用geom_point
参数。您应该使用接近0的值。
ggplot(data,aes(x=model,y=value)) +
geom_point(size=1,alpha = .1) +
geom_hline(yintercept=56.72,linetype="dashed",color = "black")