第一节 折线图简单绘制
#图1
library(ggplot2)
ggplot(BOD,aes(x = Time,y = demand))+geom_line()
#图2
#将time转换为因子变量,注意必须使用group=1
BOD1 <- BOD
BOD1$Time <- factor(BOD1$Time)
ggplot(BOD,aes(x = Time,y = demand,group=1))+geom_line()
#图3
#将纵坐标调节至0为起点
ggplot(BOD,aes(x = Time,y = demand))+geom_line()+
ylim(0,max(BOD1$demand))
#或图4
ggplot(BOD,aes(x = Time,y = demand))+geom_line()+
expand_limits(y=0)
图2中,将time转换为因子变量,注意必须使用group=1

图3-4 分别使用ylim(), expand_limits(y=0)延长纵坐标至0

第二节 向折线图添加数据标记
当纵坐标呈指数变化时,我们可以对其取对数,更方便我们观察数据的变化趋势。
library(gcookbook)
#图1
ggplot(worldpop,aes(x = Year,y = Population))+geom_point()+geom_line()
#图2,对纵坐标取对数
ggplot(worldpop,aes(x = Year,y = Population))+geom_point()+geom_line()+
scale_y_log10()


第三节 绘制多重折线
首先,生成我们需要的数据集,使用的是plyr包中的ddplyr函数
,这个函数功能强大,感兴趣可以通过其帮助文档大致“摸一下这只象”,以后会经常遇到它。
library(plyr)
tg <- ddply(ToothGrowth,c("supp","dose"),summarise,length=mean(len))

开始绘制多条折线图,可以将分组信息赋予线的颜色,线条的类型等
#将supp映射至颜色
ggplot(tg,aes(x = dose,y = length,colour=supp)) +
geom_line()
#将supp映射至线的类型
ggplot(tg,aes(x = dose,y = length,linetype=supp)) +
geom_line()


将分组信息赋予点的颜色和形状
#可以将分组映射至fill或shape上
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)


将重合的点错开,使用position_dodge()
#将重合的点错开
ggplot(tg,aes(x = dose,y = length,shape=supp)) +
geom_line(position = position_dodge(0.2))+
geom_point(position = position_dodge(0.2),size=4)

第四节 修改线条样式
library(ggplot2)
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))

ggplot(tg,aes(x = dose,y = length,colour=supp))+
geom_line()+
scale_color_brewer(palette = "Set1")

第五节 修改数据标记样式
ggplot(BOD,aes(x = Time,y = demand))+
geom_line()+
geom_point(size=4, alpha=.5, shape=22, colour="darkred", fill="pink")

library(plyr)
tg <- ddply(ToothGrowth,c("supp","dose"),summarise,length=mean(len))
pd <- position_dodge(0.2)
ggplot(tg,aes(x = dose,y = length,fill=supp))+
geom_line(position = pd)+
geom_point(shape=21,size=4,position = pd)+
scale_color_manual(values = c("black","white"))

第六节 绘制面积图
面积图的绘制靠geom_area()
sunspotyear <- data.frame(
year=as.numeric(time(sunspot.year)),
sunspots=as.numeric(sunspot.year)
)
ggplot(sunspotyear,aes(x = year,y = sunspots))+
geom_area()

ggplot(sunspotyear,aes(x = year,y = sunspots))+
geom_area(colour="black",fill="blue",alpha=.2)

第七节 绘制堆叠面积图
library(gcookbook)
ggplot(uspopage,aes(x = Year,y = Thousands,fill=AgeGroup))+
geom_area()

第八节 绘制百分比堆积面积图
关键在于得到百分比数据,绘图等同于堆叠面积图
library(gcookbook)
library(plyr)
uspopage_prop <- ddply(uspopage, "Year", transform, percent = Thousands / sum (Thousands) * 100)
ggplot(uspopage_prop,aes(x = Year,y = percent,fill=AgeGroup))+
geom_area(colour="black",size=2,alpha=.4)+
scale_color_brewer(palette = "Blues", breaks = rev(levels(uspopage$AgeGroup)))

geom_ribbon
添加置信区间
##添加置信区间
library(gcookbook)
clim <- subset(climate,Source=="Berkeley",
select = c("Year","Anomaly10y","Unc10y"))
ggplot(clim,aes(x = Year,y = Anomaly10y))+
geom_ribbon(aes(ymin=Anomaly10y-Unc10y,ymax=Anomaly10y+Unc10y),alpha=0.2)+
geom_line()

geom_line
添加置信区间
#或者直接添加线作为置信区间
ggplot(clim,aes(x = Year,y = Anomaly10y))+
geom_line(aes(y=Anomaly10y-Unc10y),colour="grey30",linetype="dotted")+
geom_line(aes(y=Anomaly10y+Unc10y),colour="grey30",linetype="dotted")+
geom_line()

网友评论