【分析绘图】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)"
)
相关推荐
Michelle80236 小时前
R语言 for循环
开发语言·r语言
余丁,微生信1 天前
在线绘制饼+弧线图以展示venn交集情况
数据分析·数据可视化·论文插图·生信分析·科学科普·文恩图·基因表达
没有梦想的咸鱼185-1037-16631 天前
AI大模型支持下的顶刊绘图|散点图、气泡图、柱状图、热力图、柱状图、热力图、箱线图、热力图、云雨图、韦恩图、瀑布图、神经网络图、时间序列或分布展示
人工智能·神经网络·arcgis·信息可视化·数据分析·r语言·ai写作
易基因科技1 天前
易基因:NC/IF15.7:浙江大学陈淑洁/王良静团队acRIP-seq等揭示ac4C RNA修饰调控肠道衰老及年龄相关肠道疾病发病机制
人工智能·科研·生物学·生信分析
青春不败 177-3266-05202 天前
基于R语言BIOMOD2及机器学习方法的物种分布模拟与案例分析
随机森林·机器学习·r语言·生态学·植被遥感·生物多样性·物种分布
青春不败 177-3266-05203 天前
R语言与DSSAT作物模型
r语言·生态学·作物模型·数字农业·dssat模型·农业科学
asyxchenchong8883 天前
农业系统模拟APSIM全流程详解(气象/土壤/碳氮平衡/NG版本)附R批量处理代码
开发语言·r语言
idolao7 天前
R语言4.4.3统计分析软件安装教程:详细步骤+自定义安装路径(64位)
开发语言·r语言
做cv的小昊7 天前
【TJU】应用统计学——第四周作业(2.3 C-R不等式、2.4区间估计)
c语言·人工智能·算法·机器学习·数学建模·r语言·概率论
爱技术的阿呆9 天前
R code debug 和 study
开发语言·r语言