There are already many questions about this theme, but I could not find one that answered my specific problem.
I have a barplot
(see testplot1
and testplot3
below) plotting a dataset (bardata
below) and want to add points to it from another dataset (pointdata
). See the simplified example:
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))
)
Now testplot1
works perfectly fine, but testplot2
gives the error
Error in factor(colorname) : object 'colorname' not found.
I do not understand why he says this, and would like to know, but this is not my main problem since there is an easy workaround, see testplot3
below.
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)))
Now this time the program says:
Error: Aesthetics must either be length one, or the same length as the dataProblems:xname, ypos, factor(ptyname).
So my question is: what does this even mean? Obviously both aes
and the data are of length 3. The number of rows in pointdata
is less than that in bardata
, but that is in itself not a problem, see for instance this answer: https://stackoverflow.com/a/2330825/2298323
So what is going on here? (And how do I get my points in my plot?)