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

Jos*_*eke 7 r overwrite ggplot2 facets

我使用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

Ram*_*han 10

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

请注意下面splitter列的作用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)
Run Code Online (Sandbox Code Playgroud)

哪个产生:

在此输入图像描述