将其他数据集中的点添加到ggplot2

时间:2015-06-26 09:15:16

标签: r ggplot2

关于这个主题已经有很多问题了,但我找不到能够回答我这个问题的问题。

我有barplot(请参阅下面的testplot1testplot3)绘制数据集(下面为bardata),并希望从其他数据集添加点数({{ 1}})。请参阅简化示例:

pointdata

现在bardata <- data.frame( xname = c(1, 1, 1, 2, 2, 2, 3, 3, 3), yvalue = c(1, 2, 3, 2, 3, 1, 4, 2, 1), colorname = c("a", "b", "c", "a", "b", "c", "a", "b", "c") ) pointdata <- data.frame( xname = c(1, 1, 3), ypos = c(2, 4, 3), ptyname = c("p", "q", "r") ) testplot1 <- qplot(xname, yvalue, data= bardata, stat = "identity", fill= factor(colorname), geom = "bar") testplot2 <- testplot1 + geom_point(data = pointdata, mapping = aes(x = xname, y = ypos, shape = factor(ptyname)) ) 完全正常,但testplot1给出了错误

  

因子(colorname)出错:找不到对象'colorname'。

我不明白为什么他这么说,并且想知道,但这不是我的主要问题,因为有一个简单的解决方法,请参阅下面的testplot2

testplot3

现在这个程序说:

  

错误:美学必须是长度1或与长度相同   dataProblems:xname,ypos,factor(ptyname)。

所以我的问题是:这甚至意味着什么?显然, testplot3 <- qplot(xname, yvalue, data= bardata, stat = "identity", fill= factor(bardata$colorname), geom = "bar") testplot4 <- testplot3 + geom_point(data = pointdata, mapping = aes(x = xname, y = ypos, shape = factor(ptyname))) 和数据的长度均为3. aes中的行数小于pointdata中的行数,但这本身不是问题,请参阅此答案:https://stackoverflow.com/a/2330825/2298323

那么这里发生了什么? (我怎样才能在我的情节中获得积分?)

2 个答案:

答案 0 :(得分:12)

The issue is that you are assigning fill = factor(colorname)for the whole plot in your qplot call.

So testplot2 will also try to map colorname to the fill aesthetic but there is no colorname column in the pointdata data.frame which is why you have this error message. If you rewrite it using ggplot, it looks like this :

ggplot(bardata, aes(xname, yvalue, fill = factor(colorname))) +
  geom_bar(stat = "identity")+ 
  geom_point(data = pointdata, 
             mapping = aes(x = xname, y = ypos, shape = factor(ptyname)))

What you need to do is to apply the mapping only to the geom_bar call, like this :

ggplot(bardata, aes(xname, yvalue)) +
  geom_bar(stat = "identity", aes(fill = factor(colorname)))+ 
  geom_point(data = pointdata, 
             mapping = aes(x = xname, y = ypos, shape = factor(ptyname)))

答案 1 :(得分:5)

Yeah, sometimes ggplot error descriptions are quite difficult to understand. First note: try to avoid qplot, for rather complicated plots it tends to obscure things. Your code is equivalent to

ggplot(bardata, aes(xname, yvalue, fill = factor(colorname))) + 
  geom_bar(stat = "identity") + 
  geom_point(data = pointdata, aes(x = xname, y = ypos, shape = factor(ptyname))

#Error in factor(colorname) : object 'colorname' not found

And here's the problem: when you specify aes mapping within ggplot() (or qplot() in your case), this setting is automatically applied to any subsequent geom. You specified x, y and fill. For geom_bar, everything is okay. For geom_point you override x and y, but the fill is still mapped to colorname which doesn't exist in pointdata, therefore the error.

If you mix several data frames, here's the recommended way to go: empty ggplot() plus specific aes for each geom.

ggplot() + 
  geom_bar(data = bardata, aes(xname, yvalue, fill = factor(colorname)), stat = "identity") + 
  geom_point(data = pointdata, aes(xname, ypos, shape = factor(ptyname)))

enter image description here

相关问题