我如何添加另一个图层/新系列到ggplot? [英] How can I add another layer / new series to a ggplot?

查看:136
本文介绍了我如何添加另一个图层/新系列到ggplot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

  ggplot(钻石,aes(x =克拉,y =价格))+ geom_point()+ aes(x =红宝石,y =价格)

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

解决方案

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

ggplot(钻石,aes(克拉,价格))+
geom_point()+
geom_point(data = rubies,color =red)


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.

解决方案

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")

这篇关于我如何添加另一个图层/新系列到ggplot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