美文网首页
Fig1-b 绘制环形图和饼图并拼接在一起 2021-01-03

Fig1-b 绘制环形图和饼图并拼接在一起 2021-01-03

作者: RashidinAbdu | 来源:发表于2022-04-14 11:19 被阅读0次

    1. 绘制环形图:

    安装需要的包:

    install.packages("tidyverse")
    install.packages("ggthemes")
    
    • 数据准备与作图:

    library(tidyverse)
    library(ggthemes)
    nm<-c('Xenobiotics','Unnamed','Nucleotide','Lipid','Peptide','Amino acid' )
    rate<-c(7,3,1,19,1,9)
    Set3<-c('red','yellow','brown','orange','pink','green')
    
    rate_per<-paste(as.character(rate))
    ad<-data.frame(type=nm,n=rate,rate_per=rate_per)
    
    ad$fraction = ad$n / sum(ad$n)
    ad$ymax = cumsum(ad$fraction)
    ad$ymin = c(0, head(ad$ymax, n = -1))
    
    • 绘制环形图:

    p1<-ggplot(data = ad, aes(fill = type, ymax = ymax, ymin = ymin, xmax = 4, xmin = 3)) +
        geom_rect(show.legend = F,alpha=0.8) +
        scale_fill_brewer(palette =Set3)+
        coord_polar(theta = "y") +
        labs(x = "", y = "", title = "b") + 
        xlim(c(0, 5)) +
        theme_light() +
        theme(panel.grid=element_blank()) + ## 去掉白色外框
        theme(axis.text=element_blank()) + ## 把图旁边的标签去掉
        theme(axis.ticks=element_blank()) + ## 去掉左上角的坐标刻度线
        theme(panel.border=element_blank()) + ## 去掉最外层的正方形边框
        geom_text(aes(x = 5.0, y = ((ymin+ymax)/2),label = type) ,size=4,vjust=1.5)+
        geom_text(aes(x = 3.5, y = ((ymin+ymax)/2),label = rate_per) ,size=4.7,vjust=0.4)
    p1
    

    得到:


    image.png

    2. 绘制饼图:

    • 数据准备:
    df <- data.frame ( group = c( "Chemical", "Fodd component", "Benzonate"),  value = c(1, 2, 4) )
    head(df)
    
    image.png
    • 绘制柱状图:
    library(ggplot2)
     # Barplot
    bp<- ggplot(df, aes(x="", y=value, fill=group))+ geom_bar(width = 1, stat = "identity")
    bp
    
    image.png
    • 转换成饼图:
    pie <- bp + coord_polar("y", start=0)
    pie
    
    image.png
    • 进行调整:
    # Use custom color palettes
    pie<-pie+ scale_fill_manual(values=c("darkgreen", "green", "lightgreen"))
    # Use custom color palettes
    pie1<-pie + scale_fill_manual(values=c("darkgreen", "green", "lightgreen"))
    # Use custom color palettes
    pie2<-pie1+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")
    )
    pie2
    
    image.png
    • 设置空白theme函数:
    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)
    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 = (value)), size=5)
    

    [图片上传失败...(image-f0e7d1-1609838141963)]

    • 绘制对应数值的pie:
    library(scales)
    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 = (value/100)), size=5)
    
    image.png
    • 或者颜色调整:
    pie+scale_fill_brewer()+blank_theme + theme(axis.text.x=element_blank()) +geom_text(aes(y = value/3 + c(0, cumsum(value)[-length(value)]),  label = 100*(value/100)), size=5)
    
    image.png
    • 删除legend:
    pie+scale_fill_brewer()+blank_theme + theme(axis.text.x=element_blank()) +geom_text(aes(y = value/3 + c(0, cumsum(value)[-length(value)]),  label = 100*(value/100)), size=5)+ theme(legend.position = "none")
    
    image.png

    相关文章

      网友评论

          本文标题:Fig1-b 绘制环形图和饼图并拼接在一起 2021-01-03

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