如何向ggplot添加另一个层/新系列?

时间:2021-06-04 21:12:13

In ggplot I can add a series to a plot with:

在ggplot中,我可以在一个情节中添加一个系列:

ggplot(diamonds, aes(x = carat, y = price)) + geom_point()

How do I simply add another series, e.g. plotting the cost of rubies against diamonds. Assuming rubies was also in the diamonds dataset. I have tried to lay over the top another layer with the rubies data, but it just plots the rubies and not the diamonds/carat.

我如何简单地添加另一个系列,例如,绘制红宝石与钻石的价格。假设红宝石也在钻石数据集中。我试着在顶部的另一层铺上红宝石数据,但它只是绘制红宝石而不是钻石/克拉。

ggplot(diamonds, aes(x = carat, y = price)) + geom_point() + aes(x = rubies, y = price)

I can see that this would be possible by melding all the data together first, ready to plot it, so maybe I should go down that route. However, just adding another series to a plot like this seems like it should not be too hard, but I can't figure out how to do it.

我可以看到这是可能的通过将所有数据融合在一起,准备好绘图,也许我应该走那条路线。然而,仅仅在这样的情节中加入另一个系列似乎并不难,但我不知道怎么做。

1 个解决方案

#1


46  

rubies  <- data.frame(carat = c(3, 4, 5), price= c(5000, 5000, 5000))

ggplot(diamonds, aes(carat, price)) + 
  geom_point() + 
  geom_point(data = rubies, colour = "red")

#1


46  

rubies  <- data.frame(carat = c(3, 4, 5), price= c(5000, 5000, 5000))

ggplot(diamonds, aes(carat, price)) + 
  geom_point() + 
  geom_point(data = rubies, colour = "red")