r - Adding points from other dataset to ggplot2 -


there many questions theme, not find 1 answered specific problem.

i have barplot (see testplot1 , testplot3 below) plotting dataset (bardata below) , want add points dataset (pointdata). see 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 fine, testplot2 gives error

error in factor(colorname) : object 'colorname' not found.

i not understand why says this, , know, not main problem since there 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 time program says:

error: aesthetics must either length one, or same length dataproblems:xname, ypos, factor(ptyname).

so question is: mean? both aes , data of length 3. number of rows in pointdata less in bardata, in not problem, see instance answer: https://stackoverflow.com/a/2330825/2298323

so going on here? (and how points in plot?)

the issue assigning fill = factor(colorname)for whole plot in qplot call.

so testplot2 try map colorname fill aesthetic there no colorname column in pointdata data.frame why have error message. if rewrite using ggplot, looks :

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 need apply mapping geom_bar call, :

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))) 

Comments

Popular posts from this blog

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

php - phpWord - Tables with borders in headers/footers -