ggplot2 boxplot,具有几何平均值,第90和第10百分位数

时间:2016-01-30 00:04:44

标签: r ggplot2 boxplot

我需要创建一个独特的盒子图。我希望它代表几何平均值而不是中位数,并且框的顶部和底部是第90和第10百分位数。我已经找到了有关如何添加工具和SD以及如何在图上扩展wiskers而不是如何更改基本统计信息的信息。我想使用ggplot2,因为我熟悉它,但我对任何事情都持开放态度。

我按年使用以下代码绘制粪便大肠菌群数据:

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

我想使用新组件制作相同的图表。任何见解都表示赞赏。谢谢!

1 个答案:

答案 0 :(得分: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改编的。