Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

See this exampleExample of overlaid scatter plots

This was created in matlab by making two scatter plots independently, creating images of each, then using the imagesc to draw them into the same figure and then finally setting the alpha of the top image to 0.5.

I would like to do this in R or matlab without using images, since creating an image does not preserve the axis scale information, nor can I overlay a grid (e.g. using 'grid on' in matlab). Ideally I wold like to do this properly in matlab, but would also be happy with a solution in R. It seems like it should be possible but I can't for the life of me figure it out.

So generally, I would like to be able to set the alpha of an entire plotted object (i.e. of a matlab plot handle in matlab parlance...)

Thanks,

Ben.

EDIT: The data in the above example is actually 2D. The plotted points are from a computer simulation. Each point represents 'amplitude' (y-axis) (an emergent property specific to the simulation I'm running), plotted against 'performance' (x-axis).

EDIT 2: There are 1796400 points in each data set.

like image 901
Ben J Avatar asked Mar 14 '12 17:03

Ben J


People also ask

How do I merge two scatter plots in Matlab?

However, you can use the hold on command to combine multiple plots in the same axes. For example, plot two lines and a scatter plot. Then reset the hold state to off. When the hold state is on, new plots do not clear existing plots or reset axes properties, such as the title or axis labels.

How do you visualize a scatter plot?

A scatter plot is a type of data visualization that shows the relationship between different variables. This data is shown by placing various data points between an x- and y-axis. Essentially, each of these data points looks “scattered” around the graph, giving this type of data visualization its name.

What does Gscatter do in Matlab?

gscatter( x , y , g ) creates a scatter plot of x and y , grouped by g . The inputs x and y are vectors of the same size. gscatter( x , y , g , clr , sym , siz ) specifies the marker color clr , symbol sym , and size siz for each group.


1 Answers

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

like image 102
Paul Hiemstra Avatar answered Sep 24 '22 17:09

Paul Hiemstra