Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proportionally sized symbols in ggplot

Tags:

r

ggplot2

Using ggplot to plot proportional area symbols seems to require using sqrt() to achieve true proportionality:

require(ggplot2)

t <- data.frame(x=rep(c(1:5),5), y=rep(c(1:5),each=5), s=round(seq(1,100,length.out=25)))
t
p <- ggplot(data=t, aes(x=x,y=y))

# direct size-to-variable mapping
p + geom_point(aes(size=s), pch=22, fill='#0000FF75', col=NA) +
  scale_size(range = c(1, 40)) +
  geom_text(data=t, aes(x=x,y=y,label=s),size=3,vjust=1)

# proportional area size-to-variable mapping
p + geom_point(aes(size=sqrt(s)), pch=22, fill='#0000FF75', col=NA) +
  scale_size(range = c(1, 40)) +
  geom_text(data=t, aes(x=x,y=y,label=s),size=3,vjust=1)

enter image description here

As you can see the labels are rooted when I need them to show the original data. Have tried playing with the scale_size options but nothing works. Anyone know a fix to this, or is there maybe an obscure setting to achieve proportional area size mapping?

Thanks in advance.

like image 920
geotheory Avatar asked Sep 01 '12 12:09

geotheory


1 Answers

You can use scale_area instead of scale_size:

p + geom_point(aes(size=s), pch=22, fill='#0000FF75', col=NA) +
  scale_area(range = c(1, 40)) +
  geom_text(data=t, aes(x=x,y=y,label=s),size=3,vjust=1)

enter image description here


I agree, it's not entirely obvious, but not that obsure either - there is an example using scale_area in the help for ?scale_size.

like image 139
Andrie Avatar answered Oct 19 '22 16:10

Andrie