如何在 R 中的 ggplot2 中制作带有误差条的条形图?
在本文中,我们将讨论如何使用 R 编程语言中的 ggplot2 制作带有误差条的条形图。
误差线帮助我们可视化数据的分布。误差线可应用于任何类型的绘图,以提供有关所呈现数据的额外细节层。由于误差幅度,数据中的计数值通常可能存在不确定性,我们可以将它们表示为误差条。误差线用于显示围绕数据分布的不确定性范围。
我们可以使用 R 语言的 ggplot2 包的 geom_errorbar()函数将误差线绘制到图上。
Syntax: plot + geom_errorbar( aes( ymin= value – standard_error, ymax= value + standard_error ))
where,
value: determines the column for mean values
standard_error: determines the mean error
ggplot2 中带有误差条的基本条形图
这是一个基本的条形图,使用 geom_errorbar()函数在顶部绘制误差条。
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name = c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value = c(31,12,15,28,45))
# create standard error
standard_error = 5
# Load ggplot2 pckage
library("ggplot2")
# Create bar plot using ggplot() function
ggplot(sample_data,
aes(name,value)) +
# geom_bar function is used to plot bars of barplot
geom_bar(stat = "identity")+
# geom_errorbar function is used to plot error bars
geom_errorbar(aes(ymin=value-standard_error,
ymax=value+standard_error),
width=.2)
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name = c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value = c(31,12,15,28,45))
# create standard error
standard_error = 5
# Load ggplot2 pckage
library("ggplot2")
# Create bar plot using ggplot() function
ggplot(sample_data,
aes(name,value,, color=name)) +
# geom_bar function is used to plot bars of barplot
geom_bar(stat = "identity", fill="white")+
# geom_errorbar function is used to plot error bars
# color width and size parameter are used to format
# the error bars
geom_errorbar(aes(ymin=value-standard_error,
ymax=value+standard_error, color=name),
width=0.2, size=2)
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name = c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value = c(31,12,15,28,45))
# create standard error
standard_error = 5
# Load ggplot2 pckage
library("ggplot2")
# Create bar plot using ggplot() function
ggplot(sample_data,
aes(name,value,, color=name)) +
# geom_bar function is used to plot bars of barplot
geom_bar(stat = "identity", fill="white")+
# geom_errorbar function is used to plot error bars
# color width and size parameter are used to
# format the error bars
geom_errorbar(aes(ymin=value-standard_error,
ymax=value+standard_error, color=name),
width=0.2, size=2, linetype=2)
R
# Create sample data
set.seed(5642)
sample_data <- data.frame(name = c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value = c(31,12,15,28,45))
# create standard error
standard_error = 5
# Load ggplot2 pckage
library("ggplot2")
# Create bar plot using ggplot() function
ggplot(sample_data,
aes(name,value,, color=name)) +
# geom_bar function is used to plot bars of barplot
geom_bar(stat = "identity", fill="white")+
# geom_errorbar function is used to plot error bars
# color width and size parameter are used to format
# the error bars
geom_errorbar(aes(ymin=value-standard_error,
ymax=value+standard_error, color=name),
width=0.2, size=1, linetype=1)+
# geom_point is used to give a point at mean
geom_point(mapping=aes(name, value), size=4, shape=21, fill="white")
输出:
颜色定制
我们可以使用 geom_errorbar()函数的颜色、宽度和大小参数对误差线进行颜色自定义以满足我们的要求。
Syntax: plot + geom_errorbar( aes( ymin, ymax, color ), width, size)
where,
- color: determines the color of the error bar
- width: determines the horizontal spread of the error bar.
- size: determines the thickness of the error bar.
这是一个带有按列名称着色的误差线的条形图。误差条使用宽度和大小参数进行格式化,以提供对比的吸引力和外观。
电阻
# Create sample data
set.seed(5642)
sample_data <- data.frame(name = c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value = c(31,12,15,28,45))
# create standard error
standard_error = 5
# Load ggplot2 pckage
library("ggplot2")
# Create bar plot using ggplot() function
ggplot(sample_data,
aes(name,value,, color=name)) +
# geom_bar function is used to plot bars of barplot
geom_bar(stat = "identity", fill="white")+
# geom_errorbar function is used to plot error bars
# color width and size parameter are used to format
# the error bars
geom_errorbar(aes(ymin=value-standard_error,
ymax=value+standard_error, color=name),
width=0.2, size=2)
输出:
线型定制
我们可以使用R语言中ggplot2包的geom_errorbar函数的linetype参数来自定义误差线的线型。
Syntax: plot + geom_errorbar( aes( ymin, ymax ), linetype)
where,
- linetype: determines the shape of stroke of the error bar
这是一个基本的 ggplot2 条形图,带有使用 geom_errorbar()函数的 linetype 属性制作的带条纹线的误差条。
电阻
# Create sample data
set.seed(5642)
sample_data <- data.frame(name = c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value = c(31,12,15,28,45))
# create standard error
standard_error = 5
# Load ggplot2 pckage
library("ggplot2")
# Create bar plot using ggplot() function
ggplot(sample_data,
aes(name,value,, color=name)) +
# geom_bar function is used to plot bars of barplot
geom_bar(stat = "identity", fill="white")+
# geom_errorbar function is used to plot error bars
# color width and size parameter are used to
# format the error bars
geom_errorbar(aes(ymin=value-standard_error,
ymax=value+standard_error, color=name),
width=0.2, size=2, linetype=2)
输出:
误差条中的平均点
为了强调误差线的平均值,我们甚至可以使用 ggplot2 包的 geom_point()函数在误差线的中心添加一个点。
Syntax: plot+ geom_point( aes( x, y), size, shape, fill)
where,
- size: determines the size of the point
- shape: determines the shape of the point
- fill: determines the fill color of the point
这是一个带有误差线的条形图,误差线中间有一个点,使用 ggplot2 包的 geom_errorbar() 和 geom_point()函数制作。
电阻
# Create sample data
set.seed(5642)
sample_data <- data.frame(name = c("Geek1","Geek2",
"Geek3","Geek4",
"Geeek5") ,
value = c(31,12,15,28,45))
# create standard error
standard_error = 5
# Load ggplot2 pckage
library("ggplot2")
# Create bar plot using ggplot() function
ggplot(sample_data,
aes(name,value,, color=name)) +
# geom_bar function is used to plot bars of barplot
geom_bar(stat = "identity", fill="white")+
# geom_errorbar function is used to plot error bars
# color width and size parameter are used to format
# the error bars
geom_errorbar(aes(ymin=value-standard_error,
ymax=value+standard_error, color=name),
width=0.2, size=1, linetype=1)+
# geom_point is used to give a point at mean
geom_point(mapping=aes(name, value), size=4, shape=21, fill="white")
输出: