R语言基础-画图(ggplot2)

编程/开发
133
0
0
2024-02-07
标签   R语言

title: "R语言基础-画图(ggplot2)"

output: html_document

date: "2023-09-12"

ggplot2绘图模版

# ggplot(data = <DATA>)+
#   <geom_FUNCTION>(mapping = aes(<MAPPINGS>),
#                   stat = <STAT>,
#                   position = <POSITION>)+
#   <COORDINATE_FUNCTION>+
#   <FACET_FUNCTION>

1.数据

演示数据以R的内置数据iris为例进行说明

2. 属性设置(颜色、大小、点的形状、透明度、线型等)

2.1 手动设置

颜色-color;大小-size;形状-shape;透明度-alpha;填充颜色-fill

具体的实际参数现查现用即可哦

#设置颜色
library(ggplot2)

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)  # 点的形状

可以发现这些参数都在aes()这个函数外面,因此称为手动设置哦。

2.2 映射(绘图模版-“mapping”)

#依旧以颜色为例,如果不指定就是默认配色哦
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))

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

除了颜色,shape、size、alpha等之类的参数可以通过类似的方式进行调整或指定哦

#fill和color的区分及连用
#空心或实心都可以通过将shape-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号,空心的例子

#通过color-shape-fill三个参数的连用,可以达到双色实心的目的
#类似于ppt中的轮廓颜色、填充颜色,示例如下
ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species),
             shape = 24,
             fill = "black") #24号,双色的例子(可能黑色不是很明显哈)

2.3 分面-绘图模版“FACET_FUNCTION”

分面即把一张图分成多张子图

#单分面(依旧以点图为例)
ggplot(data = iris) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_wrap(~ Species)#根据什么进行分面

#双分面
dat = iris#建议重新赋值而不是在内置数据iris上直接进行修改哦
dat$Group = sample(letters[1:5],150,replace = T)
ggplot(data = dat)+
  geom_point(mapping = aes(x = Sepal.Length,y = Petal.Width,
                           color = Species))+
  facet_grid(Group~Species)#依旧在facet函数上做文章

2.4 几何对象-绘图模版“geom_FUNCTION”

1.不同的几何对象可以叠加

2.可以通过局部设置以及全局设置的切换高度自定义不同的几何对象

#局部设置
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()

#上述两段代码等价哦,但明显第二个更简洁。根据需要可以自定义这些设置。

2.5 统计变换-绘图模版"stat= <STAT>"

#此处以R内置数据diamonds为例,先简单查看下数据,做一些简单的统计
View(diamonds)
table(diamonds$cut)
## 
##      Fair      Good Very Good   Premium     Ideal 
##      1610      4906     12082     13791     21551
#画条形图,这边只要写x,y是函数自动生成的哦,自己非要写上的话报错
#下面两段代码在这个情况下是出同一张图,不过因为市场需求关系,geom_FUNCTION类型的函数相对于用的更多。
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut))

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

2.5.1 不统计,数据直接做图

#这里需要对stat这个形式参数进行修改不然加上y的话会报错哦
#修改方法如下:
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")

2.5.2 count改为prop(比例)

#修改方法如下:
ggplot(data = diamonds)+
  geom_bar(mapping = aes(x = cut,y = after_stat(prop),group = 1))

#y这边不写的话默认是after_stat(count),由于这边需要求比例因此改成prop
#group = 1意思是将整个数据视作一组

2.6 位置关系-绘图模版"position= <POSITION>"

#position 默认值为并列
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity))

#position = 'dodge'堆叠式
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), 
           position = "dodge")

#position = 'fill' 填充式(cibersort免疫浸润分析使用较多)
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), 
           position = "fill")

2.7 坐标系-绘图模版"position= <POSITION>"

#以经典条形图为例
bar <- ggplot(data = diamonds) + #赋值成bar是为了更好地演示坐标系不同形式
  geom_bar(mapping = aes(x = cut, fill = cut),
           width = 1) + #宽度这个参数可以自己修改看看发生什么变化
  theme(aspect.ratio = 1) + #这一行是切换主题哦
  labs(x = NULL, y = NULL)
bar

#翻转坐标系
bar + coord_flip()#翻转coord_flip() 

bar + coord_polar()#极坐标系coord_polar()

补充:主题改变

#以ggpubr的白底风格为例
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut, fill = clarity), 
           position = "fill")+
  theme_classic() #加一句这个就可以了哦

3.ggpubr

#这边仅展示日常使用较多的箱线图组间比较
library(ggpubr)
p = ggboxplot(iris,x = 'Species',y = 'Sepal.Length',
              color = 'Species',shape = 'Species',
              add = 'jitter')

my_comparisions = list(c('setosa','versicolor'),
                       c('setosa','virginica'),
                       c('versicolor','virginica'))
#comparisons需要符合以下三个条件:
#1.list;2.列表的下一级元素必须是长度为2的向量;3.这个长度为2的向量必须是横坐标
p + stat_compare_means(comparisons = my_comparisions)+
  stat_compare_means(label.y = 9) #总体比较的p值纵坐标为9

4.图片保存

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

ggsave(p,filename = 'lalala1.png')
引用自生信技能树