# install.packages('ggplot2') 安装ggplot2包 install.packages('gcookbook')
# 安装gcookbook包
library(ggplot2)  #加载ggplot2包
library(gcookbook)  #加载本书的数据包

第4章 折线图

4.1 绘制简单折线图

运行ggplot()和geom_line()
BOD  #作图使用的数据集
##   Time demand
## 1    1    8.3
## 2    2   10.3
## 3    3   19.0
## 4    4   16.0
## 5    5   15.6
## 6    7   19.8
ggplot(BOD, aes(x = Time, y = demand)) + geom_line()  #变量Time为连续型变量
BOD1 <- BOD
BOD1$Time <- factor(BOD1$Time)  #将变量Time设置成为因子变量
ggplot(BOD1, aes(x = Time, y = demand, group = 1)) + geom_line()  #Time为因子变量时,x轴没有x=6,同时需要加group=1,将数据设置为同一分组
# ggplot(BOD1,aes(x=Time,y=demand))+geom_line()未加group=1,将会出错

4.2 向折线图添加数据标记

在绘制基本折线图的基础上增加geom_point()
ggplot(BOD, aes(x = Time, y = demand)) + geom_line() + geom_point()

4.3 绘制多重折线图

在分别设置一个映射给x和y的基础上,再将一个变量映射给颜色或线型即可
library(plyr)
tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = mean(len))
tg
##   supp dose length
## 1   OJ  0.5  13.23
## 2   OJ  1.0  22.70
## 3   OJ  2.0  26.06
## 4   VC  0.5   7.98
## 5   VC  1.0  16.77
## 6   VC  2.0  26.14
ggplot(tg, aes(x = dose, y = length, colour = supp)) + geom_line()
ggplot(tg, aes(x = dose, y = length, linetype = supp)) + geom_line()
ggplot(tg, aes(x = dose, y = length, shape = supp)) + geom_line() + geom_point(size = 4)
ggplot(tg, aes(x = dose, y = length, fill = supp)) + geom_line() + geom_point(size = 4, 
    shape = 21)

4.4 绘制多重折线图

通过设置线型(linetype)、线宽(size)和颜色(colour)参数可以分别修改折线的线型、线宽和颜色
ggplot(BOD, aes(x = Time, y = demand)) + geom_line(linetype = "dashed", size = 1, 
    colour = "blue")
library(plyr)
tg <- ddply(ToothGrowth, c("supp", "dose"), summarise, length = mean(len))
tg
##   supp dose length
## 1   OJ  0.5  13.23
## 2   OJ  1.0  22.70
## 3   OJ  2.0  26.06
## 4   VC  0.5   7.98
## 5   VC  1.0  16.77
## 6   VC  2.0  26.14
ggplot(tg, aes(x = dose, y = length, colour = supp)) + geom_line() + scale_colour_brewer(palette = "Set1")
ggplot(tg, aes(x = dose, y = length, group = supp)) + geom_line(colour = "darkgreen", 
    size = 1.5)
ggplot(tg, aes(x = dose, y = length, colour = supp)) + geom_line(linetype = "dashed") + 
    geom_point(shape = 21, size = 3, fill = "white")

4.5 修改数据标记样式

在函数aes()外部设定函数geom_point()的大小、颜色和填充色。数据标记默认的形状是实线圆圈,大小为2,颜色是黑色
ggplot(BOD, aes(x = Time, y = demand)) + geom_line() + geom_point(size = 4, 
    shape = 22, fill = "pink", colour = "darkred")
ggplot(BOD, aes(x = Time, y = demand)) + geom_line() + geom_point(size = 4, 
    shape = 21, fill = "white", colour = "darkred")

4.6 绘制面积图

运行geom_area()函数即可绘制面积图
sunspotyear <- data.frame(Year = as.numeric(time(sunspot.year)), Sunspots = as.numeric(sunspot.year))
head(sunspotyear)
##   Year Sunspots
## 1 1700        5
## 2 1701       11
## 3 1702       16
## 4 1703       23
## 5 1704       36
## 6 1705       58
ggplot(sunspotyear, aes(x = Year, y = Sunspots)) + geom_area(colour = "black", 
    fill = "blue", alpha = 0.2)
