在一个气泡图中实现来自多个数据框的数据
Implement data from several data frames in one bubble plot
我有一个尺寸为 625616 x 12 的数据框。我想用气泡图来说明数据。为了说明我的情况,我将使用 mtcars 数据集。
mtcars$cyl = as.factor(mtcars$cyl)
bp = ggplot(as.data.frame(mtcars), aes(x = wt, y = mpg, size = qsec)) + geom_point(shape = 21)
bp
类似于我的数据框,我在这个命令中使用了 12 列中的 3 列的数据。理想情况下,我想在此气泡图中添加另一组另一种颜色的气泡(第 4-6 列)。
我尝试使用 "add" 函数。
bp2 = ggplot(as.data.frame(mtcars), aes(x = wt2, y = mpg2, size = qsec2)) + geom_point(shape = 21)
plot(bp2, add = T)
不幸的是,它也没有成功。
如果您在同一数据集中有不同的 x
、y
和 size
变量,您可以在美学中为每个 geom_point
df <- data.frame(x1 = rnorm(20), y1 = rnorm(20),
x2 = rnorm(20), y2 = rnorm(20),
z1 = rnorm(20), z2 = rnorm(20))
ggplot(df) +
geom_point(aes(x = x1, y = y1, size = z1), col = "red") +
geom_point(aes(x = x2, y = y2, size = z2), col = "blue")
如果您有两个不同的数据集,您也可以在 geoms 中定义它们:
ggplot() +
geom_point(aes(x = x1, y = y1, size = z1), col = "red", data = df1) +
geom_point(aes(x = x2, y = y2, size = z2), col = "blue",data = df2)
根据您的评论进行编辑:您可以通过例如更改点的整体大小使用 scale_size_continuous(range = c(0, 10))
并将 10 更改为另一个值。
我有一个尺寸为 625616 x 12 的数据框。我想用气泡图来说明数据。为了说明我的情况,我将使用 mtcars 数据集。
mtcars$cyl = as.factor(mtcars$cyl)
bp = ggplot(as.data.frame(mtcars), aes(x = wt, y = mpg, size = qsec)) + geom_point(shape = 21)
bp
类似于我的数据框,我在这个命令中使用了 12 列中的 3 列的数据。理想情况下,我想在此气泡图中添加另一组另一种颜色的气泡(第 4-6 列)。
我尝试使用 "add" 函数。
bp2 = ggplot(as.data.frame(mtcars), aes(x = wt2, y = mpg2, size = qsec2)) + geom_point(shape = 21)
plot(bp2, add = T)
不幸的是,它也没有成功。
如果您在同一数据集中有不同的 x
、y
和 size
变量,您可以在美学中为每个 geom_point
df <- data.frame(x1 = rnorm(20), y1 = rnorm(20),
x2 = rnorm(20), y2 = rnorm(20),
z1 = rnorm(20), z2 = rnorm(20))
ggplot(df) +
geom_point(aes(x = x1, y = y1, size = z1), col = "red") +
geom_point(aes(x = x2, y = y2, size = z2), col = "blue")
如果您有两个不同的数据集,您也可以在 geoms 中定义它们:
ggplot() +
geom_point(aes(x = x1, y = y1, size = z1), col = "red", data = df1) +
geom_point(aes(x = x2, y = y2, size = z2), col = "blue",data = df2)
根据您的评论进行编辑:您可以通过例如更改点的整体大小使用 scale_size_continuous(range = c(0, 10))
并将 10 更改为另一个值。