zl程序教程

您现在的位置是:首页 >  工具

当前栏目

生信技能树学习笔记-day6

笔记学习 技能 生信 Day6
2023-06-13 09:16:30 时间

title: "生信技能树学习笔记"

author: "天空"

引用自生信技能树 date: "2023-01-04"

output: html_document


R语言作图

1. 常用可视化R包

常用可视化R包.jpg

2. R基础包、ggplot2和ggpubr之间的绘图差别

基础包绘图.jpg
ggplot2_ggpubr.jpg
#作图分三类
#1.基础包 略显陈旧 了解一下
plot(iris[,1],iris[,3],col = iris[,5]) 
text(6.5,4, labels = 'hello')
unnamed-chunk-26-1.png
# dev.off() #关闭画板

#2.ggplot2 中坚力量,语法有个性
library(ggplot2)
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))
unnamed-chunk-26-2.png
#3.ggpubr 新手友好型 ggplot2简化和美化 褒贬不一
library(ggpubr)
ggscatter(iris,
          x="Sepal.Length",
          y="Petal.Length",
          color="Species")
unnamed-chunk-26-3.png

3. ggplot2画图

(1)ggplot2语法

ggplot2语法.jpg
ggplot2入门.jpg
ggplot2属性设置.jpg

(2)ggplot2映射VS手动设置

ggplot2映射vs手动设置.jpg

映射:根据数据的某一列的内容分配颜色。

手动设置:把图形设置为一个或n个颜色,与数据内容无关。

#手动设置

ggplot(data = iris) + 
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length), 
             color = "blue")
unnamed-chunk-27-1.png
#映射

ggplot(data = iris) + 
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))
unnamed-chunk-27-2.png
ggplot2自定义映射的具体颜色.jpg
## Q1 能不能自行指定映射的具体颜色?

ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))+
  scale_color_manual(values = c("blue","grey","red"))
unnamed-chunk-28-1.png

(3)ggplot2:color与fill两个属性的差别

ggplot2color与fill.jpg
## Q2 区分color和fill两个属性
### Q2-1 空心形状和实心形状都用color设置颜色
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species),
             shape = 17) #17号,实心的例子
unnamed-chunk-29-1.png
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species),
             shape = 2) #2号,空心的例子
unnamed-chunk-29-2.png
### Q2-2 既有边框又有内心的,才需要color和fill两个参数

ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species),
             shape = 24,
             fill = "black") #24号,双色的例子
unnamed-chunk-29-3.png

(4)ggplot2分面功能

ggplot2分面1.jpg
ggplot2分面2.jpg
#3.分面
ggplot(data = iris) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_wrap(~ Species) 
unnamed-chunk-30-1.png
#双分面
dat = iris
dat$Group = sample(letters[1:5],150,replace = T)
ggplot(data = dat) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_grid(Group ~ Species) 
unnamed-chunk-30-2.png
# 小知识点
sample(letters[1:9],20,replace = TRUE)
##  [1] "i" "c" "e" "f" "i" "d" "g" "i" "g" "d" "h" "i" "i" "i" "e" "g" "f" "a" "a" "a"

(5)ggplot2几何对象

ggplot2几何对象1.jpg
ggplot2几何对象2.jpg
ggplot2几何对象3.jpg
#4.几何对象

#局部设置和全局设置

ggplot(data = iris) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length))+
  geom_point(mapping = aes(x = Sepal.Length, 
                           y = Petal.Length))
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
unnamed-chunk-31-1.png
ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length))+
  geom_smooth()+
  geom_point()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'
unnamed-chunk-31-2.png

(6)ggplot2统计变换

ggplot2统计变换.jpg
library(ggplot2)
#5.统计变换-直方图
table(diamonds$cut)
## 
##      Fair      Good Very Good   Premium     Ideal 
##      1610      4906     12082     13791     21551
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut))
unnamed-chunk-32-1.png
ggplot(data = diamonds) + 
  stat_count(mapping = aes(x = cut))
