R使用ggplot实现基础数据可视化

  1. 在开始之前,我们需要设置工作目录并导入数据集 liver.df 作为案例示范,如果需要liver.df可以评论。
R 复制代码
# set your own working directory!
setwd("liver_files_location")

liver.cells.df <- read.table("liver.cells.txt", header = FALSE)
liver.section.df <- read.table("liver.section.txt", header = FALSE)
liver.exper.df <- read.table("liver.exper.txt", header = FALSE) 
liver.gt.df <- read.table("liver.gt.txt", header = TRUE)
liver.df <- data.frame(liver.cells.df, liver.section.df, liver.exper.df, liver.gt.df)
colnames(liver.df) <- c("cells", "section", "exper", colnames(liver.gt.df))
  1. 安装ggplot包。
R 复制代码
install.packages("ggplot2")
library(ggplot2)

ggplot2 是由 Hadley Wickham 创建的 R 包。它提供了一个强大的图形工具,用于创建美观且复杂的图表。近年来,它在 R 社区中的受欢迎程度急剧上升。该包允许您以直接的方式创建图表,展示单变量和多变量的数值和分类数据。分组可以通过颜色、符号、大小和透明度来表示。

  1. 散点图绘制:
R 复制代码
ggplot(liver.df, aes(x = cells, y = PRL)) + geom_point(col = "red") +
  labs(title = "Cells vs PRL", x = "Number of Cells", y = "PRL Response")

选定x轴为cells,y轴为PRL,使用红颜色绘制散点,labs标签用于标题和x、y轴名称的显示。

  1. 密度图绘制:
R 复制代码
ggplot(liver.df, aes(x = PRL, colour = exper, fill = exper)) +
  geom_density(alpha = 0.5) + xlim(-20,40) + 
  labs(title = "Density of PRL", x = "PRL Response", y = "Density")

使用liver.df数据框,aes选定x轴且告诉r语言我们要分别对exper的变量填色,透明度用alpha=0。5调整,x的可视范围调为-20到40,再对标题等进行名称显示。

  1. 多张散点图绘制:
R 复制代码
ggplot(liver.df, aes(x = PRL, y = ARL, color = exper)) +
  geom_point() + facet_wrap(section ~ exper) + theme_minimal()

命令 facet_wrap(section ~ exper) 按照章节和实验分割散点图,theme_minimal() 将绘图风格改为极简主题(例如,它移除了灰色背景)。

  1. 回归线绘制:
R 复制代码
ggplot(liver.df, aes(x = cells, y = ARL, colour = exper)) + geom_point() +
      labs(title = "Regression of ARL on cells", 
      x = "Number of Injected Cells", y = "ARL Response") +
      geom_smooth(method = 'lm', formula = y ~ x)

我们为每个实验在细胞上绘制ARL的回归图,使用geom_smooth(method = 'lm'),color根据exper选择不同颜色。

  1. 箱形图:
R 复制代码
ggplot(liver.df, aes(y = PRL, x = exper, fill = exper)) +
      geom_boxplot()  +
      labs(y = "PRL Response", x = "Experiment")

使用 geom_boxplot()生成各实验的 PRL 方框图。

ggplot2数据可视化官方文档

  1. the ggplot reference site: Function reference • ggplot2

  2. this ggplot2 Tutorial (short version and part 1-3): How to make any plot in ggplot2? | ggplot2 Tutorial

相关推荐
The god of big data6 小时前
深入探索 DeepSeek 在数据分析与可视化中的应用
ai·数据挖掘·数据分析
wyg_0311137 小时前
用deepseek学大模型04-模型可视化与数据可视化
人工智能·机器学习·信息可视化
白-胖-子11 小时前
DeepSeek系统架构的逐层分类拆解分析,从底层基础设施到用户端分发全链路
人工智能·机器学习·分类·数据挖掘·系统架构·agi·deepseek
码界筑梦坊14 小时前
基于Flask的艺恩影片票房分析系统的设计与实现
大数据·后端·python·信息可视化·flask·毕业设计
赵钰老师15 小时前
【深度学习】遥感影像目标检测:从CNN(Faster-RCNN)到Transformer(DETR)
pytorch·深度学习·目标检测·机器学习·数据分析·cnn·transformer
普通网友16 小时前
自动化办公更简单了!新版python-office,有哪些更新?
运维·人工智能·python·stable diffusion·数据挖掘·自动化·midjourney
蜂鸟视图fengmap17 小时前
蜂鸟视图发布AI智能导购产品:用生成式AI重构空间服务新范式
人工智能·信息可视化·deepseek·蜂鸟云地图编辑器·地图绘制工具·室内外地图一体化
♢.*19 小时前
析言GBI:用自然语言交互重构企业数据分析范式
人工智能·数据分析·大模型·交互·bi·阿里
谁不学习揍谁!20 小时前
基于Django快递物流管理可视化分析系统(完整系统源码+数据库+详细开发文档+万字详细论文+答辩PPT+详细部署教程等资料)
数据库·人工智能·后端·python·django·数据可视化
初尘屿风1 天前
基于Python的Diango旅游数据分析推荐系统设计与实现+毕业论文(15000字)
数据库·spring boot·python·数据挖掘·数据分析·旅游