I am using ggplot2 to produce various plots in which the size of a point is proportional to the number of cases that have the same values of x and y. Is there a way to make the size of the points comparable across different plots that have different values of size
?
Example using fake data:
df1 = 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(1,20,1,70,100,70,1,1,110,1))
library(ggplot2)
pdf("plot.1.pdf")
ggplot(df1, aes(x = x, y = y, size = size)) + geom_point()
dev.off()
df2 = 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 = rep(1,length(y)))
pdf("plot.2.pdf")
ggplot(df2, aes(x = x, y = y, size = size)) + geom_point()
dev.off()
The points in Plot 1, which all have size
equal to 1, are much larger than the points in Plot 2 for which size
equals 1. I need a version of the plots where points with the same value of size
have the same size across different plots. Thank you,
Sofia
One possibility is to use
scale_size_identity()
- that will interpretsize
directly as units of pointsize, so in both plots points with value 1 will be the same size. But this approach will make too large points ifsize
values are big (as in your case). To deal with problem of too big points, you can use transformation inside scale, for example, square root, with argumenttrans="sqrt"
.UPDATE
As pointed out by @hadley, easiest way to achieve this is to set
limits=
insidescale_size_continuous()
to the same values to get identical sizes.