Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Size of points in ggplot2 comparable across plots?

Tags:

r

ggplot2

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

like image 465
user2291581 Avatar asked Apr 22 '13 09:04

user2291581


People also ask

What is the difference between plot and Ggplot in R?

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.

What does GG stand for in Ggplot?

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].


1 Answers

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")

enter image description here

enter image description here

UPDATE

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))

enter image description hereenter image description here

like image 167
Didzis Elferts Avatar answered Nov 15 '22 13:11

Didzis Elferts