美文网首页作图
第二章 条形图

第二章 条形图

作者: 芋圆学徒 | 来源:发表于2021-02-06 16:34 被阅读0次

    二、条形图

    1,简单的条形图

    library(gcookbook)
    ggplot(pg_mean,aes(group,weight,fill=group))+geom_bar(stat = "identity")
    str(pg_mean)
    View(pg_mean)
    

    2,绘制簇状条形图

    View(cabbage_exp)
    ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
      geom_bar(position = "dodge",stat = "identity")
    
    ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
      geom_bar(stat = "identity",position = "dodge",colour="black")+
      scale_fill_brewer(palette = "Paste11")
    

    3,绘制频数分布条形图

    ggplot(diamonds,aes(cut))+geom_bar()
    x <- table(diamonds$cut)
    barplot(x)
    View(diamonds)
    ggplot(diamonds,aes(cut,price))+
      geom_bar(stat = "identity")
    

    4,条形图着色

    upc <- subset(uspopchange,rank(Change)>40)
    ggplot(upc,aes(Abb,Change,fill=Region))+
      geom_bar(stat = "identity")
    
    ggplot(upc,aes(x=reorder(Abb,Change),Change,fill=Region))+
      geom_bar(stat = "identity",colour="black")+
      scale_fill_manual(values = c("#669933","#FFCC66"))+
      xlab("State")
    

    5,对正负条形图分别着色

    csub <- subset(climate,Source=="Berkeley"&Year>=1900)
    csub$pos <- csub$Anomaly10y>=0
    csub
    
    ggplot(csub,aes(Year,Anomaly10y,fill=pos))+
      geom_bar(stat = "identity",position = "identity")
    
    ggplot(csub,aes(Year,Anomaly10y,fill=pos))+
      geom_bar(stat = "identity",position = "identity",colour="black",size=0.25)+
      scale_fill_manual(values = c("#CCEEFF","#FFDDDD"),guide=F)
    

    6. 调整条形宽度和条形间距

    ggplot(pg_mean,aes(group,weight))+
      geom_bar(stat = "identity")
    ggplot(pg_mean,aes(group,weight))+
      geom_bar(stat = "identity",width=0.5)
    ggplot(pg_mean,aes(group,weight))+
      geom_bar(stat = "identity",width=1)
    
    ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
      geom_bar(stat = "identity",width = 0.5,position ="dodge")
    ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
      geom_bar(stat = "identity",width = 0.5,position = position_dodge(0.7))
    

    7.绘制堆积条形图

    ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
      geom_bar(stat = "identity",width = 0.5)
    #调整图例顺序
    ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
      geom_bar(stat = "identity",width = 0.5)+
      guides(fill=guide_legend(reverse = T))
    #调整堆叠顺序
    library(dplyr)
    ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar, order=desc(Cultivar)))+
      geom_bar(stat = "identity",width = 0.5)
    
    ggplot(cabbage_exp,aes(Date,Weight, fill= Cultivar))+
      geom_bar(stat = "identity",colour="black")+
      guides(fill=guide_legend(reverse = T))+
      scale_fill_brewer(palette = "Paste11")
    

    8.绘制百分比堆积条形图

    library(gcookbook)
    library(plyr)
    ce <- ddply(cabbage_exp,"Date",transform,percent_weight=Weight/sum(Weight)*100)
    ggplot(ce,aes(Date,percent_weight,fill=Cultivar))+
      geom_bar(stat = "identity")
    

    9.添加数据标签

    ggplot(cabbage_exp,aes(interaction(Date,Cultivar),Weight))+
      geom_bar(stat = "identity",width = 0.5)+
      geom_text(aes(label=Weight),vjust=1.5,colour="white")
    
    ggplot(cabbage_exp,aes(interaction(Date,Cultivar),Weight))+
      geom_bar(stat = "identity")+
      geom_text(aes(label=Weight),vjust=-0.2,colour="black")
    ggplot(cabbage_exp,aes(interaction(Date,Cultivar),Weight))+
      geom_bar(stat = "identity")+
      geom_text(aes(label=Weight),vjust=-0.2,colour="black")+
      ylim(0,max(cabbage_exp$Weight*1.05))
    
    ggplot(cabbage_exp,aes(interaction(Date,Cultivar),Weight))+
      geom_bar(stat = "identity")+
      geom_text(aes(label=Weight, y=Weight+0.1),colour="black")
     
    ggplot(cabbage_exp,aes(Date,Weight,fill=Cultivar))+
      geom_bar(stat = "identity",position = "dodge")+
      geom_text(aes(label=Weight),vjust=1.5,colour="white",
                size=3,position = position_dodge(.9))
    

    10.绘制Cleveland图

    library(gcookbook)
    tophit <- tophitters2001[1:25,]
    ggplot(tophit,aes(avg,name))+
      geom_point()
    tophit[,c("name","lg","avg")]
    
    ggplot(tophit,aes(avg,y=reorder(name,avg)))+
      geom_point(size=3)+
      theme_bw()+
      theme(panel.grid.major.x = element_blank(),
            panel.grid.minor.x = element_blank(),
            panel.grid.minor.y = element_line(colour="grey60",linetype = "dashed"))
    
    ggplot(tophit,aes(x=reorder(name,avg),avg))+
      geom_point(size=3)+
      theme_bw()+
      theme(axis.text.x = element_text(angle = 60,hjust = 1),
            panel.grid.major.y = element_blank(),
            panel.grid.minor.y = element_blank(),
            panel.grid.minor.x = element_line(colour="grey60",linetype = "dashed"))
    
    #根据因子lg进行分组,并排序,reorder不能同时对两组排序
    nameorder <- tophit$name[order(tophit$lg,tophit$avg)]
    tophit$name <- factor(tophit$name,levels = nameorder)
    
    ggplot(tophit,aes(avg,name))+
      geom_segment(aes(yend=name),xend=0,colour="grey50")+
      geom_point(size=3,aes(colour=lg))+
      scale_color_brewer(palette = "Set1",limits=c("NL","AL"))+
      theme_bw()+
      theme(panel.grid.major.y = element_blank(),
            legend.position = c(1,0.55),
            legend.justification = c(1,0.5))
    
    
    ggplot(tophit,aes(avg,name))+
      geom_segment(aes(yend=name),xend=0,colour="grey50")+
      geom_point(size=3,aes(colour=lg))+
      scale_color_brewer(palette = "Set1",limits=c("NL","AL"),guide=F)+
      theme_bw()+
      theme(panel.grid.major.y = element_blank())+
      facet_grid(lg~.,scales = "free_y",space = "free_y")
    

    相关文章

      网友评论

        本文标题:第二章 条形图

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