前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >技能树Day03_直播课05-06_R作图与综合利用

技能树Day03_直播课05-06_R作图与综合利用

原创
作者头像
sheldor没耳朵
发布2024-07-19 18:41:20
650
发布2024-07-19 18:41:20
举报
文章被收录于专栏:生物信息学习

1 直播课05_R作图

1.1 常用的可视化R包和函数

三种风格作图的比较

代码语言:txt
复制
#作图分三类

#1.基础包 略显陈旧 了解一下
plot(iris[,1],iris[,3],col = iris[,5]) 
text(6.5,4, labels = 'hello') # 所加lable的横纵坐标

dev.off() #关闭画板

#2.ggplot2 中坚力量,语法有个性
#灰底白线是ggplot2的默认特征
library(ggplot2)
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))

#3.ggpubr 新手友好型 ggplot2简化和美化 褒贬不一
library(ggpubr)
ggscatter(iris,
          x="Sepal.Length",
          y="Petal.Length",
          color="Species")

1.2 ggplot2

ggplot2的特殊语法:列名不带引号,函数之间写加号

属性设置:映射:根据数据的某一列的内容分配颜色;统一设置:把图形设置为一个颜色,与数据无关

注:必须先有aes(color=xx),scale_color_manual才有用,否则不干活又不报错

关于配色的R包

一个geom函数画出来的所有东西称为一个几何对象

解决点重合的问题,加上随机抖动

代码语言:r
复制
library(ggplot2)
#1.入门级绘图模板:作图数据,横纵坐标
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length))
#2.属性设置(颜色、大小、透明度、点的形状,线型等)

#2.1 手动设置,需要设置为有意义的值

ggplot(data = iris) + 
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length), 
             color = "blue")

ggplot(data = iris) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length), 
             size = 5,     # 点的大小5mm
             alpha = 0.5,  # 透明度 50%
             shape = 8)  # 点的形状

#2.2 映射:按照数据框的某一列来定义图的某个属性
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))

## Q1 能不能自行指定映射的具体颜色?

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

#想要什么颜色就有什么颜色-十六进制颜色编码
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))+
  scale_color_manual(values = c("#2874C5","#e6b707","#f87669"))
#paletteer-集成多个配色R包,两千多种选择
if(!require(paletteer))install.packages("paletteer",ask = F,update = F)
if(!require(awtools))install.packages("awtools",ask = F,update = F)
library(paletteer)
ggplot(data = iris)+ 
  geom_point(mapping = aes(x = Sepal.Length, 
                           y = Petal.Length, 
                           color = Species))+ 
  scale_color_paletteer_d("awtools::mpalette") 
palettes_d_names
#View(palettes_d_names)
## Q2 区分color和fill两个属性
### Q2-1 空心形状和实心形状都用color设置颜色
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species),
             shape = 17) #17号,实心的例子

ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species),
             shape = 2) #2号,空心的例子
### Q2-2 既有边框又有内心的,才需要color和fill两个参数

ggplot(data = iris)+  
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length),
             shape = 24,
             color = "red",          				     
             fill = "yellow") #24号,双色的例子


#3.几何对象

#局部设置和全局设置

ggplot(data = iris) + 
  geom_smooth(mapping = aes(x = Sepal.Length, 
                          y = Petal.Length))+
  geom_point(mapping = aes(x = Sepal.Length, 
                           y = Petal.Length))

ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length))+
  geom_smooth()+
  geom_point()

#4.位置

# 抖动的点图
ggplot(data = iris,mapping = aes(x = Species, 
                                 y = Sepal.Width,
                                 fill = Species)) + 
  geom_boxplot()+
  geom_point()

ggplot(data = iris,mapping = aes(x = Species, 
                                 y = Sepal.Width,
                                 fill = Species)) + 
  geom_boxplot()+
  #geom_point(position = "jitter")
  geom_jitter()

#5.坐标系

ggplot(data = iris,mapping = aes(x = Species, 
                                 y = Sepal.Width,
                                 fill = Species)) + 
  geom_boxplot()+
  geom_jitter()+ 
  coord_flip()

