How can I overlay two dense scatter plots so that I can see the outlines of each in R or Matlab?

前端 未结 4 685
清歌不尽
清歌不尽 2021-02-13 16:40

See this example\"Example

This was created in matlab by making two scatter plots indepe

4条回答
  •  佛祖请我去吃肉
    2021-02-13 17:14

    You could first export the two data sets as bitmap images, re-import them, add transparency:

    overlay

    library(grid)
    
    N <- 1e7 # Warning: slow
    d <- data.frame(x1=rnorm(N),
                    x2=rnorm(N, 0.8, 0.9),
                    y=rnorm(N, 0.8, 0.2),
                    z=rnorm(N, 0.2, 0.4))
    
    v <- with(d, dataViewport(c(x1,x2),c(y, z)))
    
    png("layer1.png", bg="transparent")
    with(d, grid.points(x1,y, vp=v,default="native",pch=".",gp=gpar(col="blue")))
    dev.off()
    png("layer2.png", bg="transparent")
    with(d, grid.points(x2,z, vp=v,default="native",pch=".",gp=gpar(col="red")))
    dev.off()
    
    library(png)
    i1 <- readPNG("layer1.png", native=FALSE)
    i2 <- readPNG("layer2.png", native=FALSE)
    
    ghostize <- function(r, alpha=0.5)
      matrix(adjustcolor(rgb(r[,,1],r[,,2],r[,,3],r[,,4]), alpha.f=alpha), nrow=dim(r)[1])
    
    grid.newpage()
    grid.rect(gp=gpar(fill="white"))
    grid.raster(ghostize(i1))
    grid.raster(ghostize(i2))
    

    you can add these as layers in, say, ggplot2.

提交回复
热议问题