library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
str(X)
## 'data.frame': 1704 obs. of 6 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 ...
qplot最简单的使用方法和R base中的plot基本一样 可以用来画散点图
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(gdpPercap, lifeExp, data=X)
使用 color = year 可以将该变量用不同颜色标示出来, 这里year是个连续的变量 所以颜色以谱的形式标示, 用 log = "x" 表示对横轴的变量进行log变换
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(gdpPercap, lifeExp, data=X, log = "x", color = year)
也可以将year变成factor,此时会用离散的颜色标示
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(gdpPercap, lifeExp, data=X, log = "x", color = factor(year))
使用 size = pop 用标志的大小显示该变量的大小
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(gdpPercap, lifeExp, data=X, log = "x", color = year, size = pop)
还可以使用 shape = continent 用不同的标志来显示该变量
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(gdpPercap, lifeExp, data=X, log = "x", color = year, shape = continent)
最后 alpha=I(0.25) 指定透明度 0为全透明 1为不透明 当数据重叠严重时比较有用
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.25))
geom是geometric object的简称 用来生成不同种类的图
首先是smooth 用来描述数据的平滑趋势 注意此处 c(“point”, “smooth”) 表示先画point 再画smooth
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("point", "smooth"))
## `geom_smooth()` using method = 'gam'
相反的 c(“smooth”, “point”) 就是先画smooth 再画point
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("smooth", "point"))
## `geom_smooth()` using method = 'gam'
除了默认的平滑方法之外,还可以自行指定,比如线性模型
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("point", "smooth"), method=lm)
## Warning: Ignoring unknown parameters: method
另外还可以自行指定公式 例如多项式回归
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), geom=c("point", "smooth"), method=lm, formula = y ~ poly(x, 3))
## Warning: Ignoring unknown parameters: method, formula
line会将数据沿横轴方向按顺序连接起来 一般用来表示时间序列数据
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(pop, lifeExp, data=X, log = "x", alpha=I(0.5), color=year, geom="line")
path会将原始数据中相邻的两个点连接起来 一般用来表示二维数据随时间的变化
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(gdpPercap, lifeExp, data=X, log = "x", alpha=I(0.5), color=year, geom=c("point", "path"))
=====
和R base中的boxplot一样,横轴的数据需要是factor
注意 color=I(“red”) 中的I()是必须的 否则“red”会被当做一个新的factor
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
X$year.fac <- factor(X$year)
qplot(year.fac, lifeExp, data=X, color=I("red"), geom="boxplot")
jitter和boxplot类似
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
X$year.fac <- factor(X$year)
qplot(year.fac, lifeExp, data=X, color=I("red"), geom="jitter")
这里使用 fill=continent 将直方图按不同的continent分割开
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(lifeExp,data=X, geom="histogram", fill=continent)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
density与histogram类似
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(lifeExp,data=X, alpha=I(0.5), geom="density", color=continent)
使用facets可以将一个图根据一个或两个变量的值分别显示出来 有利于更直观地进行比较 ~左边表示每一行的变量 右边表示每一列的变量 比如 continent~. 根据continent值的不同 将density的图在每一行里显示出来
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(lifeExp,data=X, geom="density", facets=continent~.)
类似的 facets=year~continent 根据每一行year和每一列continent值的不同 将histogram的图显示出来
library(ggplot2)
X <- read.delim("http://www.stat.ubc.ca/~rickw/gapminderDataFiveYear.txt")
qplot(lifeExp,data=X, geom="histogram", facets=year~continent)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.