r - 在 R 中生成带有误差线的条形图
问题描述
我的数据集如下所示:
> print(dataplot)
a b c d e f g h i
[1,] 0.9685 0.0150831 0.9253333 0.03018388 0.9856667 0.01330664 0.9268333 0.05894885 0.9686667
j k l
[1,] 0.01478738 0.9313333 0.07123389
其中a
, c
, e
, g
, i
,k
列是平均值b
,d
, , f
, h
, j
,列是l
它们各自的标准差。
我想绘制一个barplot()
包含三个组的分组,即:
1) 列a
和c
2) 列e
和g
3) 列i
和k
列a
, e
,i
可以有一种颜色,列c
, g
,可以有h
不同的颜色。我也想有误差线。
我一直在尝试使用以前的脚本,但我找不到如何使条形图显示为分组并具有各自的标准偏差。
我正在学习R,所以希望我能得到一些帮助。任何输入表示赞赏!
解决方案
编辑
根据评论中的要求更改了标签。
您可以稍微更改 data.frame 的结构,以获取可用于以下形状的数据ggplot()
:
library(ggplot2)
dat <- data.frame(a=0.9685, b=0.0150831, c=0.9253333, d=0.03018388, e=0.9856667, f=0.01330664, g=0.9268333, h=0.05894885, i=0.9686667,j=0.01478738, k=0.9313333, l=0.07123389)
data <- data.frame(mean=unlist(dat[, 1:ncol(dat) %% 2 == 1]), sd=unlist(dat[, 1:ncol(dat) %% 2 == 0]))
data$x <- rownames(data)
data$category <- as.factor(sort(rep(1:3, 2)))
data$Method <- as.factor(rep(c('k-NN', 'Decision trees'), 3))
gg <- ggplot(aes(x=category, y=mean, fill=Method, group=Method), data=data)
gg <- gg + geom_bar(stat='identity', position = position_dodge(), width=.5)
gg <- gg + geom_errorbar(aes(ymin=mean-sd, ymax=mean+sd), position = position_dodge(width=.5), width=.2)
gg
推荐阅读
- typescript - 为什么下拉值不使用打字稿来?
- javascript - 作为 WebSocket 消息发送时,压缩的 Java String 的长度不等于 content-length
- windows - 如何在 Windows 10 上安装 ROS - 但不在 C 盘中?
- ramda.js - 如何使用 Ramda 将数组拆分为子列表?
- javascript - Django项目中的jQuery Ajax问题
- python - 将列表转换为字典,其中包含 Python 中重复键的值列表
- javascript - 每10升价格上涨
- embedded - 使用 STM32F042F6 产生 0-5V 输出
- spring-boot - 使 Zuul 使用与 Spring Boot 应用程序不同的上下文路径
- laravel - 使用 select2 字段类型传递的空值