【发布时间】:2021-06-16 17:20:39
【问题描述】:
我有一个代码可以生成一个按密度着色的散点图(灵感来自这篇帖子R Scatter Plot: symbol color represents number of overlapping points):
## Data in a data.frame
x1 <- rnorm(n=1000, sd=2)
x2 <- x1*1.2 + rnorm(n=1000, sd=2)
df <- data.frame(x1,x2)
## Use densCols() output to get density at each point
x <- densCols(x1,x2, colramp=colorRampPalette(c("black", "white")))
df$dens <- col2rgb(x)[1,] + 1L
## Map densities to colors
cols <- colorRampPalette(c("#FF3100", "#FF9400", "#FCFF00",
"#45FE4F", "#00FEFF", "#000099"))(6)
df$col <- ifelse(df$dens >= 250, cols[1], ifelse(df$dens >= 200, cols[2], ifelse(df$dens >= 150, cols[3], ifelse(df$dens >= 100, cols[4], ifelse(df$dens >= 50, cols[5], cols[6])))))
## Plot it, reordering rows so that densest points are plotted on top
plot(x2~x1, data=df[order(df$dens),], pch=20, col=col, cex=2)
我想添加一个椭圆,其中密度值等于一个固定值,例如 200。正是like in this image,其中绘制了一个椭圆,其中密度值等于 0.7。我没有生成此图像中的情节的代码,所以我真的不知道如何获得类似的东西
有人可以帮忙吗?
【问题讨论】:
标签: r plot scatter-plot ellipse