#6.主题
ggplot(data = iris,mapping = aes(x = Species, 
                                 y = Sepal.Width,
                                 fill = Species)) + 
  geom_boxplot()+
  geom_jitter()+ 
  theme_bw()

1.3 ggpubr

代码语言:r
复制
# ggpubr 搜代码直接用,基本不需要系统学习

# sthda上有大量ggpubr出的图
library(ggpubr)
p = ggboxplot(iris, x = "Species", y = "Sepal.Length",
              color = "Species", shape = "Species",add = "jitter")
p
my_comparisons <- list( c("setosa", "versicolor"), 
                        c("setosa", "virginica"), 
                        c("versicolor", "virginica") )
p + stat_compare_means(comparisons = my_comparisons,
                       aes(label = after_stat(p.signif)))

注:ggplot2,ggpubr画图可以赋值,这样方便保存、添加修改、拼图等(Rbase不可以赋值)

comparisons参数的要求:横坐标两两组合形成的向量形成的列表

1.4 图片保存

1.5 拼图

去哪里找现成的图画代码

练习题

2 直播课06_R的综合利用

2.1 玩转字符串

字符分割时如果涉及多个分割符,需要用 | 分割

下图用| 分割了空格与逗号

多个字符替换同理 str_replace(x2,"o|e","A")

代码语言:r
复制
rm(list = ls())
if(!require(stringr))install.packages('stringr')
library(stringr)

x <- "The birch canoe slid on the smooth planks."
### 1.检测字符串长度
str_length(x)
代码语言:txt
复制
[1] 42
代码语言:r
复制
length(x)
代码语言:txt
复制
[1] 1
代码语言:r
复制
### 2.字符串拆分
str_split(x," ")
代码语言:txt
复制
[[1]]
[1] "The"     "birch"   "canoe"   "slid"    "on"      "the"     "smooth"  "planks."
代码语言:r
复制
class(str_split(x," "))
代码语言:txt
复制
[1] "list"
代码语言:r
复制
x2 = str_split(x," ")[[1]];x2
代码语言:txt
复制
[1] "The"     "birch"   "canoe"   "slid"    "on"      "the"     "smooth"  "planks."
代码语言:r
复制
y = c("jimmy 150","nicker 140","tony 152")
str_split(y," ")
代码语言:txt
复制
[[1]]
[1] "jimmy" "150"  
[[2]]
[1] "nicker" "140"   
[[3]]
[1] "tony" "152"
代码语言:r
复制
str_split(y," ",simplify = T)# simplify = T不返回列表,返回矩阵
代码语言:txt
复制
      [,1]     [,2] 
[1,] "jimmy"  "150"
[2,] "nicker" "140"
[3,] "tony"   "152"
代码语言:r
复制
### 3.按位置提取字符串
str_sub(x,5,9)
代码语言:txt
复制
[1] "birch"
代码语言:r
复制
### 4.字符检测
str_detect(x2,"h")
代码语言:txt
复制
[1]  TRUE  TRUE FALSE FALSE FALSE  TRUE  TRUE FALSE
代码语言:r
复制
str_starts(x2,"T")
代码语言:txt
复制
[1]  TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
代码语言:r
复制
str_ends(x2,"e")
代码语言:txt
复制
[1]  TRUE FALSE  TRUE FALSE FALSE  TRUE FALSE FALSE
代码语言:r
复制
### 5.字符串替换
x2
代码语言:txt
复制
[1] "The"     "birch"   "canoe"   "slid"    "on"      "the"     "smooth"  "planks."
代码语言:r
复制
str_replace(x2,"o","A")
代码语言:txt
复制
[1] "The"     "birch"   "canAe"   "slid"    "An"      "the"     "smAoth"  "planks."
代码语言:r
复制
str_replace_all(x2,"o","A")
代码语言:txt
复制
[1] "The"     "birch"   "canAe"   "slid"    "An"      "the"     "smAAth"  "planks."
代码语言:r
复制
### 6.字符删除
x
代码语言:r
复制
## [1] "The birch canoe slid on the smooth planks."
str_remove(x," ")
代码语言:txt
复制
[1] "Thebirch canoe slid on the smooth planks."
代码语言:r
复制
str_remove_all(x," ")
代码语言:txt
复制
[1] "Thebirchcanoeslidonthesmoothplanks."
代码语言:r
复制
### 7.正则表达式(简)——只要人名
y = c("jimmy 150","nicker 140","tony 152")
str_split(y," ",simplify = T)[,1]#先简化为矩阵,再取第一列
代码语言:txt
复制
[1] "jimmy"  "nicker" "tony"
代码语言:r
复制
str_remove_all(y," |\\d")#利用正则表达式,将空格和数字均去除
代码语言:txt
复制
[1] "jimmy"  "nicker" "tony"

