을 얻는 방법 geom_vline 명예 facet_wrap?

나는've 찌르고 주위에,그러나 찾을 수 없었다. 내가 하고 싶중 geom_bar 줄거리와 겹쳐 수직선을 보여주는 전반적으로 가중평균당 면을 말합니다. 나는'm 을 할 수 없이 일어날 수 있습니다. 수직 라인 것 같다는 단일 값이 적용된 모든 측면이 있습니다.

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")
질문에 대한 의견 (1)
해결책

전달하는 경우에 presumarized 데이터를,그것은 작동하는 것 같다:

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()

해설 (0)

예를 사용하여 dplyr 및 facet_wrap incase 사람이 그것을 원한다.

library(dplyr)
library(ggplot2)

df1  4)
df2 %
  group_by(Species, Big.Petal) %>%
  summarise(Mean.SL = mean(Sepal.Length))

ggplot() +
  geom_histogram(data = df1, aes(x = Sepal.Length, y = ..density..)) +
  geom_vline(data = df2, mapping = aes(xintercept = Mean.SL)) +
  facet_wrap(Species ~ Big.Petal) 

해설 (1)

 vlines 
해설 (0)