【分析绘图】R语言实现一些常见的绘图

微生信-在线绘图网站

线性图

r 复制代码
library(ggplot2)

x <- rnorm(100, 14, 5)  # rnorm(n, mean = 0, sd = 1)
y <- x + rnorm(100, 0, 1)
ggplot(data = NULL, aes(x = x, y = y)) +  # 开始绘图
  geom_point(color = "darkred") +  # 添加点
  annotate(
    "text",
    x = 13,  # 位置
    y = 20,
    parse = T,
    label = "x[1] == x[2]"
  )  # 添加注释

频率分布直方图

r 复制代码
yx = c(1, 2, 3, 5)
hist(
  yx,
  col = "PINK",
  labels = TRUE,
  ylim = c(0, 10),
  main = "频率分布图",
  xlab = "X",
  ylab = "出现频数"
)

Venn图

r 复制代码
library(VennDiagram)
library(grid)
venn.plot <- draw.pairwise.venn(
  area1 = 754,  # 区域1的数
  area2 = 687,  # 区域2的数
  cross.area = 139,  # 交叉数
  category = c("A", "B"),  # 分类名称
  fill = c("red", "blue"),  # 区域填充颜色
  lty = "blank",  # 区域边框线类型
  cex = 2,  # 区域内部数字的字体大小
  cat.cex = 1.5,  # 分类名称的字体大小
  cat.col = c("red", "blue"),  # 分类名称的显示颜色
  cat.pos = c(185, 185), #分类名称在圆的位置,默认正上方,通过角度进行调整
  # cat.dist = 0.09,   #分类名称距离边的距离(可以为负数)
  # cat.just = list(c(-1, -1), c(1, 1)),  #分类名称的位置
  # alpha = 0.7,  # 透明度
)

# 将venn.plot通过grid.draw画到pdf文件中
pdf("venn.pdf")
grid.draw(venn.plot)
dev.off()

柱状累计分布图

r 复制代码
data <- matrix(c(2587, 4576, 2457, 2946, 6670, 5790, 5862, 5421), ncol = 4, nrow = 2)
colnames(data) <- c('B-neg', 'T-neg', 'B-pos', 'T-pos')
barplot(
  height = data,
  main = "15minB和T",  # 标题
  col = c('green', 'white'),  # 填充颜色
  legend.text = c('Total','Match'),#设置图例的内容
  args.legend = list(x = "topright", cex=0.7), #修改图例的位置
  xlim = c(0, 9),
  ylim = c(0, 12000), # Y轴范围
  width = 1.5,  # 必须指定xlim
)

箱体图

r 复制代码
library(data.table)
library(ggplot2)

dat <- data.table(
  Spring = c(runif(9, 0, 1), 2),
  Summer = runif(10, 0, 1),
  Autumn = runif(10, 0, 1),
  Winter = runif(10, 0, 1)
)

dat1 <- melt(dat, measure.vars = c("Spring", "Summer", "Autumn", "Winter"))
ggplot(data = dat1, aes(x = variable, y = value, colour = variable)) + geom_boxplot()

热图

r 复制代码
library(data.table)
library(pheatmap)

# 假设这是你的示例数据
b_data <- data.frame(
  Gene = c("Gene1", "Gene2", "Gene3", "Gene4"),
  Annotation = c("TypeA", "TypeB", "TypeA", "TypeB"),
  Value1 = c(1.2, 2.3, 0.8, 1.5),
  Value2 = c(0.9, 1.8, 1.1, 2.0),
  Value3 = c(2.5, 1.1, 1.9, 1.7)
)
# b_data <- data.frame(data.table::fread("pvalue.csv", sep = ",", header = T))

# 设置行名和注释
rownames(b_data) <- b_data$Gene
annotation_row <- data.frame(Type = b_data$Annotation)
rownames(annotation_row) <- b_data$Gene
colnames(annotation_row) <- "Type    "

# 提取数值矩阵
data <- as.matrix(b_data[, -(1:2)])

# 创建热图
pheatmap(
  log2(data + 0.01),
  cluster_rows = FALSE,  # 对行聚类
  cluster_cols = TRUE,  # 对列聚类
  treeheight_col = 0,  # 列聚类树高度,默认为50
  show_rownames = FALSE,
  show_colnames = TRUE,
  annotation_row = annotation_row,  # 对行进行注释
  main = "Heatmap (log2)"
)
相关推荐
图灵信徒15 小时前
R语言第七章线性回归模型
数据挖掘·数据分析·r语言·线性回归
翰佰尔HiOmics云分析3 天前
转录组分析实战:GO与KEGG富集分析原理及R语言实现
r语言·转录组·krgg
Q一件事3 天前
R语言处理潜在蒸散nc数据
开发语言·r语言
胡侃有料4 天前
【目标检测】two-stage------Mask R-CNN浅析-2018
目标检测·r语言·cnn
饭九钦vlog7 天前
一键配置kali脚本
r语言
维维180-3121-14557 天前
从入门到精通:R语言结构方程模型(SEM)在生态学研究中的全面应用
r语言·生态·环境·农业·林业
胖达不服输7 天前
「日拱一码」155 小提琴图
人工智能·机器学习·绘图·小提琴图
Teacher.chenchong8 天前
R语言实现物种分布预测与生态位分析:多元算法实现物种气候生态位动态分析与分布预测,涵盖数据清洗、模型评价到论文写作全流程
开发语言·算法·r语言
AAIshangyanxiu9 天前
基于R语言的物种气候生态位动态量化与分布特征模拟-组合物种分布模型(Ensemble Species Distribution Model)
r语言·物种分布·物种气候生态位·物种气候