三种风格作图的比较
#作图分三类
#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")
ggplot2的特殊语法:列名不带引号,函数之间写加号
属性设置:映射:根据数据的某一列的内容分配颜色;统一设置:把图形设置为一个颜色,与数据无关
注:必须先有aes(color=xx),scale_color_manual才有用,否则不干活又不报错
关于配色的R包
一个geom函数画出来的所有东西称为一个几何对象
解决点重合的问题,加上随机抖动
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()
# 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参数的要求:横坐标两两组合形成的向量形成的列表
去哪里找现成的图画代码
练习题
字符分割时如果涉及多个分割符,需要用 | 分割
下图用| 分割了空格与逗号
多个字符替换同理 str_replace(x2,"o|e","A")
rm(list = ls())
if(!require(stringr))install.packages('stringr')
library(stringr)
x <- "The birch canoe slid on the smooth planks."
### 1.检测字符串长度
str_length(x)
[1] 42
length(x)
[1] 1
### 2.字符串拆分
str_split(x," ")
[[1]]
[1] "The" "birch" "canoe" "slid" "on" "the" "smooth" "planks."
class(str_split(x," "))
[1] "list"
x2 = str_split(x," ")[[1]];x2
[1] "The" "birch" "canoe" "slid" "on" "the" "smooth" "planks."
y = c("jimmy 150","nicker 140","tony 152")
str_split(y," ")
[[1]]
[1] "jimmy" "150"
[[2]]
[1] "nicker" "140"
[[3]]
[1] "tony" "152"
str_split(y," ",simplify = T)# simplify = T不返回列表,返回矩阵
[,1] [,2]
[1,] "jimmy" "150"
[2,] "nicker" "140"
[3,] "tony" "152"
### 3.按位置提取字符串
str_sub(x,5,9)
[1] "birch"
### 4.字符检测
str_detect(x2,"h")
[1] TRUE TRUE FALSE FALSE FALSE TRUE TRUE FALSE
str_starts(x2,"T")
[1] TRUE FALSE FALSE FALSE FALSE FALSE FALSE FALSE
str_ends(x2,"e")
[1] TRUE FALSE TRUE FALSE FALSE TRUE FALSE FALSE
### 5.字符串替换
x2
[1] "The" "birch" "canoe" "slid" "on" "the" "smooth" "planks."
str_replace(x2,"o","A")
[1] "The" "birch" "canAe" "slid" "An" "the" "smAoth" "planks."
str_replace_all(x2,"o","A")
[1] "The" "birch" "canAe" "slid" "An" "the" "smAAth" "planks."
### 6.字符删除
x
## [1] "The birch canoe slid on the smooth planks."
str_remove(x," ")
[1] "Thebirch canoe slid on the smooth planks."
str_remove_all(x," ")
[1] "Thebirchcanoeslidonthesmoothplanks."
### 7.正则表达式(简)——只要人名
y = c("jimmy 150","nicker 140","tony 152")
str_split(y," ",simplify = T)[,1]#先简化为矩阵,再取第一列
[1] "jimmy" "nicker" "tony"
str_remove_all(y," |\\d")#利用正则表达式,将空格和数字均去除
[1] "jimmy" "nicker" "tony"
test <- iris[c(1:2,51:52,101:102),]
rownames(test) =NULL # 去掉行名,NULL是“什么都没有”
test
## 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
# arrange,数据框按照某一列排序,列名不加引号否则不报错也不排序
library(dplyr)
arrange(test, Sepal.Length) #从小到大
## 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
arrange(test, desc(Sepal.Length)) #从大到小
## 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
# distinct,数据框按照某一列去重复
distinct(test,Species,.keep_all = T)
## 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
# mutate,数据框新增一列
mutate(test, new = Sepal.Length * Sepal.Width)
## 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
# 连续的步骤
# 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()
## 一.条件语句
###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"))
## 二、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)
}
隐式循环
test1 <- data.frame(name = c('jimmy','nicker','Damon','Sophie'),
blood_type = c("A","B","O","AB"))
test1
## name blood_type
## 1 jimmy A
## 2 nicker B
## 3 Damon O
## 4 Sophie AB
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
## name group vision
## 1 Damon group1 4.2
## 2 jimmy group1 4.3
## 3 nicker group2 4.9
## 4 tony group2 4.5
library(dplyr)
inner_join(test1,test2,by="name")
## name blood_type group vision
## 1 jimmy A group1 4.3
## 2 nicker B group2 4.9
## 3 Damon O group1 4.2
left_join(test1,test2,by="name")
## 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
right_join(test1,test2,by="name")
## 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
full_join(test1,test2,by="name")
## 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 删除。