2.2 玩转数据框

代码语言:r
复制
test <- iris[c(1:2,51:52,101:102),]
rownames(test) =NULL # 去掉行名,NULL是“什么都没有”
test
代码语言:txt
复制
##   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1          5.1         3.5          1.4         0.2     setosa
## 2          4.9         3.0          1.4         0.2     setosa
## 3          7.0         3.2          4.7         1.4 versicolor
## 4          6.4         3.2          4.5         1.5 versicolor
## 5          6.3         3.3          6.0         2.5  virginica
## 6          5.8         2.7          5.1         1.9  virginica
代码语言:r
复制
# arrange,数据框按照某一列排序,列名不加引号否则不报错也不排序

library(dplyr)
arrange(test, Sepal.Length) #从小到大
代码语言:txt
复制
##   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1          4.9         3.0          1.4         0.2     setosa
## 2          5.1         3.5          1.4         0.2     setosa
## 3          5.8         2.7          5.1         1.9  virginica
## 4          6.3         3.3          6.0         2.5  virginica
## 5          6.4         3.2          4.5         1.5 versicolor
## 6          7.0         3.2          4.7         1.4 versicolor
代码语言:r
复制
arrange(test, desc(Sepal.Length)) #从大到小
代码语言:txt
复制
##   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1          7.0         3.2          4.7         1.4 versicolor
## 2          6.4         3.2          4.5         1.5 versicolor
## 3          6.3         3.3          6.0         2.5  virginica
## 4          5.8         2.7          5.1         1.9  virginica
## 5          5.1         3.5          1.4         0.2     setosa
## 6          4.9         3.0          1.4         0.2     setosa
代码语言:r
复制
# distinct,数据框按照某一列去重复
distinct(test,Species,.keep_all = T)
代码语言:txt
复制
##   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species
## 1          5.1         3.5          1.4         0.2     setosa
## 2          7.0         3.2          4.7         1.4 versicolor
## 3          6.3         3.3          6.0         2.5  virginica
代码语言:r
复制
# mutate,数据框新增一列
mutate(test, new = Sepal.Length * Sepal.Width)
代码语言:txt
复制
##   Sepal.Length Sepal.Width Petal.Length Petal.Width    Species   new
## 1          5.1         3.5          1.4         0.2     setosa 17.85
## 2          4.9         3.0          1.4         0.2     setosa 14.70
## 3          7.0         3.2          4.7         1.4 versicolor 22.40
## 4          6.4         3.2          4.5         1.5 versicolor 20.48
## 5          6.3         3.3          6.0         2.5  virginica 20.79
## 6          5.8         2.7          5.1         1.9  virginica 15.66
代码语言:r
复制
# 连续的步骤

# 1.多次赋值,产生多个中间的变量
x1 = select(iris,-5)
x2 = as.matrix(x1)
x3 = head(x2,50)#截取前50行
heatmap(x3)

# 2. 嵌套,代码不易读
heatmap(head(as.matrix(select(iris,-5)),50))

# 3.管道符号传递,简洁明了(快捷键command + shift + m)
iris %>%
  select(-5) %>%
  as.matrix() %>%
  head(50) %>% 
  heatmap()

2.3 项目管理

2.4 条件与循环

代码语言:r
复制
## 一.条件语句

###1.if(){ }

#### (1)只有if没有else,那么条件是FALSE时就什么都不做

