R语言画图基础

总论可视:

基础包使用不多,不再赘述,如下图:

一般以ggplot2和ggpubr为主

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

mapping = aes是映射的意思

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

我们主要以ggplot2为主进行介绍

1.ggplot2语法

library(ggplot2)
#1.入门级绘图模板:作图数据,横纵坐标
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length))

iris为数据框,x,y为横纵坐标;使用geom类别下的各类函数画出不同类型的图。

ggplot2拥有特殊语法,数据框列名不加引号,函数间的衔接用+。

1.1ggplot2属性设置

#2.2 映射:按照数据框的某一列来定义图的某个属性
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))
#即利用数据框中的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"))
#必须要先有color = Species,才能使用scale_color_manual


#想要什么颜色就有什么颜色-十六进制颜色编码,使用snipaste可识别颜色
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))+
  scale_color_manual(values = c("#2874C5","#e6b707","#f87669"))

*使用R包配色:paletteer调色盘

#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#提供了包中的不同配色方案

 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号,双色的例子

1.2图像叠加

一个geom函数画出来的所有东西称为一个几何对象。并如何应用以上规则画出下图?

#画出的是图1
ggplot(data = iris,mapping = aes(x = Species, 
                                 y = Sepal.Width,
                                 fill = Species)) + 
  geom_boxplot()+
  geom_point()
#抖动的图2
ggplot(data = iris,mapping = aes(x = Species, 
                                 y = Sepal.Width,
                                 fill = Species)) + 
  geom_boxplot()+
  #geom_point(position = "jitter")
  geom_jitter()

如何翻转坐标系?

#使用coord_flip()可翻转坐标系
ggplot(data = iris,mapping = aes(x = Species, 
                                 y = Sepal.Width,
                                 fill = Species)) + 
  geom_boxplot()+
  geom_jitter()+ 
  coord_flip()

调整背景(theme函数,bw是白板)

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

2.ggpubr的使用

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)))
#comparisons必须两两对应的列表,长度为二的字符

3.图片保存与拼接

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

dev.new()#重开画板,用于卡住或错误
3.2 ggplot系列图(包括ggpubr)通用的简便保存 ggsave
p <- ggboxplot(iris, x = "Species", 
               y = "Sepal.Length",
               color = "Species", 
               shape = "Species",
               add = "jitter")
ggsave(p,filename = "iris_box_ggpubr.png")
3.3 eoffice包 导出为ppt,全部元素都是可编辑模式
library(eoffice)
topptx(p,"iris_box_ggpubr.pptx")
3.4大佬新包patchwork:可能是迄今为止最优秀的拼图包

后续再补充,非常简单,只需将图片赋值变量,p+q即可拼图

3.5file:///C:/Users/R_03/top50ggplot.html

R_03中有相应的学习代码,好看的50个图片,另再次推荐

4.练习

组合画图,先运行的代码图层越下面。如何修改组合画图的颜色配置?

library(ggplot2)
ggplot(data = iris,mapping = aes(x = Sepal.Length, y = Petal.Length,color = Species))+
  geom_point()+
  geom_boxplot()+
  scale_color_manual(values = c("#2874C5","#e6b707","#f87669"))

连续画图中几何对象的修改,并修改形状,增加标题:

a=ggplot(data = iris, mapping = aes(x = Sepal.Width, y = Species))+
  geom_violin(aes(fill =Species) )+
  geom_boxplot()+
  geom_jitter(aes(shape = Species))+
  scale_shape_manual(values = c(5,22,23))
a=a + labs(title = "New plot title")
ggsave(a,filename = "a.png")

用ggbetweenstats画盒子/小提琴图并拼图加标题

library(ggstatsplot)
library(patchwork)#拼图
p=ggbetweenstats(iris,"Species","Sepal.Width")
p=p + labs(title = "New plot title")
q=ggscatterstats(iris,"Sepal.Width","Petal.Length")
q=p + labs(title = "New plot title2")
p+q

引用自生信技能树

  • 5
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值