查看原文
其他

R可视化02|ggplot2-ggplot2快速绘图

pythonic生物人 pythonic生物人 2022-10-25

"pythonic生物人"的第100篇分享

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

R可视化01|ggplot2-ggplot2简介

本文速览1、ggplot2内置数据集
2、ggplot2快速绘图
  绘图必备三要素
  aes设置
  分面(facet)
  几何对象(geom)
  图形渲染(print
  图形保存(ggsave)

1、ggplot2内置数据集

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

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

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

  • 浏览下mpg数据
> head(mpg)#看下数据集的前几列
# A tibble: 6 x 11
  manufacturer model displ  year   cyl trans    drv     cty   hwy fl    class
  <chr>        <chr> <dbl> <int> <int> <chr>    <chr> <int> <int> <chr> <chr>
1 audi         a4      1.8  1999     4 auto(l5) f        18    29 p     comp~
2 audi         a4      1.8  1999     4 manual(~ f        21    29 p     comp~
3 audi         a4      2    2008     4 manual(~ f        20    31 p     comp~
4 audi         a4      2    2008     4 auto(av) f        21    30 p     comp~
5 audi         a4      2.8  1999     6 auto(l5) f        16    26 p     comp~
6 audi         a4      2.8  1999     6 manual(~ f        18    26 p     comp~
  • 查看下mpg的大小
> dim(mpg)#查看mpg的大小
[1234  11
  • mpg数据集每个变量简介
manufacturer    生产厂家,如奥迪audi、吉普jeep等
model    model name 车型,如奥迪A4、奥迪A6等    
displ    engine displacement, in litres,发动机排量,单位为每升
year    year of manufacture,出厂年份
cyl    number of cylinders,气缸数量    
trans    type of transmission,传输类型,手动还是自动    
drv    f = front-wheel drive, r = rear wheel drive, 4 = 4wd,驱动类型 ,前轮还是后轮驱动    
cty    city miles per gallon,每加仑油城市驾驶里程数
hwy    highway miles per gallon,每加仑油高速驾驶里程数
fl    fuel type,燃油型号
class    "type" of car,suv、桑塔纳等

2、ggplot2快速绘图

绘图必备三要素

  • 数据集(data)
  • 图像属性(aes)
  • 几何对象(geom)

画张散点图

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

aes设置

Colour, size, shape等等

ggplot(mpg, aes(displ, cty, colour = class)) + #按变量class分类绘制分类散点图
  geom_point()
ggplot(mpg, aes(displ, hwy, shape = drv)) + #按变量drv不同绘制不同类不同shape散点图
  geom_point()
ggplot(mpg, aes(displ, hwy, size = cyl)) + #按变量cyl值大小绘制不同类不同size散点图
  geom_point()

分面(facet

ggplot(mpg, aes(displ, hwy)) + geom_point() + 
    facet_wrap(~class)#按照class分面

几何对象(geom

ggplot2可绘制很多基本图,一行代码即可搞定,列出部分。

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

图形渲染(print)

使用print函数

图形保存(ggsave)

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

参考资料:https://ggplot2-book.org/getting-started.html
本文结束,更多好文,欢迎关注:pythonic生物人

您可能也对以下帖子感兴趣

文章有问题?点此查看未经处理的缓存