生信人

找回密码
立即注册
搜索
热搜: 活动 交友 discuz
发新帖

0

收听

12

听众

278

主题
发表于 2021-9-25 23:15:35 | 查看: 1935| 回复: 0

本文介绍如何使用ggplot2快速绘制常用图形,不纠缠底层原理,续前篇:

本文速览

  1. 1、ggplot2内置数据集
  2. 2、ggplot2快速绘图
  3.   绘图必备三要素
  4.   aes设置
  5.   分面(facet)
  6.   几何对象(geom)
  7.   图形渲染(print)
  8.   图形保存(ggsave)
复制代码

1、ggplot2内置数据集

ggplot2包含哪些内置的数据集?

  1. library('ggplot2')
  2. data(package = 'ggplot2')#查看ggplot2内置数据集
复制代码
  1. Package Item Title
  2. ggplot2 diamonds Prices of over 50,000 round cut diamonds
  3. ggplot2 economics US economic time series
  4. ggplot2 economics_long US economic time series
  5. ggplot2 faithfuld 2d density estimate of Old Faithful data
  6. ggplot2 luv_colours 'colors()' in Luv space
  7. ggplot2 midwest Midwest demographics
  8. ggplot2 mpg Fuel economy data from 1999 to 2008 for 38 popular models of cars
  9. ggplot2 msleep An updated and expanded version of the mammals sleep dataset
  10. ggplot2 presidential Terms of 11 presidents from Eisenhower to Obama
  11. ggplot2 seals Vector field of seal movements
  12. ggplot2 txhousing Housing sales in TX
复制代码

本文使用mpg数据集,重点介绍:

浏览下mpg数据

  1. > head(mpg)#看下数据集的前几列
  2. # A tibble: 6 x 11
  3.   manufacturer model displ  year   cyl trans    drv     cty   hwy fl    class
  4.   <chr>        <chr> <dbl> <int> <int> <chr>    <chr> <int> <int> <chr> <chr>
  5. 1 audi         a4      1.8  1999     4 auto(l5) f        18    29 p     comp~
  6. 2 audi         a4      1.8  1999     4 manual(~ f        21    29 p     comp~
  7. 3 audi         a4      2    2008     4 manual(~ f        20    31 p     comp~
  8. 4 audi         a4      2    2008     4 auto(av) f        21    30 p     comp~
  9. 5 audi         a4      2.8  1999     6 auto(l5) f        16    26 p     comp~
  10. 6 audi         a4      2.8  1999     6 manual(~ f        18    26 p     comp~
复制代码

查看下mpg的大小

  1. > dim(mpg)#查看mpg的大小
  2. [1] 234  11
复制代码

mpg数据集每个变量简介

  1. manufacturer    生产厂家,如奥迪audi、吉普jeep等
  2. model    model name 车型,如奥迪A4、奥迪A6等   
  3. displ    engine displacement, in litres,发动机排量,单位为每升
  4. year    year of manufacture,出厂年份
  5. cyl    number of cylinders,气缸数量   
  6. trans    type of transmission,传输类型,手动还是自动   
  7. drv    f = front-wheel drive, r = rear wheel drive, 4 = 4wd,驱动类型 ,前轮还是后轮驱动   
  8. cty    city miles per gallon,每加仑油城市驾驶里程数
  9. hwy    highway miles per gallon,每加仑油高速驾驶里程数
  10. fl    fuel type,燃油型号
  11. class    "type" of car,suv、桑塔纳等
复制代码

2、ggplot2快速绘图

绘图必备三要素

数据集(data)

图像属性(aes)

几何对象(geom)

画张散点图

  1. library('ggplot2')
  2. ggplot(mpg, #数据集(data)
  3.           aes(x = displ, y = hwy)) + #图像属性(aes),即指定x、y轴要投影的数据
  4.           #data和aes通过ggplot组合在一起,使用+号添加图层(layers)
  5.           geom_point()#几何对象(geom),此处为散点图
复制代码

aes设置

Colour, size, shape等等

  1. ggplot(mpg, aes(displ, cty, colour = class)) + #按变量class分类绘制分类散点图
  2.           geom_point()
复制代码

  1. ggplot(mpg, aes(displ, hwy, shape = drv)) + #按变量drv不同绘制不同类不同shape散点图
  2.           geom_point()
复制代码

  1. ggplot(mpg, aes(displ, hwy, size = cyl)) + #按变量cyl值大小绘制不同类不同size散点图
  2.           geom_point()
复制代码

分面(facet)

  1. ggplot(mpg, aes(displ, hwy)) + geom_point() +
  2.          facet_wrap(~class)#按照class分面
复制代码

几何对象(geom)

  1. p1 <- ggplot(mpg, aes(drv, hwy)) + geom_jitter()
  2. p2 <- ggplot(mpg, aes(drv, hwy)) + geom_boxplot()
  3. p3 <- ggplot(mpg, aes(drv, hwy)) + geom_violin()
  4. p4 <- ggplot(mpg, aes(hwy)) + geom_histogram()
  5. p5 <- ggplot(mpg, aes(hwy)) + geom_freqpoly()
  6. p6 <- ggplot(mpg, aes(manufacturer)) + geom_bar()
  7. p7 <- ggplot(economics, aes(date, unemploy/pop)) + geom_line()#时间序列图
  8. p8 <- ggplot(economics, aes(unemploy/pop, uempmed)) + geom_path() + geom_point()
  9. grid.arrange(p1, p2, p3, p4, p5, p6, p7, p8, nrow = 4)#分行显示子图
复制代码

图形渲染(print)

使用print函数

print(p8)

图形保存(ggsave)

  1. p9 <- grid.arrange(p1, p2, p3, p4, p5, p6, p7, p8, nrow = 4)
  2. ggsave("plot.png", p9, width = 5, height = 5)#ggsave保存图形,可是设置图的宽高等
复制代码

参考资料:https://ggplot2-book.org/getting-started.html


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

您需要登录后才可以回帖 登录 | 立即注册

QQ|Archiver|手机版|小黑屋|生信人

GMT+8, 2024-4-26 20:54 , Processed in 0.044124 second(s), 21 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表