在 R 中使用 ggplot2 的误差线
误差条是显示平均分数的条。误差条像胡须一样从条形图中伸出。误差条显示测量的精确程度。它显示了我们获得的价值有多少预期的变化。误差线可以水平和垂直电镀。水平误差条图显示组差异的误差条以及组的误差条。
误差条以 3 种方式之一显示均值的精度:
- 置信区间
- 均值的标准误
- 标准差
geom_errorbar()
表示垂直间隔的各种方式由 x、ymin 和 ymax 定义。每个案例绘制一个单一的图形对象。这里geom绘制了误差线,可以由下限和上限来定义。请记住,您必须自己提供 y_min 和 y_max 的值,因为误差条几何不会自动计算置信度。
句法 :
geom_errorbar(mapping = NULL,data = NULL,stat = “identity”,position = “identity”,na.rm = FALSE, orientation = NA,show.legend = NA,inherit.aes = TRUE)
例子 :
R
library("ggplot2")
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(dose,len))
p +
geom_line(aes(group = len/2))+geom_errorbar(
aes(ymin =len , ymax = dose),width=0.3)
R
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(dose,len))
p +
geom_line()+
stat_summary(func.y= mean, geom= "bar" ,fill="white",color="black") +
geom_linerange(aes(ymin =len , ymax = dose)
)
R
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(dose,len))
p +
geom_line()+
stat_summary(func.y= mean, geom= "bar" ,fill="white",color="black") +
geom_pointrange(aes(ymin =len , ymax = dose)
)
R
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(dose,len))
p +geom_crossbar(aes(ymin = len , ymax = dose),width = 0.2)
R
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(len,dose))
p +
geom_line()+
stat_summary(func.y= mean, geom= "bar" ,
fill="green",color="red") +
geom_errorbarh(aes(xmin =dose , xmax = len)
)
R
library("ggplot2")
rawdata <- read.csv("example1.csv", header = TRUE)
rawdata$gender = factor(rawdata$gender,levels = c(1,2),
labels = c("Men","Women"))
rawdata$film = factor(rawdata$film, levels = c(1,2), l
abels = c("Bridget Jones","Memento"))
rawdata_bar = ggplot(rawdata,aes(film,arousal))
rawdata_bar + stat_summary(func.y= mean, geom= "bar" ,
fill="white",color="black") +
stat_summary(fun.data = mean_cl_normal ,
geom = "errorbar",width = 0.2)
rawdata$gender = factor(rawdata$gender,levels = c(1,2),
labels = c("Men","Women"))
rawdata$film = factor(rawdata$film, levels = c(1,2),
labels = c("Bridget Jones","Memento"))
rawdata_bar + stat_summary(func.y= mean, geom= "bar" ,
fill="white",color="black")
rawdata_bar + stat_summary(func.y= mean, geom= "bar" ,
fill="white",color="black") +
stat_summary(fun.data = mean_cl_normal ,
geom = "errorbar",width = 0.2)
R
library("ggplot2")
rawdata <- read.csv("example1.csv", header = TRUE)
rawdata$gender = factor(rawdata$gender,levels = c(1,2),
labels = c("Men","Women"))
rawdata$film = factor(rawdata$film, levels = c(1,2),
labels = c("Bridget Jones","Memento"))
rawdata2 = ggplot(rawdata,aes(film, arousal, fill = gender))
rawdata2 +
stat_summary(fun.y = mean, geom = "bar",position = "dodge")+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar",
position = position_dodge(width = 0.90),width=.2)
输出 :
geom_linerange()
这里geom绘制了误差线,可以由下限和上限来定义。 linerange函数有点类似于误差线。在 line range函数,您必须自己提供 y_min 和 y_max 的值,因为 linerange geom 不会自动计算置信度。在 geom_linerange 中有一些默认存在的参数(大小、线条范围、颜色、宽度)。
句法 :
geom_linerange(mapping = NULL,data = NULL,stat = “identity”,position = “identity”,na.rm = FALSE orientation = NA,show.legend = NA,inherit.aes = TRUE)
例子 :
电阻
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(dose,len))
p +
geom_line()+
stat_summary(func.y= mean, geom= "bar" ,fill="white",color="black") +
geom_linerange(aes(ymin =len , ymax = dose)
)
输出 :
geom_pointrange()
这里geom绘制了误差线,可以由下限和上限来定义。点范围函数与误差线和线范围非常相似。在点范围函数,您必须自己提供 y_min 和 y_max 的值,因为点范围几何不会自动计算置信度。在 geom_pointrange 中有一些默认存在的参数(大小、线条范围、颜色、填充、宽度)。 pointrange函数可用于绘制置信区间。
句法 :
geom_linerange(mapping = NULL,data = NULL,stat = “identity”,position = “identity”,fatten = 4,na.rm = FALSE orientation = NA,show.legend = NA,inherit.aes = TRUE)
例子 :
电阻
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(dose,len))
p +
geom_line()+
stat_summary(func.y= mean, geom= "bar" ,fill="white",color="black") +
geom_pointrange(aes(ymin =len , ymax = dose)
)
输出 :
geom_crossbar()
这里geom绘制了误差线,可以由下限和上限来定义。 crossbar函数与误差线和线范围非常相似。横杆是中间有水平线的空心杆。
句法:
geom_crossbar(mapping = NULL,data = NULL,stat = “identity”,position = “identity”,fatten = 2.5,na.rm = FALSE,orientation = NA,show.legend = NA,inherit.aes = TRUE)
例子 :
电阻
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(dose,len))
p +geom_crossbar(aes(ymin = len , ymax = dose),width = 0.2)
输出 :
geom_errorbarh()
这里geom绘制了误差线,可以由下限和上限来定义。 geombar函数有点类似于误差线,但在这里我们改变了位置和轴。在 errorbar range 函数,您必须自己提供 x_min 和 x_max 的值,因为 error bar geom 不会自动计算置信水平。在 geom_errorbar 中有一些默认存在的参数(大小、线条范围、颜色、宽度)。
句法:
geom_errorbarh(mapping = NULL,data = NULL,stat = “identity”,position = “identity”,na.rm = FALSE,orientation = NA,show.legend = NA,inherit.aes = TRUE)
例子 :
电阻
# import libraries
library("ggplot2")
# import data
df <- ToothGrowth
# Transform dose and len column into factor
df$dose <- as.factor(df$dose)
df$len <- as.factor(df$len)
p<-ggplot(df,aes(len,dose))
p +
geom_line()+
stat_summary(func.y= mean, geom= "bar" ,
fill="green",color="red") +
geom_errorbarh(aes(xmin =dose , xmax = len)
)
输出 :
让我们在一个变量系统上实现这些。
数据: example.csv
将数据加载到变量中。检查数据是否正确,如果正确保持不变,否则根据您的需要更改。要绘制电影(x 轴)的平均唤醒分数(y 轴),首先创建绘图对象。
句法 :
factor(x = character(), levels, labels = levels,exclude = NA, ordered = is.ordered(x), nmax = NA)
如果需要更改,则通过将它们设置为实际因子来完成数据转换。然后绘制数据。
示例 1:
电阻
library("ggplot2")
rawdata <- read.csv("example1.csv", header = TRUE)
rawdata$gender = factor(rawdata$gender,levels = c(1,2),
labels = c("Men","Women"))
rawdata$film = factor(rawdata$film, levels = c(1,2), l
abels = c("Bridget Jones","Memento"))
rawdata_bar = ggplot(rawdata,aes(film,arousal))
rawdata_bar + stat_summary(func.y= mean, geom= "bar" ,
fill="white",color="black") +
stat_summary(fun.data = mean_cl_normal ,
geom = "errorbar",width = 0.2)
rawdata$gender = factor(rawdata$gender,levels = c(1,2),
labels = c("Men","Women"))
rawdata$film = factor(rawdata$film, levels = c(1,2),
labels = c("Bridget Jones","Memento"))
rawdata_bar + stat_summary(func.y= mean, geom= "bar" ,
fill="white",color="black")
rawdata_bar + stat_summary(func.y= mean, geom= "bar" ,
fill="white",color="black") +
stat_summary(fun.data = mean_cl_normal ,
geom = "errorbar",width = 0.2)
输出:
示例 2:对于两个自变量也可以这样做。
电阻
library("ggplot2")
rawdata <- read.csv("example1.csv", header = TRUE)
rawdata$gender = factor(rawdata$gender,levels = c(1,2),
labels = c("Men","Women"))
rawdata$film = factor(rawdata$film, levels = c(1,2),
labels = c("Bridget Jones","Memento"))
rawdata2 = ggplot(rawdata,aes(film, arousal, fill = gender))
rawdata2 +
stat_summary(fun.y = mean, geom = "bar",position = "dodge")+
stat_summary(fun.data = mean_cl_normal, geom = "errorbar",
position = position_dodge(width = 0.90),width=.2)
输出: