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 plote + geom_dotplot(binaxis = "y", stackdir = "center")# Change dotsize and stack ratioe + geom_dotplot(binaxis = "y", stackdir = "center",                 stackratio = 1.5, dotsize = 1.1)

 

 

添加统计值

stat_summary() 可以用来添加统计值

添加均值或中位数

# Add mean or median point: use fun = mean or fun = mediane + 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))

生物信息学

python中字典dict的操作技巧汇总

2020-5-5 21:50:08

生物信息学

R语言常用数据处理代码整理

2020-5-18 16:53:38

声明 本网站部分文章源于互联网,出于传递更多信息和学习之目的转载,并不保证内容正确或赞同其观点。
如转载稿涉及失效、版权等问题,请立即联系管理员;我们会予以修改、删除相关文章,请留言反馈
Notice: When your legal rights are being violated, please send an email to: [email protected].
0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
今日签到
有新私信 私信列表
搜索