# 给整个面积图添加边框线之后,系统会在面积图的起点和终点位置分别绘制一条垂直线,且底部绘制了一条横线。为了修正上述情况,可以先绘制不带边框线的面积图,然后添加新图层,并用geom_line绘制轨迹线
ggplot(sunspotyear, aes(x = Year, y = Sunspots)) + geom_area(fill = "blue", 
    alpha = 0.2) + geom_line()

4.7 绘制堆积面积图

运行geom_area()函数,并映射一个因子变量给填充色fill即可
head(uspopage)  #数据集
##   Year AgeGroup Thousands
## 1 1900       <5      9181
## 2 1900     5-14     16966
## 3 1900    15-24     14951
## 4 1900    25-34     12161
## 5 1900    35-44      9273
## 6 1900    45-54      6437
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) + geom_area()
# 默认情况下图例的堆积顺序与面积图的堆积顺序是相反的,通过设定标度中的切分(breaks)参数可以翻转堆积顺序。下图对图例的堆积顺序进行了反转,将调色板设定为蓝色渐变色,并在各个区域之间添加细线,将填充色设定为半透明的
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) + geom_area(colour = "black", 
    size = 0.2, alpha = 0.4) + scale_fill_brewer(palette = "Blues", breaks = rev(levels(uspopage$AgeGroup)))
# 在aes()函数中设定order=desc(AgeGroup)可以对堆积面积图的堆积顺序进行反转
library(plyr)  #为了使用desc函数
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup, order = desc(AgeGroup))) + 
    geom_area(colour = "black", size = 0.2, alpha = 0.4) + scale_fill_brewer(palette = "Blues")
# 绘制没有左右两侧的垂直线的堆积面积图
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup, order = desc(AgeGroup))) + 
    geom_area(colour = NA, alpha = 0.4) + scale_fill_brewer(palette = "Blues") + 
    geom_line(position = "stack", size = 0.2)
## ymax not defined: adjusting position using y instead

4.8 绘制百分比堆积面积图

运行geom_area()函数,并映射一个因子变量给填充色fill即可
library(gcookbook)  #为了使用数据集uspopage
library(plyr)  #为了使用ddply函数
head(uspopage)
##   Year AgeGroup Thousands
## 1 1900       <5      9181
## 2 1900     5-14     16966
## 3 1900    15-24     14951
## 4 1900    25-34     12161
## 5 1900    35-44      9273
## 6 1900    45-54      6437
uspopage_prob <- ddply(uspopage, "Year", transform, Percent = Thousands/sum(Thousands) * 
    100)
head(uspopage_prob)
##   Year AgeGroup Thousands   Percent
## 1 1900       <5      9181 12.065340
## 2 1900     5-14     16966 22.296107
## 3 1900    15-24     14951 19.648067
## 4 1900    25-34     12161 15.981549
## 5 1900    35-44      9273 12.186243
## 6 1900    45-54      6437  8.459274
ggplot(uspopage_prob, aes(x = Year, y = Percent, fill = AgeGroup, order = desc(AgeGroup))) + 
    geom_area(colour = NA, alpha = 0.4) + scale_fill_brewer(palette = "Blues") + 
    geom_line(position = "stack", size = 0.2)
## ymax not defined: adjusting position using y instead

4.9 添加置信域

运行geom_ribbon()函数,然后分别映射一个变量给ymin和ymax
library(gcookbook)  #为了使用数据集
# 选取climate数据集的一个子集
clim <- subset(climate, Source == "Berkeley", select = c("Year", "Anomaly10y", 
    "Unc10y"))
head(clim)
##   Year Anomaly10y Unc10y
## 1 1800     -0.435  0.505
## 2 1801     -0.453  0.493
## 3 1802     -0.460  0.486
## 4 1803     -0.493  0.489
## 5 1804     -0.536  0.483
## 6 1805     -0.541  0.475
# climate数据集中的Anomaly10y变量表示各年温度相对于1950-1980平均水平变异的10年移动平均,变量Unc10y表示其95%置信水平下的置信区间,令ymax和ymin分别设定为Anomaly10y加减Unc10y
ggplot(clim, aes(x = Year, y = Anomaly10y)) + geom_ribbon(aes(ymin = Anomaly10y - 
    Unc10y, ymax = Anomaly10y + Unc10y), alpha = 0.2) + geom_line()