美文网首页R语言学习
ggplot2 004 误差图,饼图

ggplot2 004 误差图,饼图

作者: caoqiansheng | 来源:发表于2020-08-21 15:12 被阅读0次

    1.Error bars

    1.1 误差图分类

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

    1.2 数据准备
    # geom_errorbar()
    # geom_linerange()
    # geom_pointrange()
    # geom_crossbar()
    # geom_errorbarh()
    # 向条形图和折线图添加误差线
    library(ggplot2)
    df <- ToothGrowth
    df$dose <- as.factor(df$dose)
    head(df)
    
    # 函数计算每组的平均值和标准偏差
    # data:数据框
    # varname:包含要汇总的变量的列的名称
    # groupnames:用作分组变量的列名称的向量
    data_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 variable
    df2$dose=as.factor(df2$dose)
    head(df2)
    
    1.3 带误差条的条形图
    library(ggplot2)
    # 默认条形图
    p1 <- 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)) 
    
    # 成品条形图
    p2 <- p1 +labs(title="Tooth length per dose", x="Dose (mg)", y = "Length")+
      theme_classic() +
      scale_fill_manual(values=c('#999999','#E69F00'))
    # 仅保留较高的误差线
    p3 <- ggplot(df2, aes(x=dose, y=len, fill=supp)) + 
      geom_bar(stat="identity", color="black", position=position_dodge()) +
      geom_errorbar(aes(ymin=len, ymax=len+sd), width=.2,
                    position=position_dodge(.9)) 
    ggarrange(p1,p2,p3)
    
    image.png
    1.4 带误差线的线图
    # 默认线图
    p4 <- 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))
    # 成品线图
    p5 <- p4 + labs(title="Tooth length per dose", x="Dose (mg)", y = "Length")+
      theme_classic() +
      scale_color_manual(values=c('#999999','#E69F00'))
    # Use geom_pointrange
    p6 <- ggplot(df2, aes(x=dose, y=len, group=supp, color=supp)) + 
      geom_pointrange(aes(ymin=len-sd, ymax=len+sd))
    # Use geom_line()+geom_pointrange()
    p7 <- ggplot(df2, aes(x=dose, y=len, group=supp, color=supp)) + 
      geom_line()+
      geom_pointrange(aes(ymin=len-sd, ymax=len+sd))
    ggarrange(p4,p5,p6,p7)
    
    image.png
    1.5 带有均值点和误差线的点图
    # 使用了geom_dotplot()和stat_summary()函数:
    # 平均值+/- SD可以添加为纵横线,误差线或点范围:
    p8 <- ggplot(df, aes(x=dose, y=len)) + 
      geom_dotplot(binaxis='y', stackdir='center')
    # use geom_crossbar()
    p9 <- p8 + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1), 
                     geom="crossbar", width=0.5)
    # Use geom_errorbar()
    p10 <- p8 + 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()
    p11 <- p8 + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1), 
                     geom="pointrange", color="red")
    ggarrange(p8,p9,p10,p11)
    
    image.png

    2.Pie chart

    df <- data.frame(
      group = c("Male", "Female", "Child"),
      value = c(25, 25, 50)
    )
    head(df)
    library(ggplot2)
    # Barplot
    bp<- ggplot(df, aes(x="", y=value, fill=group))+
      geom_bar(width = 1, stat = "identity")
    bp
    pie <- bp + coord_polar("y", start=0)
    pie
    ggarrange(bp,pie)
    
    image.png
    # 更改饼图填充颜色
    # 自定义调色板
    p1 <- pie + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
    # brewer调色板
    p2 <- pie + scale_fill_brewer(palette="Dark2")
    p3 <- pie + scale_fill_brewer(palette="Blues")+
      theme_minimal()
    # 灰度
    p4 <- pie + scale_fill_grey() + theme_minimal()
    ggarrange(p1,p2,p3,p4)
    
    image.png
    # 从因子变量创建饼图
    head(PlantGrowth)
    p5 <- ggplot(PlantGrowth, aes(x=factor(1), fill=group))+
      geom_bar(width = 1)+
      coord_polar("y")
    p5
    
    image.png
    # 自定义饼图
    blank_theme <- theme_minimal()+
      theme(
        axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        panel.border = element_blank(),
        panel.grid=element_blank(),
        axis.ticks = element_blank(),
        plot.title=element_text(size=14, face="bold")
      )
    # 套用空白主题
    library(scales)
    p6 <- pie + scale_fill_grey() +  blank_theme +
      theme(axis.text.x=element_blank()) +
      geom_text(aes(y = value/3 + c(0, cumsum(value)[-length(value)]), 
                    label = percent(value/100)), size=5)
    # brewer调色板
    p7 <- pie + scale_fill_brewer("Blues") + blank_theme +
      theme(axis.text.x=element_blank())+
      geom_text(aes(y = value/3 + c(0, cumsum(value)[-length(value)]), 
                    label = percent(value/100)), size=5)
    ggarrange(p6,p7)
    
    image.png

    Reference

    1.ggplot2 error bars : Quick start guide - R software and data visualization
    2.ggplot2 pie chart : Quick start guide - R software and data visualization
    3.ggplot2 pie chart : Quick start guide - R software and data visualization

    相关文章

      网友评论

        本文标题:ggplot2 004 误差图,饼图

        本文链接:https://www.haomeiwen.com/subject/yfjyjktx.html