I would like to visualize several datasets with ggplot as a scatterplot. The problem is that the point sizes are not comparable. The point for 20 for exampe in plot 1 is larger than in plot 2. The problem can be solved with the limits command. However, I would enlarge all points altogether. This is possible with the range command. However, the sizes of the points are then again not comparable. Is there a possibility to combine both commands or an alternative?
data <- data.frame (
A = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"),
B = c(0.5,1.5,20.3,10.5,22,3,8.5,5.5,8.8,12.5,14.6,2.4)
)
data2 <- data.frame (
A = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"),
B = c(0.2,2.5,40.4,20.5,22,3,14.9,5.5,8.8,18.8,33.6,2.4)
)
# not comparable
ggplot(data, aes(x=A, y=B)) geom_point(data=data, aes(x=A, y=B, size= B))
ggtitle("plot 1")
ggplot(data2, aes(x=A, y=B)) geom_point(data=data2, aes(x=A, y=B, size= B))
ggtitle("plot 2")
# comparable, but small points
ggplot(data, aes(x=A, y=B)) geom_point(data=data, aes(x=A, y=B, size= B))
scale_size_continuous(limits =c(1,40))
ggtitle("plot 1 limits")
ggplot(data2, aes(x=A, y=B)) geom_point(data=data, aes(x=A, y=B, size= B))
scale_size_continuous(limits =c(1,40))
ggtitle("plot 2 limits")
# bigger points, but not comparable
ggplot(data, aes(x=A, y=B)) geom_point(data=data, aes(x=A, y=B, size= B))
scale_size_continuous(range =c(1,10))
ggtitle("plot 1 range")
ggplot(data2, aes(x=A, y=B)) geom_point(data=data, aes(x=A, y=B, size= B))
scale_size_continuous(range =c(1,10))
ggtitle("plot 2 range")
CodePudding user response:
You can combine both limits and range within scale_continuous. Better, however, is to merge both data sets and use facets.
library(tidyverse)
library(patchwork)
data1 <- data.frame (
A = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"),
B = c(0.5,1.5,20.3,10.5,22,3,8.5,5.5,8.8,12.5,14.6,2.4)
)
data2 <- data.frame (
A = c("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l"),
B = c(0.2,2.5,40.4,20.5,22,3,14.9,5.5,8.8,18.8,33.6,2.4)
)
ggplot(data1, aes(x=A, y=B)) geom_point(aes(x=A, y=B, size= B))
scale_size_continuous(limits =c(1,40), range =c(1,10))
ggtitle("plot 1 limits")
ggplot(data2, aes(x=A, y=B)) geom_point(aes(x=A, y=B, size= B))
scale_size_continuous(limits =c(1,40), range =c(1,10))
ggtitle("plot 2 limits")
#> Warning: Removed 1 rows containing missing values (geom_point).
#> Warning: Removed 2 rows containing missing values (geom_point).
# or, more ggplot2-like
bind_rows(data1, data2, .id = "data") %>%
ggplot(aes(x=A, y=B))
geom_point(aes(x=A, y=B, size= B))
facet_grid(~data)
Created on 2022-08-11 by the reprex package (v2.0.1)