如何获得geom_vline来尊重facet_wrap? [英] How to get geom_vline to honor facet_wrap?

查看:155
本文介绍了如何获得geom_vline来尊重facet_wrap?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我捅了一圈,但一直无法找到答案。我想要做一个加权的geom_bar图,用垂直线覆盖,显示每个方面的总体加权平均值。我无法做到这一点。垂直线似乎适用于所有方面的单个值。

  require('ggplot2')
require(' (A,A,A,A,A,A,B,B,B ,B,B,B,B,B,B,B,B,B)
instrument <-c , V2, V1, V1, V1, V2, V1, V1, V2, V1, V1, V2, V1, V1,V2,V1)
cost <-c(1,4,1.5,1,4,4,1,2,1.5,1,2,1.5,2,1.5, 1,2)
灵敏度<-c(3,5,2,5,5,1,1,2,3,4,3,2,1,3,1,2)

#将一个初始数据框放在一起
mydata< - data.frame(panel,instrument,cost,sensitivity)

#向数据添加一个贡献向量框架:每种仪器
#对面板的加权平均敏感度的贡献。
MYFUNC< - 功能(成本,灵敏度){
返回(成本*灵敏度/总和(成本))
}
MYDATA< - ddply(MYDATA,(面板),transform,contrib = myfunc(cost,sensitivity))

#每个面板加权平均的两个视图;应该是相同的编号,无论哪种方式
ddply(MYDATA,C( 面板),概括地说,wavg = weighted.mean(灵敏度,成本))
ddply(MYDATA,C( 面板),总结一下,wavg2 = sum(contrib))

#plot每个面板从其总体成本加权灵敏度中获得。另外
#将每个面板的加权平均值作为一条简单的垂直线。

#问题!我不知道如何获得geom_vline来尊重方面的细节。它
#似乎是计算整体数据,并在每个方面图中显示相同的
#值。
ggplot(MYDATA,AES(X =灵敏度,重量=的contrib))+
geom_bar(binwidth = 1)+
geom_vline(xintercept =总和(的contrib))+
facet_wrap (〜panel)+
ylab(contrib)


解决方案

如果你传入了预设数据,它似乎工作:

$ $ $ $ $ $ $ $ $ ggplot(mydata,aes(x =灵敏度,重量=的contrib))+
geom_bar(binwidth = 1)+
geom_vline(数据= ddply(MYDATA, 面板,概括地说,wavg =总和(的contrib)),AES(xintercept = wavg) )+
facet_wrap(〜panel)+
ylab(contrib)+
theme_bw()


I've poked around, but been unable to find an answer. I want to do a weighted geom_bar plot overlaid with a vertical line that shows the overall weighted average per facet. I'm unable to make this happen. The vertical line seems to a single value applied to all facets.

require('ggplot2')
require('plyr')

# data vectors
panel <- c("A","A","A","A","A","A","B","B","B","B","B","B","B","B","B","B")
instrument <-c("V1","V2","V1","V1","V1","V2","V1","V1","V2","V1","V1","V2","V1","V1","V2","V1")
cost <- c(1,4,1.5,1,4,4,1,2,1.5,1,2,1.5,2,1.5,1,2)
sensitivity <- c(3,5,2,5,5,1,1,2,3,4,3,2,1,3,1,2)

# put an initial data frame together
mydata <- data.frame(panel, instrument, cost, sensitivity)

# add a "contribution to" vector to the data frame: contribution of each instrument
# to the panel's weighted average sensitivity.
myfunc <- function(cost, sensitivity) {
  return(cost*sensitivity/sum(cost))
}
mydata <- ddply(mydata, .(panel), transform, contrib=myfunc(cost, sensitivity))

# two views of each panels weighted average; should be the same numbers either way
ddply(mydata, c("panel"), summarize, wavg=weighted.mean(sensitivity, cost))
ddply(mydata, c("panel"), summarize, wavg2=sum(contrib))

# plot where each panel is getting its overall cost-weighted sensitivity from. Also
# put each panel's weighted average on the plot as a simple vertical line.
#
# PROBLEM! I don't know how to get geom_vline to honor the facet breakdown. It
#          seems to be computing it overall the data and showing the resulting
#          value identically in each facet plot.
ggplot(mydata, aes(x=sensitivity, weight=contrib)) +
  geom_bar(binwidth=1) +
  geom_vline(xintercept=sum(contrib)) +
  facet_wrap(~ panel) +
  ylab("contrib")

解决方案

If you pass in the presumarized data, it seems to work:

ggplot(mydata, aes(x=sensitivity, weight=contrib)) +
  geom_bar(binwidth=1) +
  geom_vline(data = ddply(mydata, "panel", summarize, wavg = sum(contrib)), aes(xintercept=wavg)) +
  facet_wrap(~ panel) +
  ylab("contrib") +
  theme_bw()

这篇关于如何获得geom_vline来尊重facet_wrap?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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