0

The code below produces a plot in which the size of a point is proportional to the number of cases that have the same values of x and y. How can I increase the space between the points so that they do not cross or intersect each other? While the example below uses fake data, the real data comprehends about 200 points that are highly intersected, thus making it difficult to perceive each individual point.

Thank you,

Sofia

    x = seq(1:10)
    y = c(4,3.8,3.8,3.2,3.1,2.5,2,1.5,1.2,1.3)
    size = c(7,20,2,70,100,70,5,80,110,2)
    pdf("example.pdf")
    par(mar=c(5,5,1.8,1.8))
    plot(y ~ x, cex = sqrt(size), pch = 1,
         ylab="y",
         xlab = "x",ylim=c(0,4),
         cex.lab =1.8,cex.axis =1.3,
         lwd = .5,type= "p", col="grey60")
    dev.off()
user2291581
  • 71
  • 1
  • 3
  • Use e.g. `cex = 0.5*sqrt(size)`? – eddi Apr 17 '13 at 16:43
  • You can't increase the spacing without moving the centers of the spots, so as other answers have noted, you need to adjust the scale of either the dot sizes or the graph range. My favorite alternative to this is to assign a color with partial transparency, e.g., `col=#00000020` so that overlapping dots get darker and darker, thus giving an indication of the local density. – Carl Witthoft Apr 17 '13 at 18:48

1 Answers1

0

I would use ggplot2 for this:

df = data.frame(x = seq(1:10), 
                y = c(4,3.8,3.8,3.2,3.1,2.5,2,1.5,1.2,1.3),
                size = c(7,20,2,70,100,70,5,80,110,2))

library(ggplot2)
ggplot(df, aes(x = x, y = y, size = size)) + geom_point()

enter image description here

ggplot2 scales the area of the maps according to the values in size, it does not directly use the size as cex. Therefore, the problem of overlapping is not present, and there is no need to manually tweak the cex.

Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • Thank you all! ggplot2 was helpful, but the partial transparency is also a nice alternative. Best, Sofia – user2291581 Apr 17 '13 at 20:53
  • Hi Paul, I decided to go for ggplot2. But I have one last question: is there a way to make the size of the points comparable across different plots that have different values of `size`? Otherwise, a plot with only 1's in `size`, for example, will have much larger points than the 1's in a plot where `size` takes other values as well. Thanks again, Sofia – user2291581 Apr 22 '13 at 07:49
  • I think it would be better if you ask a new question about this. This makes it easier for other users to also benefit from any answers. – Paul Hiemstra Apr 22 '13 at 07:51