簡體   English   中英

ggplot2箱形圖,具有幾何均值,第90和第10個百分位數

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

我需要創建一個獨特的箱形圖。 我希望它代表幾何平均值而不是中位數,並且框的頂部和底部分別是第90和第10個百分位數。 我發現了有關如何添加均值和標准差以及如何在情節上擴展維克斯的信息,但沒有有關如何更改基本統計信息的信息。 我想使用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

我想制作相同的圖形,但要使用新的組件。 任何見解均表示贊賞。 謝謝!

您可以編寫一個專用函數傳遞給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")

我有一個像這樣的函數,用於框線圖中的自定義百分位數,我最初是從這個SO答案中改編而成的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM