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

前端 未结 4 671
清歌不尽
清歌不尽 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:09

    Use the transparency capability of color descriptions. You can define a color as a sequence of four 2-byte words: muddy <- "#888888FF" . The first three pairs set the RGB colors (00 to FF); the final pair sets the transparency level.

    0 讨论(0)
  • 2021-02-13 17:12

    Using ggplot2 you can add together two geom_point's and make them transparent using the alpha parameter. ggplot2 als adds up transparency, and I think this is what you want. This should work, although I haven't run this.

    dat = data.frame(x = runif(1000), y = runif(1000), cat = rep(c("A","B"), each = 500))
    ggplot(aes(x = x, y = y, color = cat), data = dat) + geom_point(alpha = 0.3)
    

    ggplot2 is awesome!

    This is an example of calculating and drawing a convex hull:

    library(automap)
    library(ggplot2)
    library(plyr)
    loadMeuse()
    theme_set(theme_bw())
    
    meuse = as.data.frame(meuse)
    chull_per_soil = ddply(meuse, .(soil), 
               function(sub) sub[chull(sub$x, sub$y),c("x","y")])
    
    ggplot(aes(x = x, y = y), data = meuse) +
      geom_point(aes(size = log(zinc), color = ffreq)) +
      geom_polygon(aes(color = soil), data = chull_per_soil, fill = NA) +
      coord_equal()
    

    which leads to the following illustration:

    enter image description here

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2021-02-13 17:18

    AFAIK, your best option with Matlab is to just make your own plot function. The scatter plot points unfortunately do not yet have a transparency attribute so you cannot affect it. However, if you create, say, most crudely, a bunch of loops which draw many tiny circles, you can then easily give them an alpha value and obtain a transparent set of data points.

    0 讨论(0)
提交回复
热议问题