在 R 中使用 ggplot2 绘制均值和标准差
误差条显示一组测量值或基于数据集中出现的误差的计算值的置信度和精度。它有助于直观地显示数据框区域中的错误,并显示实际和准确的缺失部分。作为一种描述性行为,误差条提供有关数据差异的详细信息以及进行更改的建议,以便数据对用户更具洞察力和影响力。
入门
geom_errorbar():此函数用于生成误差线。
句法:
geom_errorbar(mapping = NULL, data = NULL, stat = “identity”, position = “identity”, …)
示例:绘制以在条形图上显示均值和标准差。
R
df<-data.frame(Mean=c(0.24,0.25,0.37,0.643,0.54),
sd=c(0.00362,0.281,0.3068,0.2432,0.322),
Quality=as.factor(c("good","bad","good",
"very good","very good")),
Category=c("A","B","C","D","E"),
Insert= c(0.0, 0.1, 0.3, 0.5, 1.0))
# Load ggplot2
library(ggplot2)
ggplot(df, aes(x=Category, y=Mean, fill=Quality)) +
geom_bar(position=position_dodge(), stat="identity",
colour='black') +
geom_errorbar(aes(ymin=Mean-sd, ymax=Mean+sd), width=.2)
R
# creating a data frame df
df<-data.frame(Mean=c(0.24,0.25,0.37,0.643,0.54),
sd=c(0.00362,0.281,0.3068,0.2432,0.322),
Quality=as.factor(c("good","bad","good",
"very good","very good")),
Category=c("A","B","C","D","E"),
Insert= c(0.0, 0.1, 0.3, 0.5, 1.0))
# plot the point plot
p<-ggplot(df, aes(x=Category, y=Mean, fill=Quality)) +
geom_point()+
geom_errorbar(aes(ymin=Mean-sd, ymax=Mean+sd), width=.2,
position=position_dodge(0.05))
p
R
# load crop_recomendation csv file and
# store it in ds
ds <- read.csv("Crop_recommendation.csv", header = TRUE)
ggplot(ds, aes(x=label, y=temperature)) + geom_boxplot()
# create a new dataframe crop_means
crop_means <- ds %>%
group_by(label) %>%
summarize(mean_temperature=mean(temperature))
crop_means
# Creating barplots of means
ggplot(crop_means, aes(x=label, y=mean_temperature)) +
geom_bar(stat="identity")
R
# load crop_recomendation csv file and
# store it in ds
ds <- read.csv("Crop_recommendation.csv", header = TRUE)
ggplot(ds, aes(x=label, y=temperature)) + geom_boxplot()
# create a new dataframe crop_means
crop_means <- ds %>%
group_by(label) %>%
summarize(mean_temperature=mean(temperature))
crop_means
# creating point plots of means
ggplot(ds, aes(x=label, y=temperature)) +
geom_point(stat="summary", fun.y="mean")
R
# load a crop recommendation csv file dataset
ds <- read.csv("Crop_recommendation.csv", header = TRUE)
# create a new dataframe crop_means_Se
crop_means_se <- ds %>%
group_by(label) %>%
summarize(mean_N=mean(N),
sd_N=sd(N),
N_N=n(),
se=sd_N/sqrt(N_N),
upper_limit=mean_N+se,
lower_limit=mean_N-se
)
crop_means_se
ggplot(crop_means_se, aes(x=label, y=mean_N)) +
geom_bar(stat="identity") +
geom_errorbar(aes(ymin=lower_limit, ymax=upper_limit))
R
# load a crop recommendation csv file dataset
ds <- read.csv("Crop_recommendation.csv", header = TRUE)
ggplot(ds, aes(x=label, y=N)) + geom_bar(stat="summary", fun.y="mean") +
geom_errorbar(stat="summary",
fun.ymin=function(x) {mean(x)-sd(x)/sqrt(length(x))},
fun.ymax=function(x) {mean(x)+sd(x)/sqrt(length(x))})
输出:
现在让我们看看点图,如果我们想在同一个数据框中添加点,只需添加 geom_point()。
句法:
geom_point(mapping = NULL, data = NULL, stat = “identity”, position = “identity”,…, na.rm = FALSE,show.legend = NA,inherit.aes = TRUE)
示例 1:绘制点
电阻
# creating a data frame df
df<-data.frame(Mean=c(0.24,0.25,0.37,0.643,0.54),
sd=c(0.00362,0.281,0.3068,0.2432,0.322),
Quality=as.factor(c("good","bad","good",
"very good","very good")),
Category=c("A","B","C","D","E"),
Insert= c(0.0, 0.1, 0.3, 0.5, 1.0))
# plot the point plot
p<-ggplot(df, aes(x=Category, y=Mean, fill=Quality)) +
geom_point()+
geom_errorbar(aes(ymin=Mean-sd, ymax=Mean+sd), width=.2,
position=position_dodge(0.05))
p
输出:
不同的群体使用不同的方法来说明他们的差异。或者,使用点图或点图。为了告诉 ggplot 一个列或一个点代表一个均值,我们需要指出一个均值统计量。让我们使用不同的数据框详细探讨这一点。为此,我们可以使用 ggplot 的“stat”函数。
让我们使用均值条形图来可视化结果。代替使用 *stat=count>',我们将告诉 stat 我们想要一个汇总度量,即平均值。然后,将数据帧分成几组,并记录并绘制每个组的平均值和标准偏差。这可以使用summary 和group_by() 来完成。
正在使用的文件: Crop_recommendation
示例:绘制每个组的均值和标准差。
电阻
# load crop_recomendation csv file and
# store it in ds
ds <- read.csv("Crop_recommendation.csv", header = TRUE)
ggplot(ds, aes(x=label, y=temperature)) + geom_boxplot()
# create a new dataframe crop_means
crop_means <- ds %>%
group_by(label) %>%
summarize(mean_temperature=mean(temperature))
crop_means
# Creating barplots of means
ggplot(crop_means, aes(x=label, y=mean_temperature)) +
geom_bar(stat="identity")
输出:
现在,如果你想指向点图,那么你也可以使用 geom_point()函数来做到这一点。
句法:
geom_point(stat=”summary”, fun.y=”mean”)
示例:点图
电阻
# load crop_recomendation csv file and
# store it in ds
ds <- read.csv("Crop_recommendation.csv", header = TRUE)
ggplot(ds, aes(x=label, y=temperature)) + geom_boxplot()
# create a new dataframe crop_means
crop_means <- ds %>%
group_by(label) %>%
summarize(mean_temperature=mean(temperature))
crop_means
# creating point plots of means
ggplot(ds, aes(x=label, y=temperature)) +
geom_point(stat="summary", fun.y="mean")
输出:
要绘制标准偏差(SD),您需要使用 geom_errorbar()。首先,我们可以创建一个新的数据集,这是创建误差条最费力的方法。这次我们还将计算标准误差(等于标准偏差除以 N 的平方根)。
Syntax:
geom_errorbar()
Parameters:
- ymin or xmin : Lower Value of custom point
- ymax or xmax: Upper Value of custom point
- height: height of errorbar
- alpha: Opacity of error bar
- color: Color of error bar
- group: Differentiate points by group
- linetype
- size
示例:绘制标准偏差
电阻
# load a crop recommendation csv file dataset
ds <- read.csv("Crop_recommendation.csv", header = TRUE)
# create a new dataframe crop_means_Se
crop_means_se <- ds %>%
group_by(label) %>%
summarize(mean_N=mean(N),
sd_N=sd(N),
N_N=n(),
se=sd_N/sqrt(N_N),
upper_limit=mean_N+se,
lower_limit=mean_N-se
)
crop_means_se
ggplot(crop_means_se, aes(x=label, y=mean_N)) +
geom_bar(stat="identity") +
geom_errorbar(aes(ymin=lower_limit, ymax=upper_limit))
输出:
您还可以使用 geom_errorbar() 创建自己的“se”函数。 Xmin & Xmax 和 Ymin & Ymax 可用于水平或垂直绘制误差条。
句法:
geom_errorbar(stat=”summary”,fun.ymin=function(x){mean(x-sd(x)/sqrt(length(x))}, fun.ymax=function(x){mean(x)+sd(x)/sqrt(length(x))}).
在这里,我们计算 ymin 和 ymax 值以垂直绘制误差条,这些值是由一个单独的函数创建的,在该函数中,为 (x-sd(x)/sqrt(length(x)) 的平均值计算 y 或ymin 和 (x+sd(x)/sqrt(length(x)) 的平均值是针对 y 或 ymax 的最大值计算的。
示例:绘制标准偏差
电阻
# load a crop recommendation csv file dataset
ds <- read.csv("Crop_recommendation.csv", header = TRUE)
ggplot(ds, aes(x=label, y=N)) + geom_bar(stat="summary", fun.y="mean") +
geom_errorbar(stat="summary",
fun.ymin=function(x) {mean(x)-sd(x)/sqrt(length(x))},
fun.ymax=function(x) {mean(x)+sd(x)/sqrt(length(x))})
输出: