绘制覆盖有拟合 2 参数 Weibull 函数的直方图
Graphing a histogram overlaid with a fitted 2 parameter Weibull function
我想在同一张图上绘制直方图和拟合 Weibull 函数。绘制直方图的代码是:
hist(data$grddia2, prob=TRUE,breaks=5)
拟合威布尔函数的代码为:(需要MASS包)
fitdistr(data$grddia2,densfun=dweibull,start=list(scale=1,shape=2))
如何在同一张图上绘制两者。我附上了数据集。
此外,对于任何能够提供能够实现相同目的但为每一列数据创建一个图表的代码的人,都会有奖励。数据集中的许多列。如果所有图表都在同一页上就好了。
https://www.dropbox.com/s/ra9c2kkk49vyyyc/Diameter%20Distribution.csv?dl=0
这是代码
library("ggplot2")
library("dplyr")
library("tidyr")
library("MASS")
# Import dataset and filter the column "treeno"
# Use namespace dplyr:: explicitly because of conflict with MASS:: for function "select"
data <- read.csv("Diameter Distribution.csv") %>%
dplyr::select(-treeno)
# Function to provide the Weibull distribution for each column
# The distribution is calculated based on the estimated scale and shape parameters of the input
fitweibull <- function(column) {
x <- seq(0,7,by=0.01)
fitparam <- column %>%
unlist %>%
fitdistr(densfun=dweibull,start=list(scale=1,shape=2))
return(dweibull(x, scale=fitparam$estimate[1], shape=fitparam$estimate[2]))
}
# Apply function for each column then consolidate all in a data.frame
fitdata <-data %>%
apply(2, as.list) %>%
lapply(FUN = fitweibull) %>%
data.frame()
# Display graphs
multiplyingFactor<-10
ggplot() +
geom_histogram(data=gather(data), aes(x=value, group=key, fill=key), alpha=0.2) +
geom_line(data=gather(fitdata), aes(x=rep(seq(0,7,by=0.01),ncol(fitdata)), y=multiplyingFactor*value, group=key, color=key))
以及输出图
变体:感谢出色的 ggplot2 包,您还可以通过添加最后一行代码来分开图表
+ facet_wrap(~ key) + theme(legend.position = "none")
这给了你另一个数字:
我想在同一张图上绘制直方图和拟合 Weibull 函数。绘制直方图的代码是:
hist(data$grddia2, prob=TRUE,breaks=5)
拟合威布尔函数的代码为:(需要MASS包)
fitdistr(data$grddia2,densfun=dweibull,start=list(scale=1,shape=2))
如何在同一张图上绘制两者。我附上了数据集。
此外,对于任何能够提供能够实现相同目的但为每一列数据创建一个图表的代码的人,都会有奖励。数据集中的许多列。如果所有图表都在同一页上就好了。
https://www.dropbox.com/s/ra9c2kkk49vyyyc/Diameter%20Distribution.csv?dl=0
这是代码
library("ggplot2")
library("dplyr")
library("tidyr")
library("MASS")
# Import dataset and filter the column "treeno"
# Use namespace dplyr:: explicitly because of conflict with MASS:: for function "select"
data <- read.csv("Diameter Distribution.csv") %>%
dplyr::select(-treeno)
# Function to provide the Weibull distribution for each column
# The distribution is calculated based on the estimated scale and shape parameters of the input
fitweibull <- function(column) {
x <- seq(0,7,by=0.01)
fitparam <- column %>%
unlist %>%
fitdistr(densfun=dweibull,start=list(scale=1,shape=2))
return(dweibull(x, scale=fitparam$estimate[1], shape=fitparam$estimate[2]))
}
# Apply function for each column then consolidate all in a data.frame
fitdata <-data %>%
apply(2, as.list) %>%
lapply(FUN = fitweibull) %>%
data.frame()
# Display graphs
multiplyingFactor<-10
ggplot() +
geom_histogram(data=gather(data), aes(x=value, group=key, fill=key), alpha=0.2) +
geom_line(data=gather(fitdata), aes(x=rep(seq(0,7,by=0.01),ncol(fitdata)), y=multiplyingFactor*value, group=key, color=key))
以及输出图
变体:感谢出色的 ggplot2 包,您还可以通过添加最后一行代码来分开图表
+ facet_wrap(~ key) + theme(legend.position = "none")
这给了你另一个数字: