1.Dot plots
1.1 语法
geom_dotplot( mapping = NULL, data = NULL, position = "identity", ..., binwidth = NULL, binaxis = "x", method = "dotdensity", binpositions = "bygroup", stackdir = "up", stackratio = 1, dotsize = 1, stackgroups = FALSE, origin = NULL, right = TRUE, width = 0.9, drop = FALSE, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
# 将数值型变量转换为因子
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
library(ggplot2)
# 基础点图
p1 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center')
# 更改点尺寸和堆叠率
p2 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center',
stackratio=1.5, dotsize=1.2)
# 旋转
p3 <- p1 + coord_flip()
# 选择展示数据
p4 <- p1 + scale_x_discrete(limits=c("0.5", "2"))
ggarrange(p1,p2,p3,p4,nrow = 1)
1.2 在点图上添加摘要统计信息 stat_summary()
# 在点图上添加摘要统计信息 stat_summary()
# 添加平均值和中位数
p5 <- p1 + stat_summary(fun.y=mean, geom="point", shape=18,
size=3, color="red")
p6<- p1 + stat_summary(fun.y=median, geom="point", shape=18,
size=3, color="red")
1.3 点状图与箱形图和小提琴图
# 点状图与箱形图和小提琴图
# 添加基础箱线图
p7 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()+
geom_dotplot(binaxis='y', stackdir='center')
# 添加缺刻箱线图
p8 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot(notch = TRUE)+
geom_dotplot(binaxis='y', stackdir='center')
# 添加小提琴图
p9 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin(trim = FALSE)+
geom_dotplot(binaxis='y', stackdir='center')
ggarrange(p7,p8,p9,nrow = 1)
1.4 添加平均值和标准偏差
# 添加平均值和标准偏差
p10 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center')
p11 <- p10 + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="crossbar", width=0.5)
p12 <- p10 + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1),
geom="pointrange", color="red")
# 产生汇总统计的函数 (mean and +/- sd)
data_summary <- function(x) {
m <- mean(x)
ymin <- m-sd(x)
ymax <- m+sd(x)
return(c(y=m,ymin=ymin,ymax=ymax))
}
p13 <- p10 + stat_summary(fun.data=data_summary, color="blue")
ggarrange(p10,p11,p12,p13,nrow = 1)
1.5 更改填充色
# 按组更改点图颜色
# 单色填充
p14 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_dotplot(binaxis='y', stackdir='center', fill="#FFAAD4")
# 按组填充
p15 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_dotplot(binaxis='y', stackdir='center')
ggarrange(p14,p15)
# 自定义调色板
p16 <- p15 + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# brewer调色板
p17 <- p15 + scale_fill_brewer(palette="Dark2")
# 灰色
p18 <- p15 + scale_fill_grey() + theme_classic()
ggarrange(p16,p17,p18,nrow = 1)
1.6 更改图例位置
# 更改图例位置
p19 <- p15 + theme(legend.position="top")
p20 <- p15 + theme(legend.position="bottom")
p21 <- p15 + theme(legend.position="none") # Remove legend
ggarrange(p19,p20,p21,nrow = 1)
1.7 更改图例中的项目顺序
# 更改图例中的项目顺序
p22 <- p15 + scale_x_discrete(limits=c("2", "0.5", "1"))
p22
ggarrange(p15,p22)
1.8 多组点图
# 多组点图
# 按组更改颜色
p23 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_dotplot(binaxis='y', stackdir='center')
p24 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.8))
ggarrange(p23,p24)
# 更改颜色
p25 <-p24 + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# 添加箱线图
p26 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_boxplot(fill="white")+
geom_dotplot(binaxis='y', stackdir='center')
# 改变图例
p27 <- ggplot(ToothGrowth, aes(x=dose, y=len, fill=supp)) +
geom_boxplot(position=position_dodge(0.8))+
geom_dotplot(binaxis='y', stackdir='center',
position=position_dodge(0.8))
ggarrange(p25,p26,p27,nrow = 1)
1.9 自定义点图
# 定制点图
# 基础点图
dp1 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()+
geom_dotplot(binaxis='y', stackdir='center')+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")+
theme_classic()
# 按组修改颜色
dp2 <-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
geom_dotplot(binaxis='y', stackdir='center')+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")
dp3 <- dp2 + theme_classic()
# 连续性颜色
dp4 <- dp2 + scale_fill_brewer(palette="Blues") + theme_classic()
# 离散型颜色
dp5 <- dp2 + scale_fill_brewer(palette="Dark2") + theme_minimal()
# 渐变色
dp6 <- dp2 + scale_fill_brewer(palette="RdBu") + theme_minimal()
ggarrange(dp1,dp2,dp3,nrow = 1)
ggarrange(dp4,dp5,dp6,nrow = 1)
2. Scatter plots
2.1 语法
geom_point( mapping = NULL, data = NULL, stat = "identity", position = "identity", ..., na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
2.2 基础散点图
# 将数值型变量转换为因子
mtcars$cyl <- as.factor(mtcars$cyl)
head(mtcars)
library(ggplot2)
# 基础散点图
p1 <- ggplot(mtcars, aes(x=wt, y=mpg)) + geom_point()
# 点的大小及形状
p2 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(size=2, shape=23)
p3 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(aes(size=qsec))
ggarrange(p1,p2,p3,nrow = 1)
2.3添加文本geom_text()
# 在散点图中标记点 geom_text()
p4 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() +
geom_text(label=rownames(mtcars))
p4
2.4 添加回归线geom_smooth(), stat_smooth(),geom_abline()
# 添加回归线geom_smooth(), stat_smooth(),geom_abline()
# 添加回归线
p5 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth(method=lm)
# 删除置信区间
p6 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth(method=lm, se=FALSE)
# Loess method
p7 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth()
ggarrange(p5,pp6,p7,nrow = 1)
2.5 更改点和线的外观
# 更改点和线的外观
# 点的颜色及形状
# 线型及颜色
p8 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(shape=18, color="blue")+
geom_smooth(method=lm, se=FALSE, linetype="dashed",
color="darkred")
# 更改置信区间填充颜色
p9 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point(shape=18, color="blue")+
geom_smooth(method=lm, linetype="dashed",
color="darkred", fill="blue")
ggarrange(p8,p9)
2.6 多组散点图
# 自动更改点的颜色/形状/大小
# --------------------------------------------------------
# 通过cyl因子水平修改点的形状
# mtcars$cyl <- as.factor(mtcars$cyl)
p10 <- ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl)) +
geom_point()
p10
# 修改点的形状及颜色
p11 <- ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl, color=cyl)) +
geom_point()
p11
# 点的形状,大小及颜色
p12 <- ggplot(mtcars, aes(x=wt, y=mpg, shape=cyl, color=cyl, size=cyl)) +
geom_point()
p12
ggarrange(p10,p11,p12,nrow = 1)
# 添加回归线
p13 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm)
# 移除置信区间
# 延长回归线
p14 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)
# 置信带的填充颜色可以如下更改:
p15 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, aes(fill=cyl))
ggarrange(p13,p14,p15,nrow = 1)
# 手动更改点的颜色/形状/大小
# ----------------------------------------------------------------
# 手动更改点的颜色及形状
p16 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
theme(legend.position="top")
# 手动更改点的大小
p17 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl))+
geom_point(aes(size=cyl)) +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
scale_shape_manual(values=c(3, 16, 17))+
scale_color_manual(values=c('#999999','#E69F00', '#56B4E9'))+
scale_size_manual(values=c(2,3,4))+
theme(legend.position="top")
p18 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point() +
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
theme_classic()
# brewer调色板
p19 <- p18 + scale_color_brewer(palette="Dark2")
# 灰度
p20 <- p18 + scale_color_grey()
ggarrange(p16,p17,p18,p19,p20,nrow = 2,ncol = 3)
2.7 添加边缘地毯
# 添加边缘地毯
# geom_rug(sides ="bl"),sides=“trbl”, for top, right, bottom, and left.
p21 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point() + geom_rug()
# 更改颜色
p22 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl)) +
geom_point() + geom_rug()
# Add marginal rugs using faithful data
p23 <- ggplot(faithful, aes(x=eruptions, y=waiting)) +
geom_point() + geom_rug()
ggarrange(p21,p22,p23,nrow = 1)
2.8 二维密度估计的散点图
# 二维密度估计的散点图 geom_density_2d(),stat_density_2d()
sp1 <- ggplot(faithful, aes(x=eruptions, y=waiting)) +
geom_point()
sp2 <- sp1 + geom_density_2d()
# 渐变色
sp3 <- sp1 + stat_density_2d(aes(fill = ..level..), geom="polygon")
# 更改渐变颜色
sp4 <- sp1 + stat_density_2d(aes(fill = ..level..), geom="polygon")+
scale_fill_gradient(low="blue", high="red")
ggarrange(sp1,sp2,sp3,sp4,nrow = 1)
2.9 椭圆散点图
# 椭圆散点图 stat_ellipse()
# 围绕所有点的一个椭圆
p24 <- ggplot(faithful, aes(waiting, eruptions))+
geom_point()+
stat_ellipse()
# 分组椭圆
p25 <- ggplot(faithful, aes(waiting, eruptions, color = eruptions > 3))+
geom_point()
p26 <- p25 + stat_ellipse()
# 更改椭圆的类型:可能的值为 "t", "norm", "euclid"
p27 <- p25 + stat_ellipse(type = "norm")
ggarrange(p24,p25,p26,p27,nrow = 1)
2.10 带有矩形箱的散点图
# geom_bin2d() 用于添加二维箱计数的热图
# stat_bin_2d() 用于计数矩形箱中的观测次数
# stat_summary_2d() 将功能应用于二维矩形箱
# plot + geom_bin2d(...)
# plot+stat_bin_2d(geom=NULL, bins=30)
# plot + stat_summary_2d(geom = NULL, bins = 30, fun = mean)
# geom:显示数据的几何对象
# bins:垂直和水平方向上的垃圾箱数。 默认值为30
# fun:汇总功能
head(diamonds)
# Plot
p28 <- ggplot(diamonds, aes(carat, price))
p29 <- p28 + geom_bin2d()
# 更改bins
p30 <- p29 + geom_bin2d(bins=10)
# 或指定bins的宽度
p31 <- p29 + geom_bin2d(binwidth=c(1, 1000))
ggarrange(p28,p29,p30,p31,nrow = 1)
2.11 具有边际密度分布图的散点图
# 建立数据
set.seed(1234)
x <- c(rnorm(500, mean = -1), rnorm(500, mean = 1.5))
y <- c(rnorm(500, mean = 1), rnorm(500, mean = 1.7))
group <- as.factor(rep(c(1,2), each=500))
df <- data.frame(x, y, group)
head(df)
# 创建散点图,按组分色
scatterPlot <- ggplot(df,aes(x, y, color=group)) +
geom_point() +
scale_color_manual(values = c('#999999','#E69F00')) +
theme(legend.position=c(0,1), legend.justification=c(0,1))
scatterPlot
# x的边际密度图(上图)
xdensity <- ggplot(df, aes(x, fill=group)) +
geom_density(alpha=.5) +
scale_fill_manual(values = c('#999999','#E69F00')) +
theme(legend.position = "none")
xdensity
# y的边际密度图(右图)
ydensity <- ggplot(df, aes(y, fill=group)) +
geom_density(alpha=.5) +
scale_fill_manual(values = c('#999999','#E69F00')) +
theme(legend.position = "none")
ydensity
# 创建一个空白的占位符图:
blankPlot <- ggplot()+geom_blank(aes(1,1))+
theme(plot.background = element_blank(),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_blank(),
panel.background = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank()
)
# 可以使用gridExtra软件包将多个图放在同一页面上
install.packages("gridExtra")
library("gridExtra")
grid.arrange(xdensity, blankPlot, scatterPlot, ydensity,
ncol=2, nrow=2, widths=c(4, 1.4), heights=c(1.4, 4))
2.12 自定义散点图
# 基础散点图
p32 <- ggplot(mtcars, aes(x=wt, y=mpg)) +
geom_point()+
geom_smooth(method=lm, color="black")+
labs(title="Miles per gallon \n according to the weight",
x="Weight (lb/1000)", y = "Miles/(US) gallon")+
theme_classic()
# 按组更改颜色及形状,移除置信区间
p33 <- ggplot(mtcars, aes(x=wt, y=mpg, color=cyl, shape=cyl)) +
geom_point()+
geom_smooth(method=lm, se=FALSE, fullrange=TRUE)+
labs(title="Miles per gallon \n according to the weight",
x="Weight (lb/1000)", y = "Miles/(US) gallon")
p34 <- p33 + theme_classic()
# 连续型颜色
p35 <- p33 + scale_color_brewer(palette="Paired") + theme_classic()
# 离散型颜色
p36 <- p33 + scale_color_brewer(palette="Dark2") + theme_minimal()
# 渐变色
p37 <- p33 + scale_color_brewer(palette="Accent") + theme_minimal()
ggarrange(p32,p33,p34,p35,p36,p37,nrow = 2,ncol = 3)
3.Stripcharts
3.1 语法
geom_jitter( mapping = NULL, data = NULL, stat = "identity", position = "jitter", ..., width = NULL, height = NULL, na.rm = FALSE, show.legend = NA, inherit.aes = TRUE )
3.2 基本带状图
# 将可变剂量从数字转换为因子变量
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
# 基本带状图
library(ggplot2)
p1 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter()
# 改变位置
# 0.2:x方向的抖动度
p2 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter(position=position_jitter(0.2))
# 旋转带状图
p3 <- p2 + coord_flip()
# 选择展示数据
p4 <- p2 + scale_x_discrete(limits=c("0.5", "2"))
ggarrange(p1,p2,p3,p4)
# 变更点大小
p5 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter(position=position_jitter(0.2), cex=1.2)
# 改变形状
p6 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter(position=position_jitter(0.2), shape=17)
ggarrange(p5,p6)
3.3 在活动图表上添加摘要统计信息
# 添加均值和中位数
p7 <- p2 + stat_summary(fun.y=mean, geom="point", shape=18,
size=3, color="red")
p8 <- p3 + stat_summary(fun.y=median, geom="point", shape=18,
size=3, color="red")
ggarrange(p7,p8)
# 带箱线图和小提琴图的带状图
# Add basic box plot
p9 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()+
geom_jitter(position=position_jitter(0.2))
# Add notched box plot
p10 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot(notch = TRUE)+
geom_jitter(position=position_jitter(0.2))
# Add violin plot
p11 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_violin(trim = FALSE)+
geom_jitter(position=position_jitter(0.2))
ggarrange(p9,p10,p11,nrow = 1)
# 添加平均值和标准偏差
p12 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter(position=position_jitter(0.2))
p13 <- p12 + stat_summary(fun.data="mean_sdl", mult=1,
geom="crossbar", width=0.5)
p14 <- p12 + stat_summary(fun.data=mean_sdl, mult=1,
geom="pointrange", color="red")
ggarrange(p12,p13,p14,nrow = 1)
# 统计公式 (mean and +/- sd)
data_summary <- function(x) {
m <- mean(x)
ymin <- m-sd(x)
ymax <- m+sd(x)
return(c(y=m,ymin=ymin,ymax=ymax))
}
p15 <- p12 + stat_summary(fun.data=data_summary, color="blue")
p15
3.4 按组带状图
# 按组更改点形状
p16 <- ggplot(ToothGrowth, aes(x=dose, y=len, shape=dose)) +
geom_jitter(position=position_jitter(0.2))
# 手动更改点形状
p17 <- p16 + scale_shape_manual(values=c(1,17,19))
ggarrange(p16,p17)
# 按组更改带状图的颜色
# 单色
p18 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_jitter(position=position_jitter(0.2), color="red")
# 按组更改带状图的颜色
p19 <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose)) +
geom_jitter(position=position_jitter(0.2))
# 自定义调色板
p20 <- p19 + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# brewer调色板
p21 <- p19 + scale_color_brewer(palette="Dark2")
# 灰度
p22 <- p19 + scale_color_grey() + theme_classic()
ggarrange(p18,p19,p20,p21,p22,nrow=2,ncol = 3)
3.5 修改图例
# 更改图例位置
p23 <- p19 + theme(legend.position="top")
p24 <- p19 + theme(legend.position="bottom")
p25 <- p19 + theme(legend.position="none")# Remove legend
# 更改图例中的项目顺序
p26 <- p19 + scale_x_discrete(limits=c("2", "0.5", "1"))
ggarrange(p23,p24,p25,p26)
3.6 带多个组的带状图
# 带多个组的带状图
# 按组更改带状图的颜色
p27 <- ggplot(ToothGrowth, aes(x=dose, y=len, color=supp)) +
geom_jitter(position=position_jitter(0.2))
# 更改位置:同一组的图表之间的间隔
p28 <-ggplot(ToothGrowth, aes(x=dose, y=len, color=supp, shape=supp)) +
geom_jitter(position=position_dodge(0.8))
ggarrange(p27,p28)
3.7 带状图添加箱形图
# 更改带状图的颜色并添加箱形图:
# 改变颜色
p29 <- p28 + scale_color_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# 添加箱线图
p30 <- ggplot(ToothGrowth, aes(x=dose, y=len, color=supp)) +
geom_boxplot(color="black")+
geom_jitter(position=position_jitter(0.2))
# 修改位置
p31 <- ggplot(ToothGrowth, aes(x=dose, y=len, color=supp)) +
geom_boxplot(position=position_dodge(0.8))+
geom_jitter(position=position_dodge(0.8))
ggarrange(p29,p30,p31,nrow = 1)
3.8 自定义带状图
# 自定义带状图
# 基础带状图
p32 <- ggplot(ToothGrowth, aes(x=dose, y=len)) +
geom_boxplot()+
geom_jitter(position=position_jitter(0.2))+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")+
theme_classic()
# 按组更改颜色/形状
p33 <- ggplot(ToothGrowth, aes(x=dose, y=len, color=dose, shape=dose)) +
geom_jitter(position=position_jitter(0.2))+
labs(title="Plot of length by dose",x="Dose (mg)", y = "Length")
p34 <- p33 + theme_classic()
# 连续型颜色
p35 <- p33 + scale_color_brewer(palette="Blues") + theme_classic()
# 离散型颜色
p36 <- p33 + scale_color_brewer(palette="Dark2") + theme_minimal()
# 渐变色
p37 <- p33 + scale_color_brewer(palette="RdBu")
ggarrange(p32,p33,p34,p35,p36,p37,nrow = 2,ncol = 3)
Reference
1.ggplot2 dot plot : Quick start guide - R software and data visualization
2.ggplot2 scatter plots : Quick start guide - R software and data visualization
3.ggplot2 stripchart (jitter) : Quick start guide - R software and data visualization