用每个方面的观察数量标注ggplot2方面 [英] Annotate ggplot2 facets with number of observations per facet

查看:132
本文介绍了用每个方面的观察数量标注ggplot2方面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:




偶尔在ggplot中创建数据时,我认为这样会很好使用落入每个方面的观察数量来标注每个方面。当分面可能导致每个方面的观察值相对较少时,这一点尤为重要。

将最佳/最简单的方法添加到每个方面的n = X这个情节?

  require(ggplot2)
mms < - data.frame(deliciousness = rnorm(100),
type = sample(as.factor(c(peanut,regular)),100,replace = TRUE),
color = sample(as.factor(c(red,绿色,黄色,棕色)),100,替换=真))


plot < - ggplot(data = mms,aes(x = deliciousness))+ geom_density()+ facet_grid(type〜color)


看起来这是之前已经问过的,我最初没有看到它们是如何连接的。我在这里回答这个问题,但是如果有人有更优雅的东西,就不要接受。另外,n = foo是一个常见的情况,希望有人能够从这个问题中得到一些用处,即使它有点重复。

 <$ c(c)要求(ggplot2)
要求(plyr)
mms < - data.frame(美味度= rnorm(100),
类型=样品(as.factor(c花生,常规)),
100,替换= TRUE),
color = sample(as.factor(c(red,green,yellow,brown)) ),
100,replace = TRUE))


mms.cor< - ddply(.data = mms,
。(type,color),
总结,
n = paste(n =,长度(美味)))

plot < - ggplot(data = mms,aes(x = deliciousness))+
geom_density()+
facet_grid(type〜color)+
geom_text(data = mms.cor,aes(x = 1.8,y = 5,label = n),
color =black,inherit.aes = FALSE,parse = FALSE)

plot


Possible Duplicate:
Creating a facet_wrap plot with ggplot2 with different annotations in each plot
Add text to a faceted plot in ggplot2 with dates on X axis

Occasionally when faceting data in ggplot, I think it would be nice to annotate each facet with the number of observations that fell into each facet. This is particularly important when faceting may result in relatively few observations per facet.

What would be the best / simplest way to add an "n=X" to each facet of this plot?

require(ggplot2)
mms <- data.frame(deliciousness = rnorm(100),
                  type=sample(as.factor(c("peanut", "regular")), 100, replace=TRUE),
                  color=sample(as.factor(c("red", "green", "yellow", "brown")), 100, replace=TRUE))


plot <- ggplot(data=mms, aes(x=deliciousness)) + geom_density() + facet_grid(type ~ color)

解决方案

It looks like this has been asked before, and I failed initially to see how they connected. I'm answering this here, but leaving it as not accepted in case someone has something more elegant. Also, the n = foo is a common enough case, that hopefully someone will get some use out of this question even though it's a bit duplicative.

require(ggplot2)
require(plyr)
mms <- data.frame(deliciousness = rnorm(100),
                  type=sample(as.factor(c("peanut", "regular")), 
                              100, replace=TRUE),
                  color=sample(as.factor(c("red", "green", "yellow", "brown")), 
                              100, replace=TRUE))


mms.cor <- ddply(.data=mms, 
                 .(type, color), 
                 summarize, 
                 n=paste("n =", length(deliciousness)))

plot <- ggplot(data=mms, aes(x=deliciousness)) + 
          geom_density() + 
          facet_grid(type ~ color) + 
          geom_text(data=mms.cor, aes(x=1.8, y=5, label=n), 
                    colour="black", inherit.aes=FALSE, parse=FALSE)

plot

这篇关于用每个方面的观察数量标注ggplot2方面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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