R -> ggplot2包(1)

ggplot2包说明文档学习笔记(1)

  • ggplot2的绘图数据来源是一个dataframe
  • ggplot2通过一次次地添加图层来达到绘图的目的,每个图层之间通过+连接
  • ggplot2并不会在乎画出来的图是否有实际意义,所以当使用了不正确的代码时有可能会生成一些很奇葩的图,但是如果有足够的想象力,可以利用ggplot2画出各种图像
  • 要画出特别好看且有意义的图,需要的不仅仅是想象力和写代码的能力,还需要对色彩,构图等的掌控,基本可以确认的是我在这方面狗屁不是
  • ggplot2的基本图形函数中几乎都包含statposition两个参数,stat决定了对数据进行变换的方式,position决定了对图形进行摆放的方式,修改这两个参数,基本上就是奇葩图像出现的开始

ggplot2中的基本图形

在ggplot2中需要通过aes()来设定x轴与y轴的数据,故根据x和y的数据类型,把ggplot2中的基本图形分为以下几种

  1. 连续型x 连续型y
  2. 连续性x 不需要y
  3. 离散型x 连续型y
  4. 离散型x 不需要y
  5. 不需要x 不需要y

连续型x 连续型y

geom_point(stat = "identity", position = "identity")
  • 散点图
geom_jitter(stat = "identity", position = "jitter")
position_jitter(width = NULL, height = NULL)
  • 对散点图增加了一个扰动,position_jitter()的返回值可以作为参数position的值,适合重复数据较多导致呈现离散化的小数据集
geom_count(stat = "sum", position = "identity")
stat_sum(geom = "point", position = "identity")
  • 对每个位置点的数量计数,适合离散数据,当然连续型数据也能用就是没什么用
geom_bin2d(stat = "bin2d", position = "identity")
stat_bin_2d(geom = "tile", position = "identity")

geom_hex(stat = "binhex", position = "identity")
stat_bin_hex(geom = "hex", position = "identity")
  • 根据范围内点的数量来填充颜色,适合数据量特别大的数据,geom_bin2d为矩形范围,geom_hex为六边形范围
geom_line(stat = "identity", position = "identity")
geom_path(stat = "identity", position = "identity") 
geom_step(stat = "identity", position = "identity", direction = "hv")
  • geom_line将图上的点从左至右依次连接
    geom_path将图上的点按照源数据中的顺序依次连接
    geom_step将图上的点从左至右阶梯型连接,参数direction决定是先水平再垂直(hv)连接还是先垂直再水平(vh)连接
geom_label(stat = "identity", position = "identity")
geom_text(stat = "identity", position = "identity")
  • geom_label添加标签,就是在文本的后面还有一个小背景
    geom_text添加文本,纯文本
geom_col(position = "stack")
  • 类似于柱形图,区别在于柱形图是对于x计数,geom_col是对于y求和
geom_tile(stat = "identity", position = "identity", width, height)
geom_rect(stat = "identity", position = "identity", aes(xmin, xmax, ymin, ymax))
geom_raster(stat = "identity", position = "identity", hjust = 0.5, vjust = 0.5)
  • 三个图形都是在点的后方绘制一个长方形,区别在于:
    geom_tile使用widthheight两个参数代表绘制的长方形的长和宽,点在长方形中央
    geom_rect使用aes(xmin, xmax, ymin, ymax)几个参数来决定长方形四条边的位置,点有可能不在长方形中央
    geom_raster的长方形的长和宽为所有点中相隔最近的两个点的水平距离和垂直距离,hjustvjust都取1时,长方形在点的右上角。但是这个函数似乎有bug,如下:
df <- data.frame(x1 = c(1,2,4,8,16), y1 = c(1,2,4,8,16),
                 x2 = c(1,2,5,8,16), y2 = c(1,2,5,8,16))
ggplot(df) + 
  geom_raster(aes(x1, y1), fill = "blue", alpha = 0.3, hjust = 1, vjust = 1) + 
  geom_point(aes(x1, y1), colour = "blue", shape = 2) +
  geom_raster(aes(x2, y2), fill = "red", alpha = 0.3, hjust = 1, vjust = 1) + 
  geom_point(aes(x2, y2), colour = "red", shape = 1)
geom_crossbar(stat = "identity", position = "identity", aes(ymax, ymin))
geom_errorbar(stat = "identity", position = "identity", aes(ymax, ymin))
geom_linerange(stat = "identity", position = "identity", aes(ymax, ymin))
geom_pointrange(stat = "identity", position = "identity", aes(ymax, ymin))
geom_errorbarh(stat = "identity", position = "identity", aes(xmax, xmin))
  • 各种添加垂直间隔,对应图形如下图


geom_rug(stat = "identity", position = "identity")
  • 在图的边缘添加描述x和y的边缘分布的小线段
geom_segment(stat = "identity", position = "identity", aes(xend, yend))
geom_curve(stat = "identity", position = "identity", aes(xend, yend))
geom_spoke(stat = "identity", position = "identity", aes(angle), radius)
  • geom_segment在点(x, y)和点(xend, yend)之间画上线段,而geom_curve在两个点之间画上曲线,geom_spoke通过极坐标来添加线段,angle代表角度,radius代表半径
geom_smooth(stat = "smooth", position = "identity")
stat_smooth(geom = "smooth", position = "identity")
  • 添加拟合曲线
geom_polygon(stat = "identity", position = "identity")
  • 将点通过geom_path连接后内部填充颜色
geom_area(stat = "identity", position = "stack")
geom_ribbon(stat = "identity", position = "identity", aes(ymin, ymax))
  • geom_ribbonyminymax之间填充颜色,geom_area其实就是ymin = 0, ymax = ygeom_ribbon

连续型x 不需要y

geom_histogram(stat = "bin", position = "stack")
geom_freqpoly(stat = "bin", position = "identity")
stat_bin(geom = "bar", position = "stack")
  • 直方图,geom_freqpoly相当于用线来描绘直方图的边界
geom_density(stat = "density", position = "identity")
stat_density(geom = "area", position = "stack")
  • 密度曲线,相比较于直方图来说更加平滑

离散型x 连续型y

geom_boxplot(stat = "boxplot", position = "dodge2")
stat_boxplot(geom = "boxplot", position = "dodge2")
  • 箱线图
geom_violin(stat = "ydensity", position = "dodge")
stat_ydensity(geom = "violin", posion = "dodge")
  • 小提琴图。类似于箱线图,不过在中央部分不用矩形而用类似于小提琴的图形,从而能够大致看出中央部分数据的密度曲线

离散型x 不需要y

geom_bar(stat = "count", position = "stack")
stat_count(geom = "bar", position = "stack")
  • 柱形图,对x进行计数

不需要x 不需要y

geom_abline(slope, intercept)
geom_hline(yintercept)
geom_vline(xintercept)
geom_blank()
  • geom_abline添加一条斜率为slope截距为intercept的直线,geom_hline添加一条y = yintercept的直线,geom_vline添加一条x = xintercept的直线,geom_blank什么也不干
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。