ggplot使用第二个数据源作为误差条失败

3
这是对之前有关获取自定义误差条的问题的后续。
  1. 图表的外观符合我的需求,因此不必担心仅针对此进行评论(但很乐意听取与其他帮助相关的意见)
  2. 由于这些图是在循环中生成的,而且只有在满足条件时才会添加误差线,所以我无法简单地将所有数据合并到前面,因此假设为此练习中的绘图数据和误差条数据来自不同的dfs。
我有一个ggplot,试图使用不同的数据框添加一些误差线。当我调用绘图时,它说找不到父绘图的y值,即使我只是尝试使用新数据添加误差线。我知道这必须是语法错误,但我被卡住了...
首先让我们生成数据和图表。
library(ggplot2)
library(scales)

# some data
data.2015 = data.frame(score = c(-50,20,15,-40,-10,60),
                       area = c("first","second","third","first","second","third"),
                       group = c("Findings","Findings","Findings","Benchmark","Benchmark","Benchmark"))

data.2014 = data.frame(score = c(-30,40,-15),
                       area = c("first","second","third"),
                       group = c("Findings","Findings","Findings"))

# breaks and limits
breaks.major = c(-60,-40,-22.5,-10, 0,10, 22.5, 40, 60)
breaks.minor = c(-50,-30,-15,-5,0, 5, 15,30,50) 
limits =c(-70,70)

# plot 2015 data
ggplot(data.2015, aes(x = area, y = score, fill = group)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.9)) +
  coord_flip() +
  scale_y_continuous(limit = limits, oob = squish, minor_breaks = breaks.minor, 
                     breaks = breaks.major)

调用plot(c)会产生一个漂亮的图表,现在让我们设置误差条并尝试将它们作为新层添加到图表“c”中。

# get the error bar values
alldat = merge(data.2015, data.2014, all = TRUE, by = c("area", "group"), 
               suffixes = c(".2015", ".2014"))
alldat$plotscore = with(alldat, ifelse(is.na(score.2014), NA, score.2015))
alldat$direction = with(alldat, ifelse(score.2015 < score.2014, "dec", "inc"))
alldat$direction[is.na(alldat$score.2014)] = "absent"

#add error bars to original plot
c <- c+
  geom_errorbar(data=alldat, aes(ymin = plotscore, ymax = score.2014, color = direction), 
                position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE)

当我现在调用c时,我会得到以下结果:
"Error in eval(expr, envir, enclos) : object 'score' not found"

当我只想使用第二个alldat数据框覆盖geom_errorbar时,为什么它会寻找data.2015 $ score?编辑* 我已经尝试使用alldata $ plotscore和alldat $ score.2014指定误差条的ymin / ymax值(我确定这是不好的做法),它可以绘制,但条形的位置错误/与图形不一致(例如交换,位于基准条上等)。

看起来 geom_errobar 继承了你在 ggplot 中设置的全局美学中的 y 美学,而变量 score 不在第二个数据集中。要么在新数据集中将该列命名为 score(也许可以在 merge 中尝试一下 suffixes 参数),要么在 geom_errorbar 的美学中使用 y = score.2015 - aosmith
@aomith 能否将其添加为解决方案,这样我就可以标记我的问题已解决了吗?对我有用,通过在 aes geom_errorbar 中指定新数据框中的 "y" 和 "fill" 作为值来解决了这个问题(先做 y,然后它还要求填充)。其他人可能会来到这里想知道如何解决这个问题,但也可能想知道如何规避这个问题,如果他们在新数据框中没有匹配的数据(即只有条形图数据,没有任何替换 y 和 fill 的数据以匹配父级)。 - Alex
1个回答

3

根据我的经验,这个关于找不到某些变量的错误告诉我R去数据框中查找一个变量,但是它并不存在。有时解决方法很简单,只需修复一个拼写错误,但在你的情况下,score变量不在你用来制作误差棒的数据集中。

names(alldat)
[1] "area"       "group"      "score.2015" "score.2014" "plotscore"  "direction"
< p > y 变量是 geom_errorbar 所必需的美学要素。因为您在 ggplot 中全局设置了一个 y 变量,所以其他图形都会继承全局的 y 变量,除非您将其映射到不同的变量。在当前数据集中,您需要将 y 映射到2015年的分数变量。

geom_errorbar(data=alldat, aes(y = score.2015, ymin = plotscore, 
                               ymax = score.2014, color = direction), 
              position = position_dodge(width = .9), lwd = 1.5, show.legend = FALSE)

在您的评论中,您提到您还需要将fill添加到geom_errobar中,但是当我运行代码时,我发现这并不必要(您可以看到在您给出的示例中,group 是第二个数据集中的变量)。
另一个选择是确保合并后的2015年分数变量仍然命名为score。这可以通过更改merge中的suffixes参数来完成。然后,score将在第二个数据集中,您就不必设置geom_errorbar中的y变量了。
alldat2 = merge(data.2015, data.2014, all = TRUE, by = c("area", "group"), 
            suffixes = c("", ".2014"))
...
names(alldat2)
[1] "area"       "group"      "score"      "score.2014" "plotscore"  "direction" 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接