ggplot2笔记
下面是ggplot2的一些文档和github上的源代码
http://docs.ggplot2.org/current/
https://github.com/hadley/ggplot2
本篇文章涉及ggplot2: Elegant Graphics for Data Analysis 中的第二章
在正式开始学习ggplot2命令之前 我们首先讨论qplot
qplot是quick plot的缩写 旨在用最简短的命令画出我们所需的图
==========
1. 数据
首先在R中安装ggplot2
install.packages("ggplot2")
library(ggplot2)
我们使用的数据
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
str(X)
'data.frame': 1704 obs. of 7 variables:
$ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
$ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
$ pop : num 8425333 9240934 10267083 11537966 13079460 ...
$ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
$ lifeExp : num 28.8 30.3 32 34 36.1 ...
$ gdpPercap: num 779 821 853 836 740 ...
$ year.fac : Factor w/ 12 levels "1952","1957",..: 1 2 3 4 5 6 7 8 9 10 ...
==========
2. qplot基本用法
qplot最简单的使用方法和R base中的plot基本一样 可以用来画散点图
qplot(gdpPercap, lifeExp, data=X)
使用 color = year 可以将该变量用不同颜色标示出来
这里year是个连续的变量 所以颜色以谱的形式标示
用 log = "x" 表示对横轴的变量进行log变换
qplot(gdpPercap, lifeExp, data=X, log = "x", color = year)
也可以将year变成factor 此时会用离散的颜色标示
qplot(gdpPercap, lifeExp, data=X, log = "x", color = factor(year))
使用 size = pop 用标志的大小显示该变量的大小
qplot(gdpPercap, lifeExp, data=X, log = "x", color = year, size = pop)
还可以使用 shape = continent 用不同的标志来显示该变量
qplot(gdpPercap, lifeExp, data=X, log = "x", color = year, shape = continent)
最后 alpha=I(0.25) 指定透明度 0为全透明 1为不透明
当数据重叠严重时比较有用
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.25))
==========
3. geom
geom是geometric object的简称 用来生成不同种类的图
=====
3.1 smooth
首先是smooth 用来描述数据的平滑趋势
注意此处 c("point", "smooth") 表示先画point 再画smooth
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("point", "smooth"))
相反的 c("smooth", "point") 就是先画smooth 再画point
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("smooth", "point"))
除了默认的平滑方法之外 还可以自行指定 比如线性模型
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("point", "smooth"), method=lm)
另外还可以自行指定公式 例如多项式回归
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("point", "smooth"), method=lm, formula = y ~ poly(x, 3))
=====
3.2 line和path
line会将数据沿横轴方向按顺序连接起来 一般用来表示时间序列数据
qplot(pop, lifeExp, data=X, log = "x", alpha=I(0.5), color=year, geom="line")
path会将原始数据中相邻的两个点连接起来 一般用来表示二维数据随时间的变化
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), color=year, geom=c("point", "path"))
=====
3.3 boxplot和jitter
和R base中的boxplot一样 横轴的数据需要是factor
X$year.fac <- factor(X$year)
注意 color=I("red") 中的I()是必须的 否则"red"会被当做一个新的factor
qplot(year.fac, lifeExp, data=X, color=I("red"), geom="boxplot")
jitter和boxplot类似
qplot(year.fac, lifeExp, data=X, color=I("red"), geom="jitter")
=====
3.4 histogram和density
这里使用 fill=continent 将直方图按不同的continent分割开
qplot(lifeExp,data=X, geom="histogram", fill=continent)
density与histogram类似
qplot(lifeExp,data=X, alpha=I(0.5), geom="density", color=continent)
==========
4. facets
使用facets可以将一个图根据一个或两个变量的值分别显示出来 有利于更直观地进行比较
~左边表示每一行的变量 右边表示每一列的变量
比如 continent~. 根据continent值的不同 将density的图在每一行里显示出来
qplot(lifeExp,data=X, geom="density", facets=continent~.)
类似的 facets=year~continent 根据每一行year和每一列continent值的不同 将histogram的图显示出来
qplot(lifeExp,data=X, geom="histogram", facets=year~continent)
http://docs.ggplot2.org/current/
https://github.com/hadley/ggplot2
本篇文章涉及ggplot2: Elegant Graphics for Data Analysis 中的第二章
在正式开始学习ggplot2命令之前 我们首先讨论qplot
qplot是quick plot的缩写 旨在用最简短的命令画出我们所需的图
==========
1. 数据
首先在R中安装ggplot2
install.packages("ggplot2")
library(ggplot2)
我们使用的数据
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
str(X)
'data.frame': 1704 obs. of 7 variables:
$ country : Factor w/ 142 levels "Afghanistan",..: 1 1 1 1 1 1 1 1 1 1 ...
$ year : int 1952 1957 1962 1967 1972 1977 1982 1987 1992 1997 ...
$ pop : num 8425333 9240934 10267083 11537966 13079460 ...
$ continent: Factor w/ 5 levels "Africa","Americas",..: 3 3 3 3 3 3 3 3 3 3 ...
$ lifeExp : num 28.8 30.3 32 34 36.1 ...
$ gdpPercap: num 779 821 853 836 740 ...
$ year.fac : Factor w/ 12 levels "1952","1957",..: 1 2 3 4 5 6 7 8 9 10 ...
==========
2. qplot基本用法
qplot最简单的使用方法和R base中的plot基本一样 可以用来画散点图
qplot(gdpPercap, lifeExp, data=X)
![]() |
使用 color = year 可以将该变量用不同颜色标示出来
这里year是个连续的变量 所以颜色以谱的形式标示
用 log = "x" 表示对横轴的变量进行log变换
qplot(gdpPercap, lifeExp, data=X, log = "x", color = year)
![]() |
也可以将year变成factor 此时会用离散的颜色标示
qplot(gdpPercap, lifeExp, data=X, log = "x", color = factor(year))
![]() |
使用 size = pop 用标志的大小显示该变量的大小
qplot(gdpPercap, lifeExp, data=X, log = "x", color = year, size = pop)
![]() |
还可以使用 shape = continent 用不同的标志来显示该变量
qplot(gdpPercap, lifeExp, data=X, log = "x", color = year, shape = continent)
![]() |
最后 alpha=I(0.25) 指定透明度 0为全透明 1为不透明
当数据重叠严重时比较有用
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.25))
![]() |
==========
3. geom
geom是geometric object的简称 用来生成不同种类的图
=====
3.1 smooth
首先是smooth 用来描述数据的平滑趋势
注意此处 c("point", "smooth") 表示先画point 再画smooth
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("point", "smooth"))
![]() |
相反的 c("smooth", "point") 就是先画smooth 再画point
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("smooth", "point"))
![]() |
除了默认的平滑方法之外 还可以自行指定 比如线性模型
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("point", "smooth"), method=lm)
![]() |
另外还可以自行指定公式 例如多项式回归
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("point", "smooth"), method=lm, formula = y ~ poly(x, 3))
![]() |
=====
3.2 line和path
line会将数据沿横轴方向按顺序连接起来 一般用来表示时间序列数据
qplot(pop, lifeExp, data=X, log = "x", alpha=I(0.5), color=year, geom="line")
![]() |
path会将原始数据中相邻的两个点连接起来 一般用来表示二维数据随时间的变化
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), color=year, geom=c("point", "path"))
![]() |
=====
3.3 boxplot和jitter
和R base中的boxplot一样 横轴的数据需要是factor
X$year.fac <- factor(X$year)
注意 color=I("red") 中的I()是必须的 否则"red"会被当做一个新的factor
qplot(year.fac, lifeExp, data=X, color=I("red"), geom="boxplot")
![]() |
jitter和boxplot类似
qplot(year.fac, lifeExp, data=X, color=I("red"), geom="jitter")
![]() |
=====
3.4 histogram和density
这里使用 fill=continent 将直方图按不同的continent分割开
qplot(lifeExp,data=X, geom="histogram", fill=continent)
![]() |
density与histogram类似
qplot(lifeExp,data=X, alpha=I(0.5), geom="density", color=continent)
![]() |
==========
4. facets
使用facets可以将一个图根据一个或两个变量的值分别显示出来 有利于更直观地进行比较
~左边表示每一行的变量 右边表示每一列的变量
比如 continent~. 根据continent值的不同 将density的图在每一行里显示出来
qplot(lifeExp,data=X, geom="density", facets=continent~.)
![]() |
类似的 facets=year~continent 根据每一行year和每一列continent值的不同 将histogram的图显示出来
qplot(lifeExp,data=X, geom="histogram", facets=year~continent)
![]() |