【发布时间】:2021-07-08 07:28:38
【问题描述】:
我有一个代码可以生成这样的密度散点图(在这里找到How to add an ellipse to a scatter plot colored by density in R?):
## 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=1)
我想添加与下图完全相同的轮廓(在此处找到 https://fr.mathworks.com/matlabcentral/fileexchange/8577-scatplot):
我必须在没有 ggplot2 的情况下这样做。
请问有人有意见吗?
【问题讨论】:
标签: r plot scatter-plot