zl程序教程

您现在的位置是:首页 >  其他

当前栏目

R语言基础笔记-03(ggplot2)

基础笔记语言 03 ggplot2
2023-06-13 09:17:25 时间

一、ggplot2

模板:

ggplot(data = <DATA>)+

<GEOM_FUNCTION>(mapping =aes(<MAPPINGS>))

注意:

低级绘图函数单独使用会报错,需依附于高级绘图函数。

ggplot2特殊语法:列名不带引号,行末写加号!

1.属性设置

fill管实心,color管边框

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

library(ggplot2)
ggplot(data = iris) + 
  geom_point(mapping = aes(x = Sepal.Length, 
                           y = Petal.Length), 
             size = 1,     # 点的大小5mm
             alpha = 0.5,  # 透明度 50%
             shape = 8,  # 点的形状
             color = "blue")#这里color是geom_point的参数,即:把图形设置为一个或n个颜色,与数据内容无关
手动设置
shape

1.2 映射:按照数据框的某一列来定义图的某个属性

ggplot(data = iris)+
  geom_point(mapping = aes(x = Sepal.Length,
                           y = Petal.Length,
                           color = Species))+#这里的color为aes的参数,紧跟x,y出现,是数据框列名
  scale_color_manual(values =c("blue","grey","red")) #自行指定映射的颜色
映射

2.分面

分面的值必须有重复值

2.1分一面

+facet_wrap(~ xxx) ,xxx为数据框的一列,分面的依据

#分一面
ggplot(data = iris) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_wrap(~ Species) 
分一面

2.2分两个面

+facet_grid(xxx ~ yyy)

#双分面
dat = iris 
#sample()取值,replace表示是否放回
dat$Group = sample(letters[1:5],150,replace = T)#结果是新增一列group内容为随机取的1:5中一个
head(dat)
ggplot(data = dat) + 
  geom_point(mapping = aes(x = Sepal.Length, y = Petal.Length)) + 
  facet_grid(Group ~ Species) 
分两面

3.几何对象

#局部设置
#全局设置
ggplot(data = iris,mapping = aes(x = Sepal.Width,
                                 y = Species)) + 
  geom_violin(aes(fill = Species))+
  geom_boxplot()+
  geom_jitter(aes(shape=Species))
全局设置

4.直方图:geom_bar

4.1. 直接使用只需指定x,默认y是统计值

#统计变换-直方图
head(diamonds)
table(diamonds$cut)
## 
##      Fair      Good Very Good   Premium     Ideal 
##      1610      4906     12082     13791     21551
ggplot(data = diamonds) + 
  geom_bar(mapping = aes(x = cut))
直方图

4.2. 不统计,数据直接做图

+geom_bar(mapping = aes(x = , y = ), stat = "identity")

4.3.统计比例

+geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))

5.位置关系

5.1. 抖动的点图:+geom_jitter()

5.2. 堆叠直方图:fill=

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

5.3. 并列直方图:position = "dodge"

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

6.坐标系

翻转:+coord_flip()

极坐标系:+ coord_polar()

二、ggpubr

当有分组比较需求时,可用ggpubr,其余用ggplot2就够了

library(ggpubr)
my_comparisons <- list(c("setosa","versicolor"),
                        c("setosa","virginica"),
                        c("versicolor","virginica"))
ggboxplot(iris,
               x = "Species",
               y= "Sepal.Length",
               add = "jitter")+
  stat_compare_means(comparisons = my_comparisons)+
  stat_compare_means(label.y = 9)
ggpubr

三、图片保存

1.ggplot

ggsave("name.png")

ggsave(p, filename = "name.png")

2.三段式

保存的格式和文件名:pdf("test.pdf")

作图代码:...

关闭画板:dev.off

3.eoffice:导入到PPT

library(eoffice)

topptx(p,"p.pptx")

四、拼图

library(patchwork)

p1+p2

TIPS

  1. 不要漏了+
  2. 注意color是aes的参数还是gp的参数
  3. 不要修改内置数据
  4. boxplot叠加点图的时候,点图要分散开:使用geom_jitter
  5. 代码顺序决定图层上下

引用自生信技能树马拉松课程小洁老师授课内容:R语言基础03