R基础_绘图

生信技能树学习笔记

  1. 基础包的绘图函数

高级函数:可单独运行

低级函数:需要在已有的图上运行

  1. ggplot2的特殊语法:列名不带引号,行末带加号

  1. 映射:根据数据的某一列的内容分配颜色,在映射中设置参数才有图例

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

多个图层叠加时,后面的图覆盖在前面的图上方

本身图层是没有颜色的,想要映射颜色 形状要加在想要的图层的aes中

  1. !!!不要修改内置数据,不要设置变量名和函数一样

  1. 代码不报错,不代表真没错,图片有歧义也是错误

  1. geom_jitter()抖动的点图

  1. ggpubr可以添加组间比较,p值,ggplot2中比较麻烦

  1. 代码可运行,但不出图:运行dev.off()直到null device 或 运行dev.new()

  1. 现成的画图代码:STHDA网站,公众号

sample(letters[1:5],150,replace=T) #重复抽样150次从letters[1:5]

拆分画板

par(mfrow=c(2,2))  
for (i in 1:4) {
  plot(iris[,2],col=iris[,ncol(iris)])
}
#作图分三类

#1.基础包 略显陈旧 了解一下
plot(iris[,1],iris[,3],col = iris[,5]) 
text(6.5,4, labels = 'hello')

dev.off() #关闭画板

#2.ggplot2 中坚力量,语法有个性
library(ggplot2)
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))

#3.ggpubr 新手友好型 ggplot2简化和美化 褒贬不一  可添加组间比较,p值
library(ggpubr)
ggscatter(iris,
          x="Sepal.Length",
          y="Petal.Length",
          color="Species")

ggplot2

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"))

## 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,
                           color = Species),
             shape = 24,
             fill = "black") #24号,双色的例子 fill设置的颜色是内部,color是设置的边框

#3.分面----
ggplot(data = iris) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_wrap(~ Species) 
#双分面
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) 

#4.几何对象----

#局部设置和全局设置

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()

#5.统计变换-直方图----
View(diamonds)
table(diamonds$cut)

ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut))

ggplot(data = diamonds) + 
  stat_count(mapping = aes(x = cut))

#统计变换使用场景
#5.1.不统计,数据直接做图----
fre = as.data.frame(table(diamonds$cut))
fre

ggplot(data = fre) +
  geom_bar(mapping = aes(x = Var1, y = Freq), stat = "identity")#stat不需要统计
#5.2count改为prop----
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))  # ..prop.. 用比例表示,group=1表示fair在所有数据中的比例


#6.位置关系----

# 6.1抖动的点图----
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_jitter()

# 6.2堆叠直方图-----
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut,fill=clarity))

# 6.3 并列直方图----
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")

#7.坐标系----

#翻转coord_flip()

ggplot(data = mpg, mapping = aes(x = class, y = hwy)) + 
  geom_boxplot() +
  coord_flip()
#极坐标系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
bar + coord_flip()
bar + coord_polar()

ggpubr

# ggpubr 搜代码直接用,基本不需要系统学习

# sthda上有大量ggpubr出的图
library(ggpubr)
ggscatter(iris,x="Sepal.Length",
          y="Petal.Length",
          color="Species")

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)+ # Add pairwise comparisons p-value
  stat_compare_means(label.y = 9) 

图片的保存和导出

#图片保存的三种方法

#1.基础包作图的保存
pdf("iris_box_ggpubr.pdf")
boxplot(iris[,1]~iris[,5])
text(6.5,4, labels = 'hello')
dev.off()

#2.ggplot系列图(包括ggpubr)通用的简便保存 ggsave
p1 <- ggboxplot(iris, x = "Species", 
               y = "Sepal.Length",
               color = "Species", 
               shape = "Species",
               add = "jitter")
ggsave(p,filename = "iris_box_ggpubr.png")

#3.eoffice包 导出为ppt,全部元素都是可编辑模式
library(eoffice)
topptx(p,"iris_box_ggpubr.pptx")

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

拼图

p2 = p1
p1 + p2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

xiaoxiang学生信

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值