【分析绘图】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]"
  )  # 添加注释

在这里插入图片描述

频率分布直方图

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

在这里插入图片描述

Venn图

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()

在这里插入图片描述

柱状累计分布图

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
)

在这里插入图片描述

箱体图

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()

在这里插入图片描述

热图

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)"
)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/liluo_2951121599/article/details/132469022