bibzzzz
bibzzzz

Reputation: 191

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

library(ggplot2)

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)) +
  geom_point()

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

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:

df_1_plot

df2_plot

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.

Upvotes: 4

Views: 1535

Answers (1)

bibzzzz
bibzzzz

Reputation: 191

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.

Upvotes: 1

Related Questions