2012-03-16 61 views
2

我正在試圖製作一個ggplot圖形,它將根據另一個(ZONE)使用face_grid顯示變量的比例(SIZE),然後在每個類別中顯示使用stat_summary的二進制變量(BG)。但現在,我想加入點(手段)並以百分比格式顯示平均值(接近點)。爲了加入我用stat_summary('arguments here',geom="line")和其他命令嘗試的點,但事實是我不知道該怎麼做。加入一行來自stat_summary()的結果

library(ggplot2) 
    library(scales) 

    set.seed(100) 

    data <- data.frame(ID = 1:600, 
        SIZE = rep(c('B','M','S'), each = 200), 
        ZONE = sample(c('A','B'), size = 600, replace=T), 
        BG = c(sample(0:1, size = 200, prob = c(1,1), replace=T), 
          sample(0:1, size = 200, prob = c(2,3), replace=T), 
          sample(0:1, size = 200, prob = c(1,2), replace=T))) 

    ggplot(data, aes(x = SIZE)) + 
     geom_bar(aes(y = (..count..)/sum(..count..))) + 
     facet_grid(~ZONE) + 
     stat_summary(aes(y = BG), fun.y=mean, colour="red", geom="point", size = 3) + 
     scale_y_continuous('Percent', labels = percent_format()) 

在此先感謝和抱歉我的英語。

回答

3

使用geom_line時請務必記住group美學!

ggplot(data, aes(x = SIZE)) + 
     geom_bar(aes(y = (..count..)/sum(..count..))) + 
     facet_grid(~ZONE) + 
     stat_summary(aes(y = BG,group = 1), fun.y=mean, colour="red", geom="line", size = 3) + 
     scale_y_continuous('Percent', labels = percent_format()) 
+0

謝謝@joran,你教會我一些新的東西。現在,我如何使用geom_text來顯示平均值?再次感謝。 – jbkunst 2012-03-16 15:38:41