1、兩個變量:x,y皆連續(xù)
使用數(shù)據(jù)集mtcars
mtcars數(shù)據(jù)集包含從1974年《美國汽車趨勢》雜志中提取的數(shù)據(jù),該數(shù)據(jù)描述了32輛汽車(1973–74年型號)的油耗以及汽車設計和性能的10個其他屬性。

#先創(chuàng)建一個ggplot圖層
library(ggplot2)
b <- ggplot(data = mtcars, aes(x=wt, y=mpg))
#DatistEQ回收數(shù)據(jù),后續(xù)代碼省略此行
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
可能添加的圖層有:
- geom_point():散點圖
- geom_smooth():平滑線
- geom_quantile():分位線
- geom_rug():邊際地毯線
- geom_jitter():避免重疊
- geom_text():添加文本注釋

1.1 散點圖
b+geom_point()

將變量cyl映射給點的顏色和形狀
b + geom_point(aes(color = factor(cyl), shape = factor(cyl)))

自定義顏色
b+geom_point(aes(color=factor(cyl), shape=factor(cyl)))+
scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+theme_classic()

1.2 平滑線
可以添加回歸曲線
b+geom_smooth()

散點圖+回歸線
b+geom_point()+
geom_smooth(method = "lm", se=FALSE)#去掉置信區(qū)間

使用loess方法
b+geom_point()+
geom_smooth(method = "loess")

將變量映射給顏色和形狀
b+geom_point(aes(color=factor(cyl), shape=factor(cyl)))+
geom_smooth(aes(color=factor(cyl), shape=factor(cyl)), method = "lm", se=FALSE, fullrange=TRUE)

1.3 分位線
# Package `quantreg` required for `stat_quantile`.
if (!require("quantreg")) install.packages("quantreg")
ggplot(data = mpg, aes(cty, hwy))+
geom_point()+geom_quantile()+
theme_minimal()

1.4 邊際地毯線
使用數(shù)據(jù)集faithful
ggplot(data = faithful, aes(x=eruptions, y=waiting))+
geom_point()+geom_rug()

避免重疊
實際上geom_jitter()是geom_point(position="jitter")的簡稱,下面使用數(shù)據(jù)集mpg
p <- ggplot(data = mpg, aes(displ, hwy))
p+geom_point()

1.5 增加抖動防止重疊
p+geom_jitter(width = 0.5, height = 0.5)

其中兩個參數(shù):
width:x軸方向的抖動幅度
height:y軸方向的抖動幅度
1.6 文本注釋
參數(shù)label用來指定注釋標簽 (ggrepel可以避免標簽重疊)
b+geom_text(aes(label=rownames(mtcars)))

完整代碼如下:
header1("兩個變量:x,y皆連續(xù)")
header2("散點圖")
library(ggplot2)
b <- ggplot(data = mtcars, aes(x=wt, y=mpg))
b+geom_point()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#將變量cyl映射給點的顏色和形狀
b + geom_point(aes(color = factor(cyl), shape = factor(cyl)))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#自定義顏色
b+geom_point(aes(color=factor(cyl), shape=factor(cyl)))+
scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))+theme_classic()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
header2("平滑線")
b+geom_smooth()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#散點圖+回歸線
b+geom_point()+
geom_smooth(method = "lm", se=FALSE)#去掉置信區(qū)間
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#使用loess方法
b+geom_point()+
geom_smooth(method = "loess")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#將變量映射給顏色和形狀
b+geom_point(aes(color=factor(cyl), shape=factor(cyl)))+
geom_smooth(aes(color=factor(cyl), shape=factor(cyl)), method = "lm", se=FALSE, fullrange=TRUE)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#分位線
# Package `quantreg` required for `stat_quantile`.
if (!require("quantreg")) install.packages("quantreg")
ggplot(data = mpg, aes(cty, hwy))+ geom_point() + geom_quantile() + theme_minimal()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
ggplot(mtcars, aes(hp, disp)) +
geom_point() +
geom_quantile(quantiles = 0.5, aes(group = factor(gear), colour = factor(gear)),
xseq = min(mtcars$hp):max(mtcars$hp))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#邊際地毯線
#使用數(shù)據(jù)集faithful
ggplot(data = faithful, aes(x=eruptions, y=waiting))+
geom_point()+geom_rug()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#避免重疊
#實際上geom_jitter()是geom_point(position="jitter")的簡稱,下面使用數(shù)據(jù)集mpg
p <- ggplot(data = mpg, aes(displ, hwy))
p+geom_point()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#增加抖動防止重疊
p+geom_jitter(width = 0.5, height = 0.5)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#文本注釋
#參數(shù)label用來指定注釋標簽 (ggrepel可以避免標簽重疊)
b <- ggplot(data = mtcars, aes(x=wt, y=mpg))
b+geom_text(aes(label=rownames(mtcars)))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
2、兩個變量:連續(xù)二元分布
使用數(shù)據(jù)集diamonds,該數(shù)據(jù)集收集了約54000顆鉆石的價格和質量的信息。每條記錄由十個變量構成,其中有三個是名義變量,分別描述鉆石的切工,顏色和凈度;
注:數(shù)據(jù)集diamonds,源于ggplot2包(因為鉆石的價格定價取決于重量,顏色,刀工等影響,價格該如何制定合理,為公司搶占市場制定價格提供依據(jù)。)

carat:克拉重量
cut:切工
color:顏色
clarity:凈度
depth:深度
table:鉆石寬度
以及X,Y,Z

創(chuàng)建ggplot圖層,后面再逐步添加圖層
c <- ggplot(data=diamonds, aes(carat, price))
可添加的圖層有:
geom_bin2d(): 二維封箱熱圖
geom_hex(): 六邊形封箱圖
geom_density_2d(): 二維等高線密度圖

2.1 二維封箱熱圖
geom_bin2d()將點的數(shù)量用矩形封裝起來,通過顏色深淺來反映點密度
c+geom_bin2d()

設置bin的數(shù)量
c+geom_bin2d(bins=150)

2.2 六邊形封箱圖
geom_hex()依賴于另一個R包hexbin,所以沒安裝的先安裝:
if (!require("hexbin")) install.packages("hexbin")
library(hexbin)
c+geom_hex()

修改bin的數(shù)目
c+geom_hex(bins=10)

2.3 二維等高線密度圖
sp <- ggplot(faithful, aes(x=eruptions, y=waiting))
sp+geom_point()+ geom_density_2d()

完整代碼如下:
library(ggplot2)
#diamonds有54000顆鉆石的數(shù)據(jù),太大只顯示頭部的幾行記錄
output(head(diamonds))
#二維封箱熱圖
header1("二維封箱熱圖")
c <- ggplot(data=diamonds, aes(carat, price))
c+geom_bin2d()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#設置bin的數(shù)量
c+geom_bin2d(bins=150)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#六邊形封箱圖
header1("六邊形封箱圖")
if (!require("hexbin")) install.packages("hexbin")
library(hexbin)
c+geom_hex()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#修改bin的數(shù)目
c+geom_hex(bins=10)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
header1("二維等高線密度圖")
sp <- ggplot(faithful, aes(x=eruptions, y=waiting))
sp+geom_point()+ geom_density_2d()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
3、兩個變量:連續(xù)函數(shù)
主要是如何通過線來連接兩個變量,使用數(shù)據(jù)集economics。
head(economics)

先創(chuàng)建一個ggplot圖層,后面逐步添加圖層
d <- ggplot(data = economics, aes(x=date, y=unemploy))
可添加的圖層有:
geom_area():面積圖
geom_line():折線圖
geom_step(): 階梯圖
3.1 面積圖
d+geom_area()

3.2 線圖
d+geom_line()

3.3 階梯圖
set.seed(1111)
ss <- economics[sample(1:nrow(economics), 20),]
ggplot(ss, aes(x=date, y=unemploy))+geom_step()

完整代碼如下:
library(ggplot2)
output(head(economics))
#面積圖
header1("面積圖")
d <- ggplot(data = economics, aes(x=date, y=unemploy))
d+geom_area()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
header1("線圖")
d+geom_line()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
header1("階梯圖")
set.seed(1111)
ss <- economics[sample(1:nrow(economics), 20),]
ggplot(ss, aes(x=date, y=unemploy))+ geom_step()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
4、 兩個變量:x離散,y連續(xù)
使用數(shù)據(jù)集ToothGrowth,其中的變量len(Tooth length)是連續(xù)變量,dose是離散變量。
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
創(chuàng)建圖層
e <- ggplot(data = ToothGrowth, aes(x=dose, y=len))
可添加的圖層有:
geom_boxplot(): 箱線圖
geom_violin():小提琴圖
geom_dotplot():點圖
geom_jitter(): 帶狀圖
geom_line(): 線圖
geom_bar(): 條形圖

