R语言绘图知识点随手记(不定期更新纠正)

0. 说明

记录自己在R语言绘图中常用的一些绘图微调的知识点。

1. 拼图

1.1 gridExtra

示例:

grid.arrange(arrangeGrob(f1,f2,f3,nrow=1,widths=c(1/3,1/3,1/3)),
		     arrangeGrob(f4,f5,f6,nrow=1,widths=c(1/3,1/3,1/3)),
		     ncol=1)

1.2 patchwork

示例:
f1 + f2 + f3 + plot_layout(ncol=1,heights = c(1,1,1))

1.3 ggpubr

示例:
ggarrange(p1,p2,p3,ncol = 3, nrow = 1, common.legend = TRUE)
'common.legend = TRUE' 可以为多张图提供一个共用的图例。参考:https://stackoverflow.com/questions/13649473/add-a-common-legend-for-combined-ggplots

2. 给图片添加注释

2.1 ggplot2

2.1.1 geom_text()

2.1.2 geom_label()

2.2 ggrepel

geom_text_repel(), geom_label_repel()中的:

1.nudge_x = 0, nudge_y = 0 可以调整注释的位置;
2.size = 20 可以调整字体大小;
3.segment.color='black',segment.size=1.5 可以调整引线的颜色和粗细;

2.2.1 geom_text_repel()

2.2.2 geom_label_repel()

3. 调整图例

3.1 提取图片中的图例使之单独成为一张图片

p = ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width", color = "Species", palette = "jco", ggtheme = theme_minimal())To create a scatter plot

leg = get_legend(p)To get an object of class gtable.
leg可以用于ggarrange等拼图函数,用于为多图提供一个共用的图例。
例如:ggarrange(p1,p2,leg,ncol=3)
as_ggplot(leg)Convert to a ggplot and print.

参考:https://www.rdocumentation.org/packages/ggpubr/versions/0.4.0/topics/get_legend

3.2 调整图例顺序

guides(fill=guide_legend(reverse=TRUE))将图例顺序调为反向。

4. 分面图的微调

4.1 facet_wrap()facet_grid()都可以绘制分面图,但有差异。

facet_wrap() 绘图如下:
facet_wrap()
facet_grid()绘图如下:
在这里插入图片描述

4.2 每个小窗口的标签的调整

theme(strip.text = element_text(size = 50,face = "bold",family = "italic"))

4.3 指定纵轴顺序

在处理数据时,给数据指定顺序即可:

data$col_1 = factor(data$col_1, levels = c("Angiosperms","Gymnosperms","Ferns","Clubmosses","Bryophytes","Charophytes","Algaes")

4.4 指定颜色

p +
scale_fill_manual(values=c("Angiosperms" = 'purple',
                             "Gymnosperms" = 'blue',
                             "Ferns" = 'green',
                             "Clubmosses" = 'yellow',
                             "Bryophytes" = 'orange',
                             "Charophytes" = 'red',
                             "Algaes" = 'white')) 

5. function() 自定义函数

格式:

func_name = function(parameter){
}

6. for 循环

格式:

for (val in sequence)
{
	statement
}

利用 for循环,多次调用 自定义函数function的例子。

each_plot = function(name){
  data = subset(data_source_2, data_source_2$species_name == name)
  p = ggplot(data = data) +
  			geom_col(aes(x=gene_id, y=count)) +
    		ylim(0,22) +
    		coord_flip() +
    		ggtitle(name)
  return(f)
  
}
d = list()
e = list()
i = 1
for (name in data_source_1[,1]){
  d[[i]] = each_plot(name)
  e[i]=list(d[[i]])
  i = i+1
}

do.call('grid.arrange',c(e,ncol=7,nrow=5))

7. 循环时给变量赋值assign()

格式:
assign(name, value)

 i = 1
for (name in data_source_1[,1]){
	assign(paste0('f',i),each_plot(name))
	#重复调用上面的each_plot函数,并将每次的结果赋值给fi
	i = i+1
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值