ggplot2绘图学习 Stripcharts
之前我们学习了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
今天我们介绍一下Stripcharts
library(ggplot2)
data("ToothGrowth")
ToothGrowth$dose <- as.factor(ToothGrowth$dose)
e <- ggplot(ToothGrowth, aes(x = dose, y = len))
主要函数及参数
· Key functions: geom_jitter(), stat_summary()
· Key arguments to customize the plot: alpha, color, shape, size and fill.
基础用法
# 基础图
e + geom_jitter()
# 改变点的位置 0.2表示震动幅度
e + geom_jitter(position = position_jitter(0.2))
# 改变点的形状和大小
e + geom_jitter(position=position_jitter(0.2),
shape=17, size = 1.2)
添加统计值
stat_summary() 可以用来添加统计值
添加均值或中位数
e + geom_jitter(position = position_jitter(0.2)) +
stat_summary(fun = mean, geom = "point",
shape = 18, size = 3, color = "red")
添加误差棒(均值加减标准差)
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_boxplot() +
geom_jitter(position = position_jitter(0.2))
# 与小提琴合并
e + geom_violin(trim = FALSE) +
geom_jitter(position = position_jitter(0.2))
# 三合一
e + geom_violin(trim = FALSE) +
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), color = "steelblue") +
theme_minimal()
# 不同分组不同颜色
e + geom_jitter(aes(color = dose), position = position_jitter(0.2)) +
theme_minimal()
手动设置颜色
e2 <- e + geom_jitter(aes(color = dose), position = position_jitter(0.2)) +
theme_minimal()
e2 + scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
e2 + scale_color_brewer(palette="Dark2")
多重分组
改变不同组的颜色和形状
# 改变不同组的颜色和形状
e + geom_jitter(aes(color = supp, shape = supp),
position=position_jitter(0.2))
改变不同组的点的位置
e + geom_jitter(aes(color = supp, shape = supp),
position=position_dodge(0.2))
多重分组添加箱图
# 改变颜色
e + geom_jitter(aes(color = supp, shape = supp),
position=position_jitter(0.2))+
scale_color_manual(values=c("#999999", "#E69F00"))
# 加箱子
e + geom_boxplot(color = "black") +
geom_jitter(aes(color = supp, shape = supp),
position=position_jitter(0.2))
# 改变位置
e + geom_boxplot(aes(color = supp), position=position_dodge(0.8)) +
geom_jitter(aes(color = supp, shape = supp),
position=position_dodge(0.8))
资源贴
单基因泛癌分析
赞 (0)