在一个气泡图中实现来自多个数据框的数据

问题描述 投票:0回答:1

我有一个尺寸为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列)。

我试着使用“添加”功能。

bp2 = ggplot(as.data.frame(mtcars), aes(x = wt2, y = mpg2, size = qsec2)) + geom_point(shape = 21)
plot(bp2, add = T)

不幸的是,它也没有成功。

r plot bubble-chart
1个回答
0
投票

如果您在同一数据集中有不同的xysize变量,您可以在每个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")

enter image description here

如果您有两个不同的数据集,您也可以在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更改为另一个值。

© www.soinside.com 2019 - 2024. All rights reserved.