继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

R语言之可视化①误差棒

慕姐8265434
关注TA
已关注
手记 1147
粉丝 221
获赞 1064

本教程介绍如何使用R软件和ggplot2包创建带有误差棒的图形。 可以使用以下函数创建不同类型的错误栏:

geom_errorbar()
geom_linerange()
geom_pointrange()
geom_crossbar()
geom_errorbarh()

准备数据
使用ToothGrowth数据。 它描述了维生素C对豚鼠牙齿生长的影响。 使用三种剂量水平的维生素C(0.5mg,1mg和2 mg)和两种递送方法[橙汁(OJ)或抗坏血酸(VC)]中的每一种:

> library(ggplot2)
> df <- ToothGrowth
> df$dose <- as.factor(df$dose)
> head(df)
   len supp dose1  4.2   VC  0.52 11.5   VC  0.53  7.3   VC  0.54  5.8   VC  0.55  6.4   VC  0.56 10.0   VC  0.5

len:齿长
dose:剂量单位为mg(0.5,1,2)
supp:补充类型(VC或OJ)
在下面的示例中,我们将绘制每组中Tooth长度的平均值。 标准差用于绘制图形上的误差线。首先,使用下面的辅助函数将用于计算每组感兴趣变量的均值和标准差。

#+++++++++++++++++++++++++# Function to calculate the mean and the standard deviation# for each group#+++++++++++++++++++++++++# data : a data frame# varname : the name of a column containing the variable#to be summariezed# groupnames : vector of column names to be used as# grouping variablesdata_summary <- function(data, varname, groupnames){  require(plyr)
  summary_func <- function(x, col){
    c(mean = mean(x[[col]], na.rm=TRUE),
      sd = sd(x[[col]], na.rm=TRUE))
  }
  data_sum<-ddply(data, groupnames, .fun=summary_func,
                  varname)
  data_sum <- rename(data_sum, c("mean" = varname))  return(data_sum)
}
df2 <- data_summary(ToothGrowth, varname="len", 
                    groupnames=c("supp", "dose"))# Convert dose to a factor variabledf2$dose=as.factor(df2$dose)
head(df2)
  • 向条形图添加误差线

函数geom_errorbar()可用于生成误差棒:

library(ggplot2)
# Default bar plot
p<- ggplot(df2, aes(x=dose, y=len, fill=supp)) + 
  geom_bar(stat="identity", color="black", 
           position=position_dodge()) +  geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.2,
                position=position_dodge(.9)) print(p)
# Finished bar plotp+labs(title="Tooth length per dose", x="Dose (mg)", y = "Length")+  theme_classic() +  scale_fill_manual(values=c('#999999','#E69F00'))

webp

webp

  • 使用线图绘制误差棒

# Default line plot
p<- ggplot(df2, aes(x=dose, y=len, group=supp, color=supp)) + 
  geom_line() +  geom_point()+  geom_errorbar(aes(ymin=len-sd, ymax=len+sd), width=.2,
                position=position_dodge(0.05))print(p)
# Finished line plotp+labs(title="Tooth length per dose", x="Dose (mg)", y = "Length")+  theme_classic() +  scale_color_manual(values=c('#999999','#E69F00'))

webp


webp

  • 使用点图绘制误差棒

p <- ggplot(df, aes(x=dose, y=len)) + 
  geom_dotplot(binaxis='y', stackdir='center')
# use geom_crossbar()p + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1), 
                 geom="crossbar", width=0.5)# Use geom_errorbar()p + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1), 
                 geom="errorbar", color="red", width=0.2) +  stat_summary(fun.y=mean, geom="point", color="red")

# Use geom_pointrange()p + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1), 
                 geom="pointrange", color="red")

使用函数geom_dotplot()和stat_summary():

平均值+/- SD可以添加为误差条或点范围:


webp



作者:夜神moon
链接:https://www.jianshu.com/p/44e4321a77ca


打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP