当前位置: 首页 > article >正文

R语言科研编程-标准偏差柱状图

生成随机数据

在R中,可以使用rnorm()生成正态分布的随机数据,并模拟分组数据。以下代码生成3组(A、B、C)随机数据,每组包含10个样本:

set.seed(123)  # 确保可重复性
group_A <- rnorm(10, mean=50, sd=5)
group_B <- rnorm(10, mean=60, sd=8)
group_C <- rnorm(10, mean=45, sd=6)
data <- data.frame(Group = rep(c("A", "B", "C"), each=10),Value = c(group_A, group_B, group_C)
)

计算均值和标准偏差

使用dplyr包汇总数据,计算每组均值和标准偏差:

library(dplyr)
summary_data <- data %>%group_by(Group) %>%summarise(Mean = mean(Value),SD = sd(Value))

绘制柱状图与误差棒

使用ggplot2绘制柱状图,并通过geom_errorbar添加标准偏差误差棒:

library(ggplot2)
ggplot(summary_data, aes(x=Group, y=Mean, fill=Group)) +geom_bar(stat="identity", width=0.5) +geom_errorbar(aes(ymin=Mean-SD, ymax=Mean+SD), width=0.2) +labs(title="误差分析柱状图", y="均值 ± 标准偏差") +theme_minimal()

在这里插入图片描述

自定义图形样式(可选)

调整颜色、标题和坐标轴:

ggplot(summary_data, aes(x=Group, y=Mean, fill=Group)) +geom_bar(stat="identity", width=0.5, color="black") +geom_errorbar(aes(ymin=Mean-SD, ymax=Mean+SD), width=0.2, linewidth=0.7) +scale_fill_brewer(palette="Set2") +labs(title="误差分析柱状图", x="分组", y="测量值") +theme_classic()

输出图形

执行代码后,图形将显示在R的绘图窗口。如需保存为文件,使用ggsave()

ggsave("error_bar_plot.png", width=6, height=4, dpi=300)

安装patchwork包 在R或RStudio中执行以下命令从CRAN安装:

install.packages("patchwork")

双排显示

library(ggplot2)
library(patchwork)``````r 
p1 <- ggplot(summary_data, aes(x=Group, y=Mean, fill=Group)) + geom_bar(stat="identity", width=0.5) + geom_errorbar(aes(ymin=Mean-SD, ymax=Mean+SD), width=0.2) + labs(title="误差分析柱状图", y="均值 ± 标准偏差") + theme_minimal()p2 <- ggplot(summary_data, aes(x=Group, y=Mean, fill=Group)) + geom_bar(stat="identity", width=0.5, color="black") + geom_errorbar(aes(ymin=Mean-SD, ymax=Mean+SD), width=0.2, linewidth=0.7) + scale_fill_brewer(palette="Set2") + labs(title="误差分析柱状图", x="分组", y="测量值") + theme_classic()

左右排列显示结果

combined_horizontal <- p1 + p2 + plot_layout(ncol = 2)
print(combined_horizontal)

在这里插入图片描述

combined_vertical <- p1 / p2 + plot_layout(nrow = 2)
print(combined_vertical)

在这里插入图片描述

combined_horizontal + plot_annotation(title = "双图对比分析") + plot_layout(guides = "collect") & theme(plot.margin = unit(c(1,1,1,1), "cm"))

在这里插入图片描述
在这里插入图片描述

总结

柱状图是所有图像的基础,尝试建立不同的柱状图,在基本的格式基础上更改参数,多练习多尝试,加油吧小伙伴们。

http://www.lryc.cn/news/2387432.html

相关文章:

  • 未来教育考试答题软件4.0【自用链接备份】
  • OpenGL Chan视频学习-11 Uniforms in OpenGL
  • Flink系列文章列表
  • GitLab 从 17.10 到 18.0.1 的升级指南
  • 产业集群间的专利合作关系
  • PyQt学习系列02-模型-视图架构与数据管理
  • redis主从复制架构安装与部署
  • Kotlin 中 Lambda 表达式的语法结构及简化推导
  • YOLOv2 深度解析:目标检测领域的进阶之路
  • KT6368A通过蓝牙芯片获取手机时间详细说明,对应串口指令举例
  • 计算机网络实验课(二)——抓取网络数据包,并实现根据条件过滤抓取的以太网帧,分析帧结构
  • 自动生成提示技术突破:AUTOPROMPT重塑语言模型应用
  • 78. Subsets和90. Subsets II
  • VSCode 插件 GitLens 破解方法
  • linux 通过命令将 MinIO 桶的权限设置为 Custom(自定义策略)
  • 模型评价指标介绍
  • ElasticSearch整合SpringBoot
  • ArcGIS Pro 3.4 二次开发 - 知识图谱
  • 2025上半年软考高级系统架构设计师经验分享
  • uni-app学习笔记十二-vue3中创建组件
  • React 虚拟dom
  • 互联网大厂Java求职面试:AI与大模型应用集成中的架构难题与解决方案-1
  • 《算法笔记》13.2小节——专题扩展->树状数组(BIT) 问题 D: 数列-训练套题T10T3
  • 一键启动多个 Chrome 实例并自动清理的 Bash 脚本分享!
  • 4 月 62100 款 App 被谷歌下架!环比增长 28%
  • 图像分割全路线学习(结合论文)
  • Go语言之定义结构体(Struct)-《Go语言实战指南》
  • mediapipe标注视频姿态关键点(基础版加进阶版)
  • PCtoLCD2002如何制作6*8字符
  • SmartPlayer与VLC播放RTMP:深度对比分析延迟、稳定性与功能