4.1 箱線圖
e+geom_boxplot()

添加有缺口的箱線圖
e+geom_boxplot(notch = TRUE)

將dose映射給填充顏色
e+geom_boxplot(aes(fill=dose))

按supp進行分類并映射給填充顏色
ggplot(ToothGrowth, aes(x=dose, y=len))+ geom_boxplot(aes(fill=supp))

4.2 小提琴圖
e+geom_violin(trim = FALSE)

添加中值點
e+geom_violin(trim = FALSE)+
stat_summary(fun.data = mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="red")

與箱線圖結合
e+geom_violin(trim = FALSE)+
geom_boxplot(width=0.2)

將dose映射給顏色進行分組
e+geom_violin(aes(color=dose), trim = FALSE)

4.3 點圖
e+geom_dotplot(binaxis = "y", stackdir = "center")

添加中值點
e + geom_dotplot(binaxis = "y", stackdir = "center") +
stat_summary(fun.data=mean_sdl, color = "red",geom = "pointrange",fun.args=list(mult=1))

與箱線圖結合
e + geom_boxplot() +
geom_dotplot(binaxis = "y", stackdir = "center")

添加小提琴圖
e + geom_violin(trim = FALSE) +
geom_dotplot(binaxis='y', stackdir='center')

將dose映射給顏色以及填充色
e + geom_dotplot(aes(color = dose, fill = dose),
binaxis = "y", stackdir = "center")

4.5 帶狀圖
帶狀圖是一種一維散點圖,當樣本量很小時,與箱線圖相當
e + geom_jitter(position=position_jitter(0.2))

添加中值點
e + geom_jitter(position=position_jitter(0.2)) +
stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="pointrange", color = "red")

與點圖結合
e + geom_jitter(position=position_jitter(0.2)) +
geom_dotplot(binaxis = "y", stackdir = "center")

與小提琴圖結合
e + geom_violin(trim = FALSE) +
geom_jitter(position=position_jitter(0.2))

將dose映射給顏色和形狀
e + geom_jitter(aes(color = dose, shape = dose),
position=position_jitter(0.2))

4.6 線圖
#構造數(shù)據(jù)集
df <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
head(df)

將supp映射線型
ggplot(df, aes(x=dose, y=len, group=supp)) +
geom_line(aes(linetype=supp))+
geom_point()

修改線型、點的形狀以及顏色
ggplot(df, aes(x=dose, y=len, group=supp)) +
geom_line(aes(linetype=supp, color = supp))+
geom_point(aes(shape=supp, color = supp))

4.7 條形圖
#構造數(shù)據(jù)集
df <- data.frame(dose=c("D0.5", "D1", "D2"),
len=c(4.2, 10, 29.5))
head(df)
## dose len
## 1 D0.5 4.2
## 2 D1 10.0
## 3 D2 29.5
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
head(df2)
## supp dose len
## 1 VC D0.5 6.8
## 2 VC D1 15.0
## 3 VC D2 33.0
## 4 OJ D0.5 4.2
## 5 OJ D1 10.0
## 6 OJ D2 29.5
創(chuàng)建圖層
f <- ggplot(df, aes(x = dose, y = len))
f + geom_bar(stat = "identity")

修改填充色以及添加標簽
f + geom_bar(stat="identity", fill="steelblue")+
geom_text(aes(label=len), vjust=-0.3, size=3.5)+
theme_minimal()

將dose映射給條形圖顏色
f + geom_bar(aes(color = dose),
stat="identity", fill="white")

修改填充色
f + geom_bar(aes(fill = dose), stat="identity")

將變量supp映射給填充色,從而達到分組效果
g <- ggplot(data=df2, aes(x=dose, y=len, fill=supp))
g + geom_bar(stat = "identity")#position默認為stack

修改position為dodge
g + geom_bar(stat="identity", position=position_dodge())

完整代碼如下:
library(ggplot2)
#output(head(ToothGrowth))
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
output(head(ToothGrowth))
#箱線圖
header1("箱線圖")
e <- ggplot(data = ToothGrowth, aes(x=dose, y=len))
e+geom_boxplot()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#添加有缺口的箱線圖
e+geom_boxplot(notch = TRUE)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#將dose映射給填充顏色
e+geom_boxplot(aes(fill=dose))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#按supp進行分類并映射給填充顏色
ggplot(ToothGrowth, aes(x=dose, y=len))+ geom_boxplot(aes(fill=supp))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#小提琴圖
header1("小提琴圖")
e+geom_violin(trim = FALSE)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#添加中值點
if (!require("Hmisc")) install.packages("Hmisc")
e+geom_violin(trim = FALSE)+
stat_summary(fun.data = mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="red")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#與箱線圖結合
e+geom_violin(trim = FALSE)+geom_boxplot(width=0.2)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#將dose映射給顏色進行分組
e+geom_violin(aes(color=dose), trim = FALSE)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#點圖
header1("點圖")
e+geom_dotplot(binaxis = "y", stackdir = "center")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#添加中值點
e + geom_dotplot(binaxis = "y", stackdir = "center") +
stat_summary(fun.data=mean_sdl, color = "red",geom = "pointrange",fun.args=list(mult=1))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#與箱線圖結合
e + geom_boxplot() +
geom_dotplot(binaxis = "y", stackdir = "center")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#添加小提琴圖
e + geom_violin(trim = FALSE) +
geom_dotplot(binaxis='y', stackdir='center')
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#將dose映射給顏色以及填充色
e + geom_dotplot(aes(color = dose, fill = dose),
binaxis = "y", stackdir = "center")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#帶狀圖
header1("帶狀圖")
e + geom_jitter(position=position_jitter(0.2))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#添加中值點
e + geom_jitter(position=position_jitter(0.2)) +
stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="pointrange", color = "red")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#與點圖結合
e + geom_jitter(position=position_jitter(0.2)) +
geom_dotplot(binaxis = "y", stackdir = "center")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#與小提琴圖結合
e + geom_violin(trim = FALSE) +
geom_jitter(position=position_jitter(0.2))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#將dose映射給顏色和形狀
e + geom_jitter(aes(color = dose, shape = dose),
position=position_jitter(0.2))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#線圖
header1("線圖")
df <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
output( head(df))
ggplot(df, aes(x=dose, y=len, group=supp)) +
geom_line(aes(linetype=supp))+
geom_point()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#修改線型、點的形狀以及顏色
ggplot(df, aes(x=dose, y=len, group=supp)) +
geom_line(aes(linetype=supp, color = supp))+
geom_point(aes(shape=supp, color = supp))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
header1("條形圖")
df <- data.frame(dose=c("D0.5", "D1", "D2"),
len=c(4.2, 10, 29.5))
output(head(df))
df2 <- data.frame(supp=rep(c("VC", "OJ"), each=3),
dose=rep(c("D0.5", "D1", "D2"),2),
len=c(6.8, 15, 33, 4.2, 10, 29.5))
output(head(df2))
f <- ggplot(df, aes(x = dose, y = len))
f + geom_bar(stat = "identity")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#修改填充色以及添加標簽
f + geom_bar(stat="identity", fill="steelblue")+
geom_text(aes(label=len), vjust=-0.3, size=3.5)+
theme_minimal()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#將dose映射給條形圖顏色
f + geom_bar(aes(color = dose),
stat="identity", fill="white")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#修改填充色
f + geom_bar(aes(fill = dose), stat="identity")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#將變量supp映射給填充色,從而達到分組效果
g <- ggplot(data=df2, aes(x=dose, y=len, fill=supp))
g + geom_bar(stat = "identity")#position默認為stack
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#修改position為dodge
g + geom_bar(stat="identity", position=position_dodge())
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
5、 兩個變量:x、y皆離散
使用數(shù)據(jù)集diamonds中的兩個離散變量color以及cut
ggplot(diamonds, aes(cut, color)) +
geom_jitter(aes(color = cut), size = 0.5)

6、 兩個變量:繪制誤差圖
df <- ToothGrowth
df$dose <- as.factor(df$dose)
head(df)
## len supp dose
## 1 4.2 VC 0.5
## 2 11.5 VC 0.5
## 3 7.3 VC 0.5
## 4 5.8 VC 0.5
## 5 6.4 VC 0.5
## 6 10.0 VC 0.5
繪制誤差圖需要知道均值以及標準誤,下面這個函數(shù)用來計算每組的均值以及標準誤。
data_summary <- function(data, varname, grps){
require(plyr)
summary_func <- function(x, col){
c(mean = mean(x[[col]], na.rm=TRUE),
sd = sd(x[[col]], na.rm=TRUE))
}
data_sum<-ddply(data, grps, .fun=summary_func, varname)
data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
計算均值以及標準誤
df2 <- data_summary(df, varname="len", grps= "dose")
# Convert dose to a factor variable
df2$dose=as.factor(df2$dose)
head(df2)
## dose len sd
## 1 0.5 10.605 4.499763
## 2 1 19.735 4.415436
## 3 2 26.100 3.774150
創(chuàng)建圖層
f <- ggplot(df2, aes(x = dose, y = len,
ymin = len-sd, ymax = len+sd))
可添加的圖層有:
geom_crossbar(): 空心柱,上中下三線分別代表ymax、mean、ymin
geom_errorbar(): 誤差棒
geom_errorbarh(): 水平誤差棒
geom_linerange():豎直誤差線
geom_pointrange():中間為一點的誤差線

6.1 空心柱
geom_crossbar()
f+geom_crossbar()

將dose映射給顏色
f+geom_crossbar(aes(color=dose))

自定義顏色
f+geom_crossbar(aes(color=dose))+
scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9"))+theme_classic()

修改填充色
f+geom_crossbar(aes(fill=dose))+
scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9"))+
theme_classic()

通過將supp映射給顏色實現(xiàn)分組,可以利用函數(shù)stat_summary()來計算mean和sd
f <- ggplot(df, aes(x=dose, y=len, color=supp))
f+stat_summary(fun.data = mean_sdl, fun.args = list(mult=1), geom="crossbar", width=0.6, position = position_dodge(0.8))

6.2 誤差棒
f <- ggplot(df2, aes(x=dose, y=len, ymin=len-sd, ymax=len+sd))
將dose映射給顏色
f+geom_errorbar(aes(color=dose), width=0.2)

與線圖結合
f+geom_line(aes(group=1))+
geom_errorbar(width=0.15)

與條形圖結合,并將變量dose映射給顏色
f+geom_bar(aes(color=dose), stat = "identity", fill="white")+
geom_errorbar(aes(color=dose), width=0.1)

6.3 水平誤差棒
#構造數(shù)據(jù)集
df2 <- data_summary(ToothGrowth, varname="len", grps = "dose")
df2$dose <- as.factor(df2$dose)
head(df2)
## dose len sd
## 1 0.5 10.605 4.499763
## 2 1 19.735 4.415436
## 3 2 26.100 3.774150
創(chuàng)建圖層
f <- ggplot(data = df2, aes(x=len, y=dose,xmin=len-sd, xmax=len+sd))
參數(shù)xmin與xmax用來設置水平誤差棒
f+geom_errorbarh()

通過映射實現(xiàn)分組
f+geom_errorbarh(aes(color=dose))

6.4 豎直誤差線
geom_linerange()與geom_pointrange()
f <- ggplot(df2, aes(x=dose, y=len, ymin=len-sd, ymax=len+sd))
line range
f+geom_linerange()

6.5 中間為一點的誤差線
point range
f+geom_pointrange()

6.6 點圖+誤差棒
g <- ggplot(df, aes(x=dose, y=len))+
geom_dotplot(binaxis = "y", stackdir = "center")
添加geom_crossbar()
g+stat_summary(fun.data = mean_sdl, fun.args = list(mult=1), geom="crossbar", color="red", width=0.1)

添加geom_errorbar()
g + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="errorbar", color="red", width=0.2) +
stat_summary(fun.y=mean, geom="point", color="red")

添加geom_pointrange()
g + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="red")

