简体   繁体   English

ggplot2箱形图,具有几何均值,第90和第10个百分位数

[英]ggplot2 boxplot with geometric mean, and 90th and 10th percentiles

I need to create a unique box plot. 我需要创建一个独特的箱形图。 I want it to represent the geometric mean instead of the median, and the top and bottom of the box to be the 90th and 10th percentiles. 我希望它代表几何平均值而不是中位数,并且框的顶部和底部分别是第90和第10个百分位数。 I have found information on how to add means and sd and how to extend wiskers on plots but not how to change the basic statistics. 我发现了有关如何添加均值和标准差以及如何在情节上扩展维克斯的信息,但没有有关如何更改基本统计信息的信息。 I would like to use ggplot2 because I'm familiar with it but I'm open to anything. 我想使用ggplot2,因为我对此很熟悉,但是我对任何事物都开放。

I'm plotting fecal coliform data by year with the following code: 我用以下代码按年绘制粪便大肠菌群数据:

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

I would like to make the same graph but with the new components. 我想制作相同的图形,但要使用新的组件。 Any insight is appreciated. 任何见解均表示赞赏。 Thank you! 谢谢!

You can write a special-purpose function to pass to stat_summary : 您可以编写一个专用函数传递给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")

I have a function like this that I use for custom percentiles in boxplots and I originally adapted it from this SO answer . 我有一个像这样的函数,用于框线图中的自定义百分位数,我最初是从这个SO答案中改编而成的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM