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
The base plotting paradigm is "ink on paper" whereas the lattice and ggplot paradigms are basically writing a program that uses the grid -package to accomplish the low-level output to the target graphics devices.
ggplot2 [library(ggplot2)] ) is a plotting library for R developed by Hadley Wickham, based on Leland Wilkinson's landmark book The Grammar of Graphics ["gg" stands for Grammar of Graphics].
One possibility is to use scale_size_identity()
- that will interpret size
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 if size
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 argument trans="sqrt"
.
ggplot(df1, aes(x = x, y = y, size = size)) +
geom_point()+scale_size_identity(trans="sqrt",guide="legend")
ggplot(df2, aes(x = x, y = y, size = size)) +
geom_point()+scale_size_identity(trans="sqrt",guide="legend")
As pointed out by @hadley, easiest way to achieve this is to set limits=
inside scale_size_continuous()
to the same values to get identical sizes.
ggplot(df1, aes(x = x, y = y, size = size)) + geom_point()+
scale_size_continuous(limits=c(1,110))
ggplot(df2, aes(x = x, y = y, size = size)) + geom_point()+
scale_size_continuous(limits=c(1,110))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With