前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言之可视化②点图

R语言之可视化②点图

作者头像
用户1359560
发布2018-12-05 11:39:41
2.4K0
发布2018-12-05 11:39:41
举报
文章被收录于专栏:生信小驿站生信小驿站

主要内容:

  • 准备数据
  • 基本点图
  • 在点图上添加摘要统计信息
  • 添加平均值和中位数
  • 带有盒子图和小提琴图的点图
  • 添加平均值和标准差
  • 按组更改点图颜色
  • 更改图例位置
  • 更改图例中项目的顺序
  • 具有多个组的点图
  • 定制的点图
  • 相关信息

第一步:准备数据,使用的数据包括三列,len长度,supp是分类变量,dose是0.5mg,1mg和2mg三个变量。

代码语言:javascript
复制
> # Convert the variable dose from a numeric to a factor variable
> ToothGrowth$dose <- as.factor(ToothGrowth$dose)
> head(ToothGrowth)
   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

第二步:绘制最基础的点图,然后修改点的大小,然后翻转X,Y轴

代码语言:javascript
复制
library(ggplot2)
# Basic dot plot
p<-ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_dotplot(binaxis='y', stackdir='center')
p
# Change dotsize and stack ratio
ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_dotplot(binaxis='y', stackdir='center',
               stackratio=1.5, dotsize=1.2)
# Rotate the dot plot
p + coord_flip()

设置仅显示dose为0.5mg和2mg两个分组的点图

代码语言:javascript
复制
p + scale_x_discrete(limits=c("0.5", "2"))

第三步:在点图上添加摘要统计信息,使用函数stat_summary()可用于向点图中添加均值/中值点等。

代码语言:javascript
复制
# dot plot with mean points
p + stat_summary(fun.y=mean, geom="point", shape=18,
                 size=3, color="red")
# dot plot with median points
p + stat_summary(fun.y=median, geom="point", shape=18,
                 size=3, color="red")

第四步:添加箱图

代码语言:javascript
复制
# Add basic box plot
ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_boxplot()+
  geom_dotplot(binaxis='y', stackdir='center')
# Add notched box plot
ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_boxplot(notch = TRUE)+
  geom_dotplot(binaxis='y', stackdir='center')
# Add violin plot
ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_violin(trim = FALSE)+
  geom_dotplot(binaxis='y', stackdir='center')

第六步:添加平均值和标准差,使用函数mean_sdl。 mean_sdl计算平均值加上或减去常数乘以标准差。在下面的R代码中,使用参数mult(mult = 1)指定常量。 默认情况下,mult = 2。平均值+/- SD可以添加为交叉开关或点范围:

代码语言:javascript
复制
p <- ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_dotplot(binaxis='y', stackdir='center')
p + stat_summary(fun.data="mean_sdl", fun.args = list(mult=1), 
                 geom="crossbar", width=0.5)
p + stat_summary(fun.data=mean_sdl, fun.args = list(mult=1), 
                 geom="pointrange", color="red")

第七步:按组更改点图颜色,在下面的R代码中,点图的填充颜色由剂量水平自动控制:

代码语言:javascript
复制
# Use single fill color
ggplot(ToothGrowth, aes(x=dose, y=len)) + 
  geom_dotplot(binaxis='y', stackdir='center', fill="#FFAAD4")
# Change dot plot colors by groups
p<-ggplot(ToothGrowth, aes(x=dose, y=len, fill=dose)) +
  geom_dotplot(binaxis='y', stackdir='center')
p

也可以使用以下功能手动更改点图颜色:

  • scale_fill_manual():使用自定义颜色
  • scale_fill_brewer():使用RColorBrewer包中的调色板
  • scale_fill_grey():使用灰色调色板
代码语言:javascript
复制
# Use custom color palettes
p+scale_fill_manual(values=c("#999999", "#E69F00", "#56B4E9"))
# Use brewer color palettes
p+scale_fill_brewer(palette="Dark2")
# Use grey scale
p + scale_fill_grey() + theme_classic()
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018.11.02 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档