首页 文章

geom_text在所有方面写入所有数据

提问于
浏览
7

我使用ggplot和facet_grid,我想在每个方面指出每个方面的观察数量 . 我按照许多网站上提供的示例进行操作,但是当我将其写入任何内容时,它会在所有四个图上将所有四个观察数字叠加在一起 .

这里是geom_text图层命令:geom_text(data = ldata,aes(x = xpos,y = ypos,label = lab,size = 1),group = NULL,hjust = 0,parse = FALSE)

和ldata是一个数据框,列出了每个图上的坐标(xpos,ypos)和观察数(实验室) . 它在图上的正确位置打印数字,但所有四个都在所有四个图上相互重叠 . 我无法弄清楚我做错了什么 .

LDATA:

xpos ypos实验室

1 10 1.35 378

2 10 1.35 2

3 10 1.35 50

4 10 1.35 26

1 回答

  • 10

    你几乎拥有它 . 只是你需要在 ldata 数据框中再添一列,这就是你给 facet_grid 的内容 . (我将Ypos改为Inf)

    请注意 ldata 列在 ldata 中的作用,以及如何在 facet_grid 中使用它

    xpos <- c(10,10,10) 
    ypos <- c(Inf,Inf,Inf)
    lab <- c(378,2,50)
    splitter <- c(1:3)
    ldata <- data.frame(xpos, ypos, lab, splitter)
    
    
    ggplot(mtcars) + geom_bar(aes(x=cyl)) + facet_grid(~splitter) + 
      geom_text(data=ldata, aes(x=xpos, y=ypos, 
                                label=lab, size=1), 
                vjust=2, parse=FALSE)
    

    哪个产生:

    enter image description here

相关问题