gpt4 book ai didi

r - R 中具有大重叠和 3000+ 点的散点图

转载 作者:行者123 更新时间:2023-12-04 12:31:54 25 4
gpt4 key购买 nike

我正在使用 ggplot2 在 R 中绘制散点图。我正在比较希拉里和伯尼在小学和教育阶段获得的选票比例。有很多过度重叠和通往许多点的方式。我尝试使用透明度以便可以看到重叠部分,但它看起来仍然很糟糕。

My Graph

代码:

demanalyze <- function(infocode, n = 1){
infoname <- filter(infolookup, column_name == infocode)$description
infocolumn <- as.vector(as.matrix(mydata[infocode]))
ggplot(mydata) +
aes(x = infocolumn) +
ggtitle(infoname) +
xlab(infoname) +
ylab("Fraction of votes each canidate recieved") +
xlab(infoname) +
geom_point(aes(y = sanders_vote_fraction, colour = "Bernie Sanders")) +#, color = alpha("blue",0.02), size=I(1)) +
stat_smooth(aes(y = sanders_vote_fraction), method = "lm", formula = y ~ poly(x, n), size = 1, color = "darkblue", se = F) +
geom_point(aes(y = clinton_vote_fraction, colour = "Hillary Clinton")) +#, color = alpha("red",0.02), size=I(1)) +
stat_smooth(aes(y = clinton_vote_fraction), method = "lm", formula = y ~ poly(x, n), size = 1, color = "darkred", se = F) +
scale_colour_manual("",
values = c("Bernie Sanders" = alpha("blue",0.02), "Hillary Clinton" = alpha("red",0.02))
) +
guides(colour = guide_legend(override.aes = list(alpha = 1)))
}

我可以更改什么以使重叠区域看起来不那么凌乱?

最佳答案

在 2 维上绘制大量点的标准方法是使用 2D 密度图:

用可重现的例子:

x1 <- rnorm(1000, mean=10)
x2 <- rnorm(1000, mean=10)
y1 <- rnorm(1000, mean= 5)
y2 <- rnorm(1000, mean = 7)


mydat <- data.frame(xaxis=c(x1, x2), yaxis=c(y1, y2), lab=rep(c("H","B"),each=1000))
head(mydat)

library(ggplot2)
##Dots and density plots (kinda messy, but can play with alpha)
p1 <-ggplot(mydat) + geom_point(aes(x=xaxis, y = yaxis, color=lab),alpha=0.4) +
stat_density2d(aes(x=xaxis, y = yaxis, color=lab))
p1

dots and radii

## just density
p2 <-ggplot(mydat) + stat_density2d(aes(x=xaxis, y = yaxis, color=lab))
p2

density plot

有很多参数可以玩,看here有关 ggplot2 中绘图类型的完整信息。

关于r - R 中具有大重叠和 3000+ 点的散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38333608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com