R语言基础统计与图形绘制:面向SRE工程师的数据探索指南 (Version 2024.07.31)
TL;DR: This document provides a foundational guide for SRE engineers on R language for statistical analysis and data visualization. It covers installation, basic operations, descriptive statistics, and `ggplot2` for plotting. Focus is on practical application for operational data exploration.
1. 前置条件与环境准备
本章节概述R语言环境的搭建步骤。所有操作均在Linux (Ubuntu 22.04 LTS) 环境下验证。
1.1 R环境安装
确保系统已安装R基础环境。本例使用CRAN提供的官方包。
1.1.1 添加CRAN仓库。
sudo apt update
sudo apt install --no-install-recommends software-properties-common dirmngr
# Add the CRAN GPG key
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys E298A3A825C0D65DFD57CBB651716619E084DAB9
# Add the CRAN repository
sudo add-apt-repository 'deb https://cloud.r-project.org/bin/linux/ubuntu focal-cran40/'
Expected Output (partial):
...
GPG key ID: E298A3A825C0D65DFD57CBB651716619E084DAB9
...
Repository successfully added.
1.1.2 安装R。
sudo apt update
sudo apt install r-base
Expected Output (partial):
...
The following NEW packages will be installed: ... r-base ...
Do you want to continue? [Y/n] y
1.1.3 验证R版本。
R --version
Expected Output:
R version 4.3.3 (2024-02-29) -- "Masked Marvel"
Copyright (C) 2024 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
...
Note: RStudio Desktop作为IDE可简化开发流程,但非强制性依赖。对于服务器端脚本执行,纯R环境足够。
2. 基础统计分析实践
本章节演示R语言进行描述性统计分析。我们将使用一个模拟的系统响应时间数据集。
2.1 数据准备与加载
创建一个样本数据集。
# 启动R交互式环境
R
# 创建响应时间数据 (单位: 毫秒)
response_times <- c(120, 150, 110, 135, 140, 160, 125, 130, 155, 145, 210, 105)
# 查看数据
print(response_times)
Expected Output:
[1] 120 150 110 135 140 160 125 130 155 145 210 105
2.2 描述性统计
计算数据的基本统计量。
# 计算均值
mean_rt <- mean(response_times)
print(paste("Mean Response Time:", mean_rt))
# 计算中位数
median_rt <- median(response_times)
print(paste("Median Response Time:", median_rt))
# 计算标准差
sd_rt <- sd(response_times)
print(paste("Standard Deviation:", sd_rt))
# 计算四分位数和五数总括
summary_rt <- summary(response_times)
print("Summary Statistics:")
print(summary_rt)
Expected Output (partial):
[1] "Mean Response Time: 140.833333333333"
[1] "Median Response Time: 137.5"
[1] "Standard Deviation: 27.2878477610058"
[1] "Summary Statistics:"
Min. 1st Qu. Median Mean 3rd Qu. Max.
105.0 123.8 137.5 140.8 151.2 210.0
3. 数据可视化入门
本章节介绍使用ggplot2包进行数据可视化,绘制直方图和箱线图。
3.1 安装与加载ggplot2
Warning: 包安装需要网络连接。对于内部环境,可配置CRAN镜像站点。
# 安装ggplot2包
install.packages("ggplot2")
# 加载ggplot2库
library(ggplot2)
Expected Output (partial):
...
Installing package into ‘/usr/local/lib/R/site-library’
...
Loading required package: ggplot2
3.2 绘制直方图
直方图用于展示数据分布。
# 创建数据框 (ggplot2通常使用数据框)
df_rt <- data.frame(ResponseTime = response_times)
# 绘制直方图
hist_plot <- ggplot(df_rt, aes(x = ResponseTime)) +
geom_histogram(binwidth = 10, fill = "steelblue", color = "black") +
labs(title = "系统响应时间分布", x = "响应时间 (ms)", y = "频率") +
theme_minimal()
# 打印图形 (会显示在RStudio Plots面板或输出到文件)
print(hist_plot)
# 将图形保存为PNG文件。这是R语言绘图输出的重要一步,方便自动化报告。
ggsave("response_time_histogram.png", plot = hist_plot, width = 8, height = 6, dpi = 300)
Expected Output (console, after print(hist_plot)):
(No explicit console output for plot object, plot appears in graphics device/viewer)
Expected Output (file system):
(A file named 'response_time_histogram.png' will be created in the current working directory.)
3.3 绘制箱线图
箱线图用于展示数据的五数总括和异常值。
# 绘制箱线图
boxplot_plot <- ggplot(df_rt, aes(y = ResponseTime)) +
geom_boxplot(fill = "lightblue", color = "darkblue") +
labs(title = "系统响应时间箱线图", y = "响应时间 (ms)") +
theme_minimal()
# 打印图形
print(boxplot_plot)
# 保存图形
ggsave("response_time_boxplot.png", plot = boxplot_plot, width = 6, height = 8, dpi = 300)
Expected Output (console, after print(boxplot_plot)):
(No explicit console output for plot object, plot appears in graphics device/viewer)
Expected Output (file system):
(A file named 'response_time_boxplot.png' will be created in the current working directory.)
Note: ggsave命令对于自动化报告和将图表嵌入内部wiki文档非常有用。此文涵盖了R语言统计分析入门教程的核心内容,可作为SRE团队内部分析的起点。