zoukankan      html  css  js  c++  java
  • R绘图 第五篇:绘制散点图(ggplot2)

    ggplot2包中绘制点图的函数有两个:geom_point和 geom_dotplot,当使用geom_dotplot绘图时,point的形状是dot,不能改变点的形状,因此,geom_dotplot 叫做散点图(Scatter Plot),通过绘制点来呈现数据的分布,对点分箱的方法有两种:点密度(dot-density )和直方点(histodot)。当使用点密度分箱(bin)方式时,分箱的位置是由数据和binwidth决定的,会根据数据进行变化,但不会大于binwidth指定的宽度;当使用直方点分箱方式时,分箱有固定的位置和固定的宽度,就像由点构成的直方图(histogram)。

    bin是分箱的意思,在统计学中,数据分箱是一种把多个连续值分割成多个区间的方法,每一个小区间叫做一个bin(bucket),这就意味着每个bin定义一个数值区间,连续值会落到相应的区间中。

    对点进行分箱时,点的位置(Position adjustment)有多种调整方式:

    • identity:不调整
    • dodge:垂直方向不调整,只调整水平位置
    • nudge:在一定的范围内调整水平和垂直位置
    • jitter:抖动,当具有离散位置和相对较少的点数时,抖动很有用
    • jitterdodge:同时jitter和 dodge
    • stack:堆叠,
    • fill:填充,用于条形图

    每个位置调整都对应一个函数position_xxx()。

    当沿着x轴进行分箱,并沿着y轴堆叠时,y轴上的数字没有意义。

    当沿x轴进行分箱并沿y轴堆叠时,由于ggplot2的技术限制,y轴上的数字没有意义。 您可以隐藏y轴(如其中一个示例中所示),也可以手动缩放y轴以匹配点数。

    使用geom_dotplot()函数来绘制点图:

    geom_dotplot(mapping = NULL, data = NULL, position = "identity", ...,
      binwidth = NULL, binaxis = "x", method = "dotdensity",
      binpositions = "bygroup", stackdir = "up", stackratio = 1,
      dotsize = 1, stackgroups = FALSE, origin = NULL, right = TRUE,
      width = 0.9, drop = FALSE, na.rm = FALSE, show.legend = NA,
      inherit.aes = TRUE)

    常用的参数注释:

    • mapping:使用aes()来设置点图美学特征,参数x是因子,参数y是数值
    • data:数据框对象
    • position:位置调整(Position adjustment),默认值是identity,表示不调整位置。
    • method:默认值是dotdensity(点密度分箱),或者histodot(直方点,固定的分箱宽度)
    • binwidth:该参数用于调整分箱的宽度,该参数受到method参数的影响,如果method是dotdensity,那么binwidth指定分箱的最大宽度;如果method是histodot,那么binwidth指定分箱的固定宽度,默认值是数据范围(range of the data)的1/30。
    • binaxis:沿着那个轴进行分箱,默认值是x
    • stackdir:设置堆叠的方向,默认值是up,有效值是down、center、centerwhole和up。
    • stackratio:点堆叠的密集程度,默认值是1,值越小,堆集越密集;
    • dotsize:点的大小,相对于binwidth的直径,默认值是1。

    使用ToothGrowth数据来绘制点图:

    ToothGrowth$dose <- as.factor(ToothGrowth$dose)

     一,绘制点图

    绘制基本的点图

    library(ggplot2)
    
    p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + 
      geom_dotplot(binaxis='y', stackdir='center',
                   stackratio=1.5, dotsize=1.2)

    二,添加汇总数据

    向点图中添加汇总数据,使用ggplot2包中的函数

    stat_summary(mapping = NULL, data = NULL, geom = "pointrange",
      position = "identity", ..., fun.data = NULL, fun.y = NULL,
      fun.ymax = NULL, fun.ymin = NULL, fun.args = list(), na.rm = FALSE,
      show.legend = NA, inherit.aes = TRUE)

    常用参数注释:

    • fun.data:指定一个函数(function),返回带有变量ymin,y和ymax的数据框
    • 或者,指定三个单独的函数,分别向每个函数传递一个向量,分别返回一个数字,用于表示ymin、y和ymax:
      • fun.y:
      • fun.ymax:
      • fun.ymin:
    • fun.args=list():可选的参数,用于指定传递给fun.xxx函数的参数

    1,向点图中增加均值和中位数 

    # dot plot with mean points
    p + stat_summary(fun.y=mean, geom="point", shape=18,
                     size=3, color="red")
    # dot plot with median points
    p + stat_summary(fun.y=median, geom="point", shape=18,
                     size=3, color="red")

    2,向点图中增加点范围

    fun.low.mean <- function(x){mean(x)-sd(x)}
    fun.up.mean <- function(x){mean(x)+sd(x)}
    
    ggplot(ToothGrowth, aes(x=dose, y=len)) + 
      geom_dotplot(binaxis='y', stackdir='center', stackratio=1.5, dotsize=1.2)+
      stat_summary(fun.y = mean, fun.ymin = fun.low.mean, fun.ymax = fun.up.mean, colour = "red", size = 0.7)

    3,使用fun.data向点图中增加点范围

    data_summary <- function(x) {
      m <- mean(x)
      ymin <- m-sd(x)
      ymax <- m+sd(x)
      return(c(y=m,ymin=ymin,ymax=ymax))
    }
    
    ggplot(ToothGrowth, aes(x=dose, y=len)) + 
      geom_dotplot(binaxis='y', stackdir='center', stackratio=1.5, dotsize=1.2)+
      stat_summary(fun.data =  data_summary, colour = "red", size = 0.7)

    三,按照分组改变点图的颜色

    首先要对点图分组,这通过aes(fill=dose)来实现,然后,使用scale_fill_manual()来设置每个分组的颜色: 

    ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
      geom_dotplot(binaxis='y', stackdir='center')+
      scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))

    四,添加点图的说明

     通过theme函数向点图中增加说明,通过legend.position来控制说明的位置

    ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
      geom_dotplot(binaxis='y', stackdir='center')+
      scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
      theme(legend.position="top")

    五,向点图中增加标题和轴的标签

    通过labs()来添加标题和坐标轴的标签 

    ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
      geom_dotplot(binaxis='y', stackdir='center')+
      scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))+
      labs(title="Plot of length  by dose",x="Dose (mg)", y = "Length")+
      theme_classic()

    参考文档:

    ggplot2高效实用指南

    ggplot2入门与进阶(上)

    Dot plot

    ggplot2 dot plot : Quick start guide - R software and data visualization

  • 相关阅读:
    jdk1.8安装教程
    实现负载均衡的两种方式
    Java中ArrayList和LinkedList区别
    做一个英译中的命令行工具
    在 IDEA 中运行 Spark 程序报错:Multiple sources found for text.......please specify the fully qualified class name
    Android消息处理:EventBus、BroadCast和Handler-优缺点比较(转)
    C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter、转载)
    Android相关知识
    物联网相关知识
    Mqtt相关知识
  • 原文地址:https://www.cnblogs.com/ljhdo/p/4886067.html
Copyright © 2011-2022 走看看