使用geom_abline()和ggplot [英] Using geom_abline() and ggplot

查看:313
本文介绍了使用geom_abline()和ggplot的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是ggplot2的初学者,距我开始尝试仅4天.因此,对于这个问题听起来太基础了,我深表歉意.任何指导,我将不胜感激-我已经为这个问题苦苦挣扎了大约一个小时.

I am a beginner in ggplot2--it's been only 4 days since I have started experimenting with it. So, I apologize if this question sounds too basic. I'd appreciate any guidance--I've been struggling with this issue for about an hour.

我正在尝试使用geom_abline(),如下所示:

I was experimenting with using geom_abline() as below:

 p <- ggplot(mpg, aes(cty, hwy)) + geom_point()
 p + geom_abline() + facet_wrap(~cyl)

这可以像我在以下所有四个多面图中看到的参考线一样工作:

This works as in I can see a reference line in all four faceted graphs as below:

后来,我正在使用另一个相关的数据集mtcars来查看geom_abline()

Later, I was using another related dataset mtcars to see what happens to geom_abline()

  p <- ggplot(mtcars, aes(wt, mpg)) + geom_point()
     p + geom_abline() + facet_wrap(~cyl)

但是,当我运行此命令时,却没有看到geom_abline().,我在帮助文件中发现了上述命令的类似版本,并说" geom_abline()超出范围"

However, when I ran this command, I couldn't see geom_abline(). Quite surprisingly, I found similar version of above command in the help file, and it says "geom_abline() is out of the range"

我知道超出范围"是什么意思,但是如何知道在特定数据集中abline()是否超出范围?我可以通过强制它使用特定的斜率和截距来覆盖它,但随后我会认为这有点骇人听闻-即看到输出后修改代码.我有什么办法可以知道geom_abline()

While I know what "out of range" means, but how do I know whether in a particular dataset, abline() will be out of range? I can override it by forcing it to use a specific slope and an intercept, but then I'd consider this a bit of hacking--i.e. modifying the code after seeing the output. Is there any way I can know what happens behind the scenes for geom_abline()

这是我没有任何abline()的图表

Here's the graph I got without any abline()s

我将不胜感激.我真的很困惑.

I'd appreciate any thoughts. I am really confused.

推荐答案

如果不提供任何参数,则geom_abline()使用默认值intercept = 0slope = 1.

If you don't give any arguments, geom_abline() uses default values, intercept = 0 and slope = 1.

ggplot(mpg, aes(cty, hwy)) + geom_point() + geom_abline() + lims(x = c(0,35), y = c(0,50))
ggplot(mtcars, aes(wt, mpg)) + geom_point() + geom_abline() + lims(x = c(0,6), y = c(0,35))

这篇关于使用geom_abline()和ggplot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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