gpt4 book ai didi

r - 具有几何平均值、第 90 个和第 10 个百分位数的 ggplot2 箱线图

转载 作者:行者123 更新时间:2023-12-02 04:13:41 24 4
gpt4 key购买 nike

我需要创建一个独特的箱线图。我希望它代表几何平均值而不是中位数,并且框的顶部和底部是第 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")

我有一个这样的函数,用于箱线图中的自定义百分位数,最初是从 this SO answer 改编的。 。

关于r - 具有几何平均值、第 90 个和第 10 个百分位数的 ggplot2 箱线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35096287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com