unnamed-chunk-32-2.png
统计变换场景1.jpg
#统计变换使用场景
#5.1.不统计,数据直接做图
fre = as.data.frame(table(diamonds$cut))
fre
##        Var1  Freq
## 1      Fair  1610
## 2      Good  4906
## 3 Very Good 12082
## 4   Premium 13791
## 5     Ideal 21551
ggplot(data = fre) +
  geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")
unnamed-chunk-33-1.png
#5.2count改为prop,group=1表示各分组一起作为整体
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = after_stat(prop), group = 1))
unnamed-chunk-34-1.png
# Warning: The dot-dot notation (`..prop..`) was deprecated in ggplot2 3.4.0. Please use `after_stat(prop)` instead.

(7)ggplot2位置关系

ggplot2位置关系.jpg
#6.位置关系

# 6.1抖动的点图
ggplot(data = iris,mapping = aes(x = Species, 
                                 y = Sepal.Width,
                                 fill = Species)) + 
  geom_boxplot()+
  geom_point()
unnamed-chunk-35-1.png
ggplot(data = iris,mapping = aes(x = Species, 
                                 y = Sepal.Width,
                                 fill = Species)) + 
  geom_boxplot()+
  geom_jitter()
unnamed-chunk-35-2.png
ggplot2位置关系2.jpg
# 6.2堆叠直方图
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut,fill=clarity))
unnamed-chunk-36-1.png
# 6.3 并列直方图
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
unnamed-chunk-36-2.png

(8)ggplot2坐标系

ggplot2坐标系.jpg
#7.坐标系

#翻转coord_flip()

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + 
  geom_boxplot() +
  coord_flip()
unnamed-chunk-37-1.png
#极坐标系coord_polar()
bar <- ggplot(data = diamonds) + 
  geom_bar(
    mapping = aes(x = cut, fill = cut), 
    width = 1
  ) + 
  theme(aspect.ratio = 1) +
  labs(x = NULL, y = NULL)
bar
unnamed-chunk-37-2.png
bar + coord_flip()
unnamed-chunk-37-3.png
bar + coord_polar()
unnamed-chunk-37-4.png
ggplot2完整绘图模板.jpg

4. ggpubr画图

(1)ggpubr示例1

ggpubr1.jpg
# ggpubr 搜代码直接用,基本不需要系统学习
# sthda上有大量ggpubr出的图
library(ggpubr)
ggscatter(iris,x="Sepal.Length",
          y="Petal.Length",
          color="Species")
unnamed-chunk-38-1.png
ggpubr2.jpg
p <- ggboxplot(iris, x = "Species", 
               y = "Sepal.Length",
               color = "Species", 
               shape = "Species",
               add = "jitter")
p
unnamed-chunk-39-1.png
my_comparisons <- list( c("setosa", "versicolor"), 
                        c("setosa", "virginica"), 
                        c("versicolor", "virginica") )
p + stat_compare_means(comparisons = my_comparisons)+ # Add pairwise comparisons p-value
  stat_compare_means(label.y = 9) 
## [1] FALSE
unnamed-chunk-39-2.png

5. 图片的保存和导出

图片保存示例

图片保存1.jpg
图片保存2.jpg
#图片保存的三种方法

#1.基础包作图的保存
pdf("iris_box_ggpubr.pdf")
boxplot(iris[,1]~iris[,5])
text(6.5,4, labels = 'hello')
dev.off()
## RStudioGD 
##         2
#2.ggplot系列图(包括ggpubr)通用的简便保存 ggsave
p <- ggboxplot(iris, x = "Species", 
               y = "Sepal.Length",
               color = "Species", 
               shape = "Species",
               add = "jitter")
unnamed-chunk-40-1.png
ggsave(p,filename = "iris_box_ggpubr.png")
## Saving 8.39 x 6.12 in image
#3.eoffice包 导出为ppt,全部元素都是可编辑模式
library(eoffice)
topptx(p,"iris_box_ggpubr.pptx")

#https://mp.weixin.qq.com/s/p7LLLvzR5LPgHhuRGhYQBQ

拼图

拼图.jpg