Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using multiple size scales in a ggplot

I'm trying to construct a plot which shows transitions from one class to another. I want to have circles representing each class sized according to a class attribute, and arrows from one class to another, sized according to the number of transitions from one class to another.

As an example:

library(ggplot2)
points <- data.frame( x=runif(10), y=runif(10),class=1:10, size=runif(10,min=1000,max=100000) )
trans <- data.frame( from=rep(1:10,times=10), to=rep(1:10,each=10), amount=runif(100)^3 )
trans <- merge( trans, points, by.x="from", by.y="class" )
trans <- merge( trans, points, by.x="to", by.y="class", suffixes=c(".to",".from") )
ggplot( points, aes( x=x, y=y ) ) + geom_point(aes(size=size),color="red") + 
    scale_size_continuous(range=c(4,20)) + 
    geom_segment( data=trans, aes( x=x.from, y=y.from, xend=x.to, yend=y.to, size=amount ),lineend="round",arrow=arrow(),alpha=0.5)

Example image

I'd like to be able to scale the arrows on a different scale to the circles. Ideally, I'd like a legend with both scales on, but I understand this may not be possible (using two scale colour gradients on one ggplot)

Is there a more elegant way to do this than applying arbitrary scaling to the underlying data?

like image 744
mo-seph Avatar asked Feb 01 '13 13:02

mo-seph


1 Answers

A nice option is to generate the circumference of your classes as a series of points, adjusting the scale (diameter) according to your data. Then you draw the circles either as paths or polygons.

Follows some example code. The circleFun was shared by @joran in a previous post. Does this work? I think you should tweak the circle scales acording to your real data.

Important note:
Also, from your use of arrow without attaching grid, I assume you have not updated ggplot2. I changed that code to work with my setup, and tried not to include any ggplot2 code that might cause backward compatibility issues.

# Load packages
library(package=ggplot2)  # You should update ggplot2
library(package=plyr)     # To proccess each class separately


# Your data generating code
points <- data.frame(x=runif(10), y=runif(10),class=1:10,
                     size=runif(10,min=1000,max=100000) )
trans <- data.frame(from=rep(1:10,times=10), to=rep(1:10,each=10),
                    amount=runif(100)^3 )
trans <- merge(trans, points, by.x="from", by.y="class" )
trans <- merge(trans, points, by.x="to", by.y="class", suffixes=c(".to",".from") )


# Generate a set of points in a circumference
# Originally posted by @joran in
# https://stackoverflow.com/questions/6862742/draw-a-circle-with-ggplot2
circleFun <- function(center = c(0,0), diameter = 1, npoints = 100){
    r = diameter / 2
    tt <- seq(0,2*pi,length.out = npoints)
    xx <- center[1] + r * cos(tt)
    yy <- center[2] + r * sin(tt)
    return(data.frame(x = xx, y = yy))
}


# Get max and min sizes and min distances to estimate circle scales
min_size <- min(points$size, na.rm=TRUE)
max_size <- max(points$size, na.rm=TRUE)
xs <- apply(X=combn(x=points$x, m=2), MARGIN=2, diff, na.rm=TRUE)
ys <- apply(X=combn(x=points$y, m=2), MARGIN=2, diff, na.rm=TRUE)
min_dist <- min(abs(c(xs, ys)))  # Seems too small
mean_dist <- mean(abs(c(xs, ys)))

# Adjust sizes
points$fit_size <- points$size * (mean_dist/max_size)


# Generate the circles based on the points
circles <- ddply(.data=points, .variables='class',
                 .fun=function(class){
                    with(class,
                    circleFun(center = c(x, y), diameter=fit_size))
                 })
circles <- merge(circles, points[, c('class', 'size', 'fit_size')])


# Plot
ggplot(data=circles, aes(x=x, y=y)) +
    geom_polygon(aes(group=factor(class), fill=size)) + 
    geom_segment(data=trans,
                 aes(x=x.from, y=y.from, xend=x.to, yend=y.to, size=amount),
                 alpha=0.6, lineend="round", arrow=grid::arrow()) +
    coord_equal()
like image 51
Oscar de León Avatar answered Sep 28 '22 06:09

Oscar de León