2016-01-30 92 views
0

我需要創建一個獨特的箱形圖。我希望它代表幾何平均數而不是中位數,並且框的頂部和底部是第90和第10百分位數。我找到了如何添加手段和sd的信息,以及如何在情節中擴大志趣者,但不知道如何改變基本統計數據。我想使用ggplot2,因爲我很熟悉它,但我對任何事情都很開放。ggplot2 boxplot與幾何平均數,以及第90和第10百分位數

我通過一年用下面的代碼繪製糞大腸菌羣數據:

library(psych) 
library(dplyr) 
library(zoo) 
library(caTools) 
library(ggplot2) 
library(stats) 

setwd("H:/MWQSampleData/GrowingAreaRawData") 

setAs("character", "myDate", function(from) as.Date(from, format = "%m/%d/%Y")) 

RawData <- read.csv("VaughnBay1989.csv", header = TRUE, colClasses = 
      c("factor", "factor", "myDate", "numeric", "factor", "numeric", "numeric","numeric")) 

GrowingAreaYrSummary <- RawData %>% 
    select(Year, FecalColiform) %>% 
    group_by(Year) 



Graph <- ggplot(GrowingAreaYrSummary, aes(x=Year, y=FecalColiform)) 
    geom_boxplot(outlier.shape = NA) + 
    theme(axis.text.y = element_text(face = "bold", angle = 45, size = 14), 
     axis.text.x = element_text(face = "bold", angle = 45, size = 14, vjust = -0.005), 
     panel.background = element_rect(fill = "ivory2"), 
     panel.grid.major = element_line(colour = "gray88"), 
     plot.title = element_text(size = 18, face = "bold", vjust = -4), 
     axis.title.y = element_text(size = 16, face = "bold"), 
     axis.title.x = element_text(size = 16, face = "bold", vjust = -0.5), 
     axis.ticks.x = element_line(size = 1.5, colour = "black"), 
     panel.border = element_rect(colour = "black", fill = NA, size = 1)) + 
    scale_y_continuous(breaks=seq(0,50,5), limits=c(0,50)) + 
    geom_smooth(method="loess", se="TRUE", aes(group=1)) + 
    ggtitle("Vaughn Bay Growing Area \n Fecal Coliform 1989 - 2015") + 
    ylab("Fecal Coliform (fc/100 ml)") + 
    xlab("Year") + 
    annotate("text", x=10, y=43, label="Outliers Excluded \n from Graph") 

Graph 

我想作相同的圖形,但隨着新的組件。任何見解都會被讚賞。謝謝!

+2

向下滾動到這個頁面的底部(http://docs.ggplot2.org/dev/geom_boxplot.html)它解釋瞭如何去做 – MLavoie

+0

如果你需要比MLavoie的鏈接更多的幫助,那麼你應該創建一個* *最小,可重現的例子**([見這裏的提示](http://stackoverflow.com/q/5963269/903061))。我們沒有CSV(我們也不需要它),使用一些內置數據或者模擬一個小數據集來說明問題會更好。我們也可能不需要5個包和10個主題定製來獲得主要想法。 – Gregor

回答

2

你可以寫一個特殊用途功能傳遞給stat_summary

# Return the desired percentiles plus the geometric mean 
bp.vals <- function(x, probs=c(0.1, 0.25, 0.75, .9)) { 
    r <- quantile(x, probs=probs , na.rm=TRUE) 
    r = c(r[1:2], exp(mean(log(x))), r[3:4]) 
    names(r) <- c("ymin", "lower", "middle", "upper", "ymax") 
    r 
} 

# Sample usage of the function with the built-in mtcars data frame 
ggplot(mtcars, aes(x=factor(cyl), y=mpg)) + 
    stat_summary(fun.data=bp.vals, geom="boxplot") 

我有這樣的,我在箱線圖使用自定義百分位數的函數,我原本適應它this SO answer

+0

謝謝,這非常有幫助! –

相關問題