我在与geom_abline
或facet_wrap
相同的情节中使用facet_grid
时出错,我不明白为什么。例如
# 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)
导致Error in if (empty(data)) { : missing value where TRUE/FALSE needed
。
上面我在geom_point
层设置数据,因为稍后我将添加来自不同数据帧的数据。这与问题有关,因为当我在基础层中设置数据时,我得到一个不同的错误:
ggplot(ex, aes(x=x, y=y)) +
geom_abline(slope=1, intercept=0) +
facet_wrap(~f)
Error in as.environment(where) : 'where' is missing
有一个简单的解决方法:如果我创建一个数据框来定义1:1的行并使用geom_line
绘制它,我会从geom_abline
得到基本相同的图... ... < / p>
# 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)
... 所以这个问题更多地是关于为什么出现 (以及它们是代表错误还是预期行为)而不是如何解决问题。
答案 0 :(得分:2)
以下作品:
ggplot(ex, aes(x=x, y=y)) + geom_point() +
geom_abline(slope=1, intercept=0) +
facet_wrap(~f)
请注意我添加的额外geom_point()
,基于您的第二个示例。