geom_text在所有方面编写所有数据 [英] geom_text writing all data on all facets

查看:299
本文介绍了geom_text在所有方面编写所有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

>

1 10 1.35 378

2 10 1.35 2 p

3 10 1.35 50

4 10 1.35 26

解决方案

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



注意分隔符列在 facet_grid

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

产生:


I used ggplot with facet_grid and I'd like to indicate on each facet the number of observations in each facet. I follow examples provided on many sites but when I get it to write anything, it writes all four observation numbers on top of each other on all four plots.

Here the geom_text layer command: geom_text(data=ldata, aes(x=xpos, y=ypos, label=lab, size=1), group=NULL, hjust=0, parse=FALSE)

and ldata is a data frame listing the coordinates (xpos, ypos) on each plot and the number of observations (lab). It's printing the numbers in the right position on the plot, but all four are written on top of each other on all four plots. I can not figure out what I'm doing wrong.

ldata:

xpos ypos lab

1 10 1.35 378

2 10 1.35 2

3 10 1.35 50

4 10 1.35 26

解决方案

You almost have it. It is just that you need one more column in your ldata data frame which is what you will give to facet_grid. (I changed the Ypos to Inf)

Notice the role of the splitter column in ldata below, and how it is used in 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)

Which produces:

这篇关于geom_text在所有方面编写所有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