美文网首页
R语言 -- ggplot 画图小技巧

R语言 -- ggplot 画图小技巧

作者: 生信摆渡 | 来源:发表于2021-05-31 11:03 被阅读0次

并没有什么小技巧,只是我的脑壳总是记不住的东西

简单组图

library("ggplot2")
library("gridExtra")
plots = list(p1, p2, p3)
grid.arrange(p1, arrangeGrob(p2, p3, ncol = 2), nrow=2) 

barplot 分页

barplot(go, split = "ONTOLOGY")+ facet_grid(ONTOLOGY ~ ., scale = "free") 

提示信息设为英语

Sys.setenv(LANGUAGE = "en")

更新所有R包

install.packages( 
    lib  = lib <- .libPaths()[1],
    pkgs = as.data.frame(installed.packages(lib), stringsAsFactors=FALSE)$Package,
    type = 'source'
)

指定R包安装路径

install.packages("xxx", lib = "path_to_rlib")

快速获取R版本

> getRversion()
[1] ‘3.6.3’

任意位置添加文本注释

annotate("text", x = 2, y = 1.2, label = expression(paste("p = ", 0.5)), size = 8, hjust = 0, parse = TRUE)
# 斜体
annotate("text", x = 0.005, y = 0.045, label = paste("italic(R) ^ 2:", R2), size = 8, hjust = 0, parse = TRUE)

添加虚线

geom_hline(aes(yintercept = -log2(0.05)), colour = "black", linetype = "dashed", size = 1.5)
geom_vline(aes(xintercept = -log2(0.05)), colour = "black", linetype = "dashed", size = 1.5)

图形旁边添加注释

geom_text(aes(label = data$count), position = position_stack(vjust = 1), size = 4, color = "red")

设置scalebar颜色,值域,反向

scale_colour_gradient(low = "red", high = "blue", limit = c(min(data$p.adjust), max(data$p.adjust)), guide = guide_colourbar(reverse = TRUE))

修改图例标题

guides(fill = guide_legend(title = "pvalue"))

设置文本

p <- theme(plot.title   = element_text(color = "black", size = title.size, face = face, hjust = 0.5),
           axis.title.x = element_text(color = "black", size = x.title.size, face = x.title.face),
           axis.title.y = element_text(color = "black", size = y.title.size, face = y.title.face, vjust = 1.2),
           axis.text.x  = element_text(size = x.text.size, angle = x.text.angle, hjust = x.text.hjust),
           axis.text.y  = element_text(size = y.text.size, angle = y.text.angle, hjust = y.text.hjust),
           plot.margin  = margin(t = 5.5, r = 5.5, b = 5.5, l = 5.5, unit = "pt"))

修改颜色映射

scale_color_manual(values = c("Tumor" = "#dd1c77", "Normal" = "#3182bd"))

修改坐标轴标签

scale_x_discrete(breaks = keys, labels = xtext)

相关文章

网友评论

      本文标题:R语言 -- ggplot 画图小技巧

      本文链接:https://www.haomeiwen.com/subject/cvnvsltx.html