美文网首页R数据科学R语言R语言学习
R数据科学--使用ggplot2进行数据可视化2

R数据科学--使用ggplot2进行数据可视化2

作者: PhageNanoenzyme | 来源:发表于2021-01-02 08:59 被阅读0次

    7.统计变换

    (1)diamonds数据集

    # ggplot2内置数据集,包含53940颗钻石的信息。
    # carat:克拉
    # cut:切割质量
    # color:颜色等级
    # clarity:纯净度等级
    # depth:深度比例
    # table:钻石顶部相对于最宽点的宽度
    # price:价格
    # "x" "y" "z" :长宽深
    # ↑以上来自帮助文档?diamonds
    

    (2)统计变换函数和几何对象函数

    # 统计变换:绘图时用来计算新数据的算法叫做统计变换stat
    # geom_bar做出的图纵坐标为count,是计算的新数据。
    # geom_bar的默认统计变换是stat_count,stat_count会计算出两个新变量-count(计数)和prop(proportions,比例)。
    # 每个几何对象函数都有一个默认的统计变换,每个统计变换函数都又一个默认的几何对象。
    # 用几何对象函数geom_bar作直方图,默认统计变换是stat_count,
      diamonds
    # A tibble: 53,940 x 10
       carat cut       color clarity depth table price     x     y     z
       <dbl> <ord>     <ord> <ord>   <dbl> <dbl> <int> <dbl> <dbl> <dbl>
     1 0.23  Ideal     E     SI2      61.5    55   326  3.95  3.98  2.43
     2 0.21  Premium   E     SI1      59.8    61   326  3.89  3.84  2.31
     3 0.23  Good      E     VS1      56.9    65   327  4.05  4.07  2.31
     4 0.290 Premium   I     VS2      62.4    58   334  4.2   4.23  2.63
     5 0.31  Good      J     SI2      63.3    58   335  4.34  4.35  2.75
     6 0.24  Very Good J     VVS2     62.8    57   336  3.94  3.96  2.48
     7 0.24  Very Good I     VVS1     62.3    57   336  3.95  3.98  2.47
     8 0.26  Very Good H     SI1      61.9    55   337  4.07  4.11  2.53
     9 0.22  Fair      E     VS2      65.1    61   337  3.87  3.78  2.49
    10 0.23  Very Good H     VS1      59.4    61   338  4     4.05  2.39
    # ... with 53,930 more rows
      count(diamonds,cut) #显示出现次数
    # A tibble: 5 x 2
      cut           n
      <ord>     <int>
    1 Fair       1610
    2 Good       4906
    3 Very Good 12082
    4 Premium   13791
    5 Ideal     21551
      ggplot(data = diamonds) + 
      geom_bar(mapping = aes(x = cut))
    
    image.png

    用统计变换函数stat_count做计数统计图,默认几何对象是直方图。

    ggplot(data = diamonds) +
    stat_count(mapping = aes(x = cut))


    image.png

    这两个代码做出的图片结果是一致的。两种方法没有优劣之分。

    (3)显示使用某种统计变换的原因

    # 覆盖默认的统计变换
    # 直方图默认的统计变换是stat_count,也就是统计计数。当需要直接用原表格的数据作图时就会需要覆盖默认的。
    
    demo <- tribble(
      ~cut,         ~freq,
      "Fair",       1610,
      "Good",       4906,
      "Very Good",  12082,
      "Premium",    13791,
      "Ideal",      21551
    ) #新建表格并赋值给demo
    
    ggplot(data = demo) +
      geom_bar(mapping = aes(x = cut, y = freq), stat = "identity") #覆盖默认的统计变换,使用identity。
    
    image.png

    覆盖从统计变换生成变量到图形属性的默认映射
    直方图默认的y轴是x轴的计数。此例子中x轴是是五种cut(切割质量),直方图自动统计了这五种质量的钻石的统计计数,当你不想使用计数,而是想显示各质量等级所占比例的时候就需要用到prop。

    ggplot(data = diamonds) + 
      geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
    
    image.png

    这里group=1的意思是把所有钻石作为一个整体,显示五种质量的钻石所占比例体现出来。如果不加这一句,就是每种质量的钻石各为一组来计算,那么比例就都是100%,显示五根大黑柱子,实在是丑出新高度。

    在代码中强调统计变换

    以stat_summary为例。

    ggplot(data = diamonds) + 
      stat_summary(
        mapping = aes(x = cut, y = depth),
        fun.ymin = min,
        fun.ymax = max,
        fun.y = median
      )
    
    image.png

    stat_summary的默认几何图形是geom_pointrange,而这个geom_pointrange默认的统计变换却是identity,如果你不知道其中猫腻,就会发现他俩代码竟然不可逆。。。)

    因此要用几何对象函数重复这个图形,则需要指定stat_summary。

    ggplot(data = diamonds) +
      geom_pointrange(
        mapping = aes(x = cut, y = depth),
        stat = "summary",
        fun.ymin = min,
        fun.ymax = max,
        fun.y = median
      )
    
    image.png

    8.位置调整-position

    # 在直方图中,颜色映射是由color和fill之分的,表示边框和填充。如果要设置无填充(也就是透明),则fill=NA。NA在数据框里表示空值。
    # (1)直方图之堆叠式-fill
    # 
    # 堆叠式就是在基础条形图上添加第三个变量,将这个变量映射给fill,就会在每个条形中分出不同颜色且不同比例的矩形。
    ggplot(data = diamonds) + 
      geom_bar(mapping = aes(x = cut,fill=clarity))
    
    image.png
    # 除了映射的方式以外,position参数设置位置调整功能。position="fill"也可以设置,但这样设置的每组堆叠条形具有相同的高度。
    ggplot(data = diamonds) + 
      geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")
    
    image.png

    (2)直方图之对象直接显示-identity

    ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) + 
      geom_bar(alpha = 1/5, position = "identity")
    
    image.png
    ggplot(data = diamonds, mapping = aes(x = cut, colour = clarity)) + 
      geom_bar(fill = NA, position = "identity")
    
    image.png

    书中23页identity设置透明度和无填充的图是错的!

    正确的是这样

    (3)直方图之并列式-dodge

    ggplot(data = diamonds) + 
      geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
    
    image.png

    (4)散点图之扰动-jitter

    书中翻译为抖动,我认为扰动更精确。

    # 以mpg的displ和hwy散点图为例
    ggplot(data = mpg) + 
      geom_point(mapping = aes(x = displ, y = hwy))
    
    image.png
    # 在这个例子中,数据有234行,图中却只有126个点。这就是因为有些点是重叠的,图上虽然显示一个点,但其实是好几个点重叠成了一个。
    # jitter可以为点添加随机扰动,使重叠的点分散开。
    # position参数设为jitter的快速实现:geom_jitter()
    # 除了geom_jitter外,geom_point也可以展示重叠点,会根据重叠点的个数调整大小。
    

    (5)stack-堆叠 无

    # ggplot(series, aes(time, value, group = type)) +
    #   geom_line(aes(colour = type), position = "stack") +
    #   geom_point(aes(colour = type), position = "stack")
    # 
    # ggplot(series, aes(time, value, group = type)) +
    #   geom_line(aes(colour = type)) +
    #   geom_point(aes(colour = type))
    

    设置position_stack(上)和不设置(下)的区别:

    9.坐标系

    (1)coord_flip翻转坐标系

    ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + 
      geom_boxplot() +
      coord_flip()
    
    image.png

    (2)coord_quickmap

    # 为地图设置长宽比
    # 此处需要加载maps包,否则会报错。
    library(maps)
    #如果报错则:install.packages("maps")
    #library(maps)
    nz <- map_data("nz")
    
    ggplot(nz, aes(long, lat, group = group)) +
      geom_polygon(fill = "white", colour = "black")
    
    image.png
    ggplot(nz, aes(long, lat, group = group)) +
      geom_polygon(fill = "white", colour = "black") +
      coord_quickmap()
    
    image.png

    geom_polygon 是多边形图

    (3)coord_polar 极坐标系

    bar <- ggplot(data = diamonds) + 
      geom_bar(
        mapping = aes(x = cut, fill = cut), 
        show.legend = FALSE,
        width = 1
      ) + 
      theme(aspect.ratio = 1) +
      labs(x = NULL, y = NULL)
    bar + coord_flip()
    
    image.png
    bar + coord_polar()
    
    image.png

    ps:习题中涉及的

    (1)关于饼图/牛眼图/圆圈图

    ggplot(mpg, aes(x = factor(1), fill = drv)) +
      geom_bar()
    
    image.png
    ggplot(mpg, aes(x = factor(1), fill = drv)) +
      geom_bar(width = 1) +
      coord_polar(theta = "y")
    
    image.png
    # 要点:
    # 
    # 不分组,只有一个因子型变量drv。
    # 如果作图不设置width,饼图中间会出现一个白色圈圈。经测试发现width等于几在图上并没有区别,但是不设置却不行。
    # 
    # theta是角度的意思。如果不设置这个参数就会得到牛眼图哈哈哈哈哈哈。
    # 
    # 作者取名叫牛眼图
    # 
    # 多圆圈图
    

    ggplot(data = diamonds) +
    geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill") +
    coord_polar(theta = "y")

    ![image.png](https://img.haomeiwen.com/i19009296/a5905f2362d5d6de.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
    
    # (2)第三题
    # 
    # geom_abline:添加线条
    # coord_fixed:保证横纵坐标的标尺一致,线条呈45°角
    # 10.完整的绘图模板
    # ggplot(data = <DATA>) + 
    #   <GEOM_FUNCTION>(
    #     mapping = aes(<MAPPINGS>),
    #     stat = <STAT>, 
    #     position = <POSITION>
    #   ) +
    #   <COORDINATE_FUNCTION> +
    #   <FACET_FUNCTION>
    #   图形构建的过程
    # 
    # (1)数据集
    # (2)统计变换
    # (3)几何对象
    # (4)映射fill
    # (5)放置
    # (6)映射x/y
    

    参考
    R数据科学--详解ggplot2

    相关文章

      网友评论

        本文标题:R数据科学--使用ggplot2进行数据可视化2

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