美文网首页作图
R可视化学习(7) -- 气泡图

R可视化学习(7) -- 气泡图

作者: 凯凯何_Boy | 来源:发表于2020-12-03 08:24 被阅读0次

    气泡图

    R包准备

    # R包准备
    library(ggplot2)
    library(dplyr)
    # 
    library(gapminder)
    

    数据预处理

    我们用到gapminder包中的gapminder数据集,该数据集储存了关于各国预期寿命、人均GDP和人口的数据。先看下数据结构

    > head(gapminder,5)
    # A tibble: 5 x 6
      country     continent  year lifeExp      pop gdpPercap
      <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
    1 Afghanistan Asia       1952    28.8  8425333      779.
    2 Afghanistan Asia       1957    30.3  9240934      821.
    3 Afghanistan Asia       1962    32.0 10267083      853.
    4 Afghanistan Asia       1967    34.0 11537966      836.
    5 Afghanistan Asia       1972    36.1 13079460      740.
    
    # 只选取2007年的数据
    data <- gapminder %>% filter(year=="2007") %>% dplyr::select(-year)
    

    基础气泡图

    # geom_point()函数
    ggplot(data, aes(x=gdpPercap, y=lifeExp, size = pop)) +geom_point(alpha=0.7)
    
    image-20201019153317579

    控制气泡大小

    通过scale_size()函数中range参数设置最小和最大气泡的范围,且通过name参数可以自定义图例的名字

    注意:圆圈经常重叠。为了避免在图表顶部出现大圆圈,您必须首先重新排序您的数据集。代码如下:

    data %>%
      arrange(desc(pop)) %>% # 按pop列降序
      mutate(country = factor(country, country)) %>% #设置为因子格式 
      ggplot(aes(x=gdpPercap, y=lifeExp, size = pop)) +
      geom_point(alpha=0.5) +
      scale_size_area(range = c(.1, 24), name="Population (M)")
    

    另外将数值变量映射到圆的大小的还有scale_radiusscale_size函数,可以?scale_radius自行了解。

    image-20201019160434074

    映射分组颜色

    # 输出分组颜色
    data %>%
      arrange(desc(pop)) %>%
      mutate(country = factor(country, country)) %>%
      ggplot(aes(x=gdpPercap, y=lifeExp, size=pop, color=continent)) +
        geom_point(alpha=0.5) +
        scale_size(range = c(.1, 24), name="Population (M)")
    
    image-20201019160642054

    进一步美化

    data %>%
      arrange(desc(pop)) %>%
      mutate(country = factor(country, country)) %>%
      ggplot(aes(x=gdpPercap, y=lifeExp, size=pop, fill=continent)) +
        geom_point(alpha=0.5, shape=21, color="black") +
        scale_size(range = c(.1, 24), name="Population (M)") +
        scale_fill_viridis(discrete=TRUE, guide=FALSE, option="A") +
        theme_ipsum() +
        theme(legend.position="bottom") +
        ylab("Life Expectancy") +
        xlab("Gdp per Capita") +
        theme(legend.position = "none")
    
    image-20201019160735797

    交互式气泡图

    绘制交互式图形需要用到plotly包,我们还是利用上面的数据集来进行绘制。

    R包准备

    # 加载
    library(ggplot2)
    library(dplyr)
    library(plotly)
    library(viridis)
    library(hrbrthemes) 
    
    # 还是利用上面数据集
    library(gapminder)
    data <- gapminder %>% filter(year=="2007") %>% dplyr::select(-year)
    

    数据预处理

    利用dplyr包来管道操作清洗数据,

    newdata <- data %>%
      mutate(gdpPercap=round(gdpPercap,0)) %>% # 四舍五入取整
      mutate(pop=round(pop/1000000,2)) %>% # 保留俩位小数
      mutate(lifeExp=round(lifeExp,1)) %>% # 保留俩位小数
      
      # Reorder countries to having big bubbles on top
      arrange(desc(pop)) %>%
      mutate(country = factor(country, country)) %>%
      
      # prepare text for tooltip
      mutate(text = paste("Country: ", country, "\nPopulation (M): ", pop, "\nLife Expectancy: ", lifeExp, "\nGdp per capita: ", gdpPercap, sep=""))  #增加每个气泡标签
    
    
    

    绘制图形

    plot <- ggplot(newdata, aes(x=gdpPercap, y=lifeExp, size = pop, color = continent, text=text)) +
      geom_point(alpha=0.7) +
      scale_size(range = c(1.4, 19), name="Population (M)") +
      scale_color_viridis(discrete=TRUE, guide=FALSE) +
      theme_ipsum() +
      theme(legend.position="none")
      pp  
    
    image-20201019162002611

    保存图形

    # 1. 先将ggplot图形转换为交互式图形
    pp <- ggplotly(plot, tooltip="text")
    pp
    # 利用saveWidget函数保存交互式图形
    library(htmlwidgets)
    saveWidget(pp, file=  "./ggplotlyBubblechart.html")
    
    image-20201019162440105

    打开我们保存的html文件,可以看道鼠标停留在每个气泡上就会显示出我们添加的标签内容了,而且通过右上角的按钮可以进行拍照保存png格式图形、调整交互图形大小等功能。

    富集分析气泡图

    气泡图的本质就是散点图,通过数据集中的离散/连续变量等去映射气泡大小颜色产生各种气泡图,懂得了这个道理,我们再来动手绘制下转录组中常见的富集气泡图练习一下吧~~

    准备数据

    image-20201019164956347
    • 第一列:pathway名称
    • 第二列:富集基因比例
    • 第三列:Qvalue
    • 第四列:基因数量

    绘制图形

    pathway <- read.delim('KEGG_pathway.txt',check.names = FALSE,header = T,sep = '\t')
    p <- ggplot(pathway,aes(GeneRatio,Description))+geom_point(aes(size = Count,color = -1*log10(qvalue)))+
      scale_color_gradient(low = "green",high = 'red')+
      scale_size(range = c(1.4, 19), name="Gene")+
      labs(color = expression(-log[10](qvalue)),size = 'Gene',
           x = 'GeneRatio',
           y = 'Pathway')+
      theme(panel.grid = element_blank(), panel.background = element_rect(fill = 'transparent', color = 'black'),legend.key = element_blank())
    p
    
    image-20201019165509871

    分组情况

    当我们有多样品时候,也可以添加一列样品信息,然后将图中的横坐标改变为每个样品进行绘图

    pathway <- pathway %>% mutate(type = c(rep('D',15),rep('H',16))) # 添加一列样品类型
    
    # 绘制图形
    p <- ggplot(pathway,aes(type,Description))+geom_point(aes(size = Count,color = -1*log10(qvalue)))+
      scale_color_gradient(low = "green",high = 'red')+
      scale_size(range = c(1.4, 19), name="Gene")+
      labs(color = expression(-log[10](qvalue)),size = 'Gene',
           x = 'Type',
           y = 'Pathway')+
      theme(panel.grid = element_blank(), panel.background = element_rect(fill = 'transparent', color = 'black'),legend.key = element_blank())
    p  
    
    image-20201019165631943

    相关文章

      网友评论

        本文标题:R可视化学习(7) -- 气泡图

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