i = -1
if (i<0) print('up')
if (i>0) print('up')

#理解下面代码
if(!require(tidyr)) install.packages('tidyr')

#### (2)有else
i =1
if (i>0){
  print('+')
} else {
  print("-")
}
i = 1
ifelse(i>0,"+","-")

x = rnorm(3)
x
ifelse(x>0,"+","-")

#ifelse()+str_detect(),王炸
samples = c("tumor1","tumor2","tumor3","normal1","normal2","normal3")
k1 = str_detect(samples,"tumor");k1
ifelse(k1,"tumor","normal")
k2 = str_detect(samples,"normal");k2
ifelse(k2,"normal","tumor")

#### (3)多个条件
i = 0
if (i>0){
  print('+')
} else if (i==0) {
  print('0')
} else if (i< 0){
  print('-')
}

ifelse(i>0,"+",ifelse(i<0,"-","0"))
代码语言:r
复制
## 二、for循环

for( i in 1:4){
  print(i)
}

#批量画图
par(mfrow = c(2,2))
for(i in 1:4){
  plot(iris[,i],col = iris[,5])
}
#批量装包
pks = c("tidyr","dplyr","stringr")
for(g in pks){
  if(!require(g,character.only = T))
    install.packages(g,ask = F,update = F)
}

隐式循环

2.4 数据框的连接

代码语言:r
复制
test1 <- data.frame(name = c('jimmy','nicker','Damon','Sophie'), 
                    blood_type = c("A","B","O","AB"))
test1
代码语言:txt
复制
##     name blood_type
## 1  jimmy          A
## 2 nicker          B
## 3  Damon          O
## 4 Sophie         AB
代码语言:r
复制
test2 <- data.frame(name = c('Damon','jimmy','nicker','tony'),
                    group = c("group1","group1","group2","group2"),
                    vision = c(4.2,4.3,4.9,4.5))
test2
代码语言:txt
复制
##     name  group vision
## 1  Damon group1    4.2
## 2  jimmy group1    4.3
## 3 nicker group2    4.9
## 4   tony group2    4.5
代码语言:r
复制
library(dplyr)
inner_join(test1,test2,by="name")
代码语言:txt
复制
##     name blood_type  group vision
## 1  jimmy          A group1    4.3
## 2 nicker          B group2    4.9
## 3  Damon          O group1    4.2
代码语言:r
复制
left_join(test1,test2,by="name")
代码语言:txt
复制
##     name blood_type  group vision
## 1  jimmy          A group1    4.3
## 2 nicker          B group2    4.9
## 3  Damon          O group1    4.2
## 4 Sophie         AB   <NA>     NA
代码语言:r
复制
right_join(test1,test2,by="name")
代码语言:txt
复制
##     name blood_type  group vision
## 1  jimmy          A group1    4.3
## 2 nicker          B group2    4.9
## 3  Damon          O group1    4.2
## 4   tony       <NA> group2    4.5
代码语言:r
复制
full_join(test1,test2,by="name")
代码语言:txt
复制
##     name blood_type  group vision
## 1  jimmy          A group1    4.3
## 2 nicker          B group2    4.9
## 3  Damon          O group1    4.2
## 4 Sophie         AB   <NA>     NA
## 5   tony       <NA> group2    4.5

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1 直播课05_R作图
    • 1.1 常用的可视化R包和函数
      • 1.2 ggplot2
        • 1.3 ggpubr
          • 1.4 图片保存
            • 1.5 拼图
            • 2 直播课06_R的综合利用
              • 2.1 玩转字符串
                • 2.2 玩转数据框
                  • 2.3 项目管理
                    • 2.4 条件与循环
                      • 2.4 数据框的连接
                      相关产品与服务
                      云直播
                      云直播(Cloud Streaming Services,CSS)为您提供极速、稳定、专业的云端直播处理服务,根据业务的不同直播场景需求,云直播提供了标准直播、快直播、云导播台三种服务,分别针对大规模实时观看、超低延时直播、便捷云端导播的场景,配合腾讯云视立方·直播 SDK,为您提供一站式的音视频直播解决方案。
                      领券
                      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档