Reputation: 935
I am building a plot whereby each point has a size and colour based on a linear gradient in two separate columns
df1 <- data.frame (x = c(1:10), y = c(1:10), pointSize = 1:10, pointCol = 1:10)
ggplot(df1, aes(x = x, y = y, colour = pointCol, size = pointSize)) + geom_point() +
scale_colour_gradient(low = "steelblue", high = "yellow")
the maximum value in the table for columns encoding color and point size are 10. Can I change the gradient sizes so that it goes from say = 10 to 20?
Upvotes: 2
Views: 5147
Reputation: 29085
You can define the same size scale limits for both plots. Here's an example:
Sample datasets:
df1 <- data.frame (x = 1:10, y = 1:10, pointSize = 1:10, pointCol = 1:10)
df2 <- data.frame (x = 1:10, y = 10:1, pointSize = 11:20, pointCol = 1:10)
Plot without standardising the scale for size (we can see size 10 in plot 1 matches size 20 in plot 2):
p1 <- ggplot(df1, aes(x = x, y = y, colour = pointCol, size = pointSize)) +
geom_point() +
scale_colour_gradient(low = "steelblue", high = "yellow") +
theme_bw()
p2 <- ggplot(df2, aes(x = x, y = y, colour = pointCol, size = pointSize)) +
geom_point() +
scale_colour_gradient(low = "steelblue", high = "yellow") +
theme_bw()
gridExtra::grid.arrange(p1, p2, nrow = 1)
Now let's define the same scale limits for both:
size.limits <- range(df1$pointSize, df2$pointSize)
> size.limits
[1] 1 20
Add the scales for size to each plot (we can see now the two plots share the same size legend, and the largest point in plot 1 is the same size as the smallest point in plot 2):
gridExtra::grid.arrange(p1 + scale_size(limits = size.limits),
p2 + scale_size(limits = size.limits),
nrow = 1)
Upvotes: 6