ggplot2绘图学习 点图
之前我们学习了ggplot绘制单变量,两个连续变量的图形,两个离散型变量。对于一个离散型变量,一个连续型变量,有很多作图方式,包括箱图,点图等等
· geom_boxplot() for box plot
· geom_violin() for violin plot
· geom_dotplot() for dot plot
· geom_jitter() for stripchart
· geom_line() for line plot
· geom_bar() for bar plot
今天我们介绍一下点图
library(ggplot2)
data("ToothGrowth")
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
head(ToothGrowth)
我们先建立一个图层,以dose为X轴,len为Y轴
e <- ggplot(ToothGrowth, aes(x = dose, y = len))
主要函数及参数
· Key functions: geom_dotplot(), stat_summary()
· Key arguments to customize the plot: alpha, color, dotsize and fill.
基础用法
# Basic plot
e + geom_dotplot(binaxis = "y", stackdir = "center")
# Change dotsize and stack ratio
e + geom_dotplot(binaxis = "y", stackdir = "center",
stackratio = 1.5, dotsize = 1.1)
添加统计值
stat_summary() 可以用来添加统计值
添加均值或中位数
# Add mean or median point: use fun = mean or fun = median
e + geom_dotplot(binaxis = "y", stackdir = "center") +
stat_summary(fun = mean, geom = "point",
shape = 18, size = 3, color = "red")
添加误差棒(均值加减标准差)
# Add mean points +/- SD
# Use geom = "pointrange" or geom = "crossbar"
e + geom_dotplot(binaxis = "y", stackdir = "center") +
stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="pointrange", color = "red")
与箱图或者小提琴图合并
e + geom_boxplot() +
geom_dotplot(binaxis = "y", stackdir = "center")
e + geom_violin(trim = FALSE) +
geom_dotplot(binaxis='y', stackdir='center')
e + geom_violin(trim = FALSE) +
geom_dotplot(binaxis='y', stackdir='center') +
stat_summary(fun.data="mean_sdl", fun.args = list(mult=1),
geom="pointrange", color = "red")
通过颜色区分分组
# 单一颜色
e + geom_dotplot(binaxis='y', stackdir='center', color = "black",
fill = "lightgray") + theme_minimal()
# 改变轮廓颜色
e + geom_dotplot(aes(color = dose), binaxis='y', stackdir='center',
fill = "white") + theme_minimal()
# 改变填充色
e + geom_dotplot(aes(fill = dose), binaxis='y', stackdir='center') +
theme_minimal()
多组点图
# 不同分组不同颜色
e + geom_dotplot(aes(fill = supp), binaxis='y', stackdir='center')
# 改变点的位置区分不同组
e + geom_dotplot(aes(fill = supp), binaxis='y', stackdir='center',
position=position_dodge(0.8))
改变颜色,增加箱图
# 改变颜色
e + geom_dotplot(aes(fill = supp), binaxis='y', stackdir='center',
position=position_dodge(0.8)) +
scale_fill_manual(values=c("#999999", "#E69F00"))
# 加箱图
e + geom_boxplot(fill = "white") +
geom_dotplot(aes(fill = supp), binaxis='y', stackdir='center')
# 改变位置
e + geom_boxplot(aes(fill = supp), position=position_dodge(0.8))+
geom_dotplot(aes(fill = supp), binaxis='y', stackdir='center',
position=position_dodge(0.8))
资源贴
单基因泛癌分析
赞 (0)