Adding points from other dataset to ggplot2

前端 未结 2 942
自闭症患者
自闭症患者 2021-02-19 21:26

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

2条回答
  •  迷失自我
    2021-02-19 22:15

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

提交回复
热议问题