Home Ask Login Register

Developers Planet

Your answer is one click away!

bibzzzz February 2016

Is there a way to maintain absolute control over the size scale in ggplot2

I would like to specify the size of a point based on the value of a particular field, however I'd like to be able to have a size scale that spans across a series of independently generated charts.

Essentially I would like to be able to say that a value of X units should be displayed with a point of size Y.

An example is below


df_1 <- data.frame(x=c(1:3),y=rep(1,3),size=10*c(1:3))
df_2 <- data.frame(x=c(1:3),y=rep(1,3),size=100*c(1:3))

df_1_plot <- ggplot(df_1,aes(x=x,y=y,size=size)) +

df_2_plot <- ggplot(df_2,aes(x=x,y=y,size=size)) +

df_1_plot will produce a chart on a different scale to df_2_plot, although df_2 size field is 10 times the size of df_1's:



I'm looking for the point size in the df_2_plot to be 10 times larger than the points in the df_1_plot.


bibzzzz February 2016

One way to achieve this is to adjust the scales manually to fix them to the original data frames scale using scale_size_continuous() along with a specified scaling constant - any constant should work, although using min(df_1$size) keeps the sizes more manageable. That way every point is sized in relation to the same arbitrary constant (in this case min(df_1$size))

The code would then look something like:

ggplot(df_1,aes(x=x,y=y,size=size)) + 
geom_point() + 
scale_size_continuous(range = c(min(df_1$size)/min(df_1$size), max(df_1$size)/min(df_1$size))) 

ggplot(df_2,aes(x=x,y=y,size=size)) + 
geom_point() + 
scale_size_continuous(range = c(min(df_2$size)/min(df_1$size), max(df_2$size)/min(df_1$size)))

However as lukeA mentioned using scale_size_identity() instead of scale_size_continuous() achieves the equivalent result in a much more elegant way.

Post Status

Asked in February 2016
Viewed 2,906 times
Voted 10
Answered 1 times


Leave an answer

Quote of the day: live life