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

发布于 2024-08-23 01:13:27 字数 408 浏览 5 评论 0原文

在 ggplot 中,我可以将一个系列添加到绘图中:

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

如何简单地添加另一个系列,例如绘制红宝石与钻石的成本。假设红宝石也在钻石数据集中。我尝试在上面放置另一层红宝石数据,但它只绘制红宝石而不是钻石/克拉。

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦在深巷 2024-08-30 01:13:27
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")
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")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文