`geom_abline`和`facet_wrap`似乎不兼容 [英] `geom_abline` and `facet_wrap` seem incompatible

查看:49
本文介绍了`geom_abline`和`facet_wrap`似乎不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在与 facet_wrap facet_grid 相同的情节中使用 geom_abline 时出现错误,我不明白为什么.例如

I get errors when I use geom_abline in the same plot as facet_wrap or facet_grid, and I don't understand why. For example

# Example data
ex <- data.frame(x=1:10, y=1:10, f=gl(2, 5))

ggplot() + 
  geom_point(data=ex, aes(x=x, y=y)) +
  geom_abline(slope=1, intercept=0) + 
  facet_wrap(~f)

导致 if(empty(data)){错误:缺少需要TRUE/FALSE的值.

上面,我在 geom_point 层中设置了数据,因为稍后我将添加来自其他数据帧的数据.这与问题有关,因为当我在基础层中设置数据时,我得到了另一个错误:

Above I set the data in the geom_point layer because later on I will add data from a different data frame. This has something to do with the problem, because when I set the data in the base layer I get a different error:

ggplot(ex, aes(x=x, y=y)) + 
  geom_abline(slope=1, intercept=0) +
  facet_wrap(~f)

as.environment(where)中的错误:'where'丢失

有一个简单的解决方法:如果我创建一个数据框来定义1:1线并使用 geom_line 对其进行绘制,则基本上可以得到与 geom_abline 相同的图代码> ...

There's an easy workaround: If I make a data frame to define a 1:1 line and plot it using geom_line I get essentially the same plot I would have gotten from geom_abline...

# Define a 1:1 line with data
one_to_one <- data.frame(xO=range(ex$totalcells), yO=range(ex$totalcells))

# Plot the 1:1 line with geom_line
ggplot() + 
  geom_point(data=ex, aes(x=x, y=y)) +
  geom_line(data=one_to_one, aes(x=xO, y=yO), colour="black") +
  facet_wrap(~f)

... 所以这个问题更多地是关于为什么出现这些错误(以及它们代表错误还是预期的行为),而不是如何解决该问题.

...so this question is more about why those errors arise (and whether they represent a bug or expected behavior) rather than how to work around the problem.

推荐答案

以下作品:

ggplot(ex, aes(x=x, y=y)) + geom_point() + 
  geom_abline(slope=1, intercept=0) +
  facet_wrap(~f)

根据您的第二个示例,请注意我添加的其他 geom_point().

Note the additional geom_point() I added, based on your second example.

这篇关于`geom_abline`和`facet_wrap`似乎不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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