完整代碼如下:
library(ggplot2)
df <- ToothGrowth
df$dose <- as.factor(df$dose)
head(df)
data_summary <- function(data, varname, grps){
require(plyr)
summary_func <- function(x, col){
c(mean = mean(x[[col]], na.rm=TRUE),
sd = sd(x[[col]], na.rm=TRUE))
}
data_sum<-ddply(data, grps, .fun=summary_func, varname)
data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
df2 <- data_summary(df, varname="len", grps= "dose")
# Convert dose to a factor variable
df2$dose=as.factor(df2$dose)
head(df2)
f <- ggplot(df2, aes(x = dose, y = len,
ymin = len-sd, ymax = len+sd))
f+geom_crossbar()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#將dose映射給顏色
f+geom_crossbar(aes(color=dose))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#自定義顏色
f+geom_crossbar(aes(color=dose))+
scale_color_manual(values = c("#999999", "#E69F00", "#56B4E9"))+theme_classic()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#修改填充色
f+geom_crossbar(aes(fill=dose))+
scale_fill_manual(values = c("#999999", "#E69F00", "#56B4E9"))+
theme_classic()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
f <- ggplot(df, aes(x=dose, y=len, color=supp))
f+stat_summary(fun.data = mean_sdl, fun.args = list(mult=1), geom="crossbar", width=0.6, position = position_dodge(0.8))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
header1("誤差棒")
f <- ggplot(df2, aes(x=dose, y=len, ymin=len-sd, ymax=len+sd))
#將dose映射給顏色
f+geom_errorbar(aes(color=dose), width=0.2)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#與線圖結合
f+geom_line(aes(group=1))+
geom_errorbar(width=0.15)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
f+geom_bar(aes(color=dose), stat = "identity", fill="white")+
geom_errorbar(aes(color=dose), width=0.1)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
header1("水平誤差棒")
df2 <- data_summary(ToothGrowth, varname="len", grps = "dose")
df2$dose <- as.factor(df2$dose)
output(head(df2))
#創(chuàng)建圖層
f <- ggplot(data = df2, aes(x=len, y=dose,xmin=len-sd, xmax=len+sd))
f+geom_errorbarh()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
f+geom_errorbarh(aes(color=dose))
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#line range
f <- ggplot(df2, aes(x=dose, y=len, ymin=len-sd, ymax=len+sd))
f+geom_linerange()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#point range
f+geom_pointrange()
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#點圖+誤差棒
g <- ggplot(df, aes(x=dose, y=len))+
geom_dotplot(binaxis = "y", stackdir = "center")
#添加geom_crossbar()
g+stat_summary(fun.data = mean_sdl, fun.args = list(mult=1), geom="crossbar", color="red", width=0.1)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#添加geom_errorbar()
g + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="errorbar", color="red", width=0.2) +
stat_summary(fun.y=mean, geom="point", color="red")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
#添加geom_pointrange()
g + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="red")
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")
7、 兩個變量:地圖繪制
ggplot2提供了繪制地圖的函數(shù)geom_map(),依賴于包maps提供地理信息。
安裝map
if (!require("maps")) install.packages("maps")
下面將繪制美國地圖,數(shù)據(jù)集采用USArrests
library(maps)
head(USArrests)
## Murder Assault UrbanPop Rape
## Alabama 13.2 236 58 21.2
## Alaska 10.0 263 48 44.5
## Arizona 8.1 294 80 31.0
## Arkansas 8.8 190 50 19.5
## California 9.0 276 91 40.6
## Colorado 7.9 204 78 38.7
對數(shù)據(jù)進行整理一下,添加一列state
crimes <- data.frame(state=tolower(rownames(USArrests)), USArrests)
head(crimes)
## Murder Assault UrbanPop Rape
## Alabama 13.2 236 58 21.2
## Alaska 10.0 263 48 44.5
## Arizona 8.1 294 80 31.0
## Arkansas 8.8 190 50 19.5
## California 9.0 276 91 40.6
## Colorado 7.9 204 78 38.7
#數(shù)據(jù)重鑄
library(reshape2)
crimesm <- melt(crimes, id=1)
head(crimesm)
## state variable value
## 1 alabama Murder 13.2
## 2 alaska Murder 10.0
## 3 arizona Murder 8.1
## 4 arkansas Murder 8.8
## 5 california Murder 9.0
## 6 colorado Murder 7.9
map_data <- map_data("state")
#繪制地圖,使用Murder進行著色
ggplot(crimes, aes(map_id=state))+
geom_map(aes(fill=Murder), map=map_data)+
expand_limits(x=map_data$long, y=map_data$lat)

完整代碼如下:
library(ggplot2)
if (!require("maps")) install.packages("maps")
library(maps)
output(head(USArrests))
crimes <- data.frame(state=tolower(rownames(USArrests)), USArrests)
output(head(crimes))
if (!require("reshape2")) install.packages("reshape2")
library(reshape2)
crimesm <- melt(crimes, id=1)
output(head(crimesm))
map_data <- map_data("state")
ggplot(crimes, aes(map_id=state))+
geom_map(aes(fill=Murder), map=map_data)+
expand_limits(x=map_data$long, y=map_data$lat)
ggsave(gettempfile(), width = 15, height = 10, dpi=300,units = "cm")