在 R 中创建多列数据的分组条形图

编程入门 行业动态 更新时间:2024-10-25 23:36:57
本文介绍了在 R 中创建多列数据的分组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有以下数据

Input Rtime Rcost Rsolutions Btime Bcost 1 12 proc. 1 36 614425 40 36 2 15 proc. 1 51 534037 50 51 3 18-proc 5 62 1843820 66 66 4 20-proc 4 68 1645581 104400 73 5 20-proc(l) 4 64 1658509 14400 65 6 21-proc 10 78 3923623 453600 82

我想根据这些数据创建一个分组条形图,这样 x 轴包含 Input 字段(作为组),y 轴代表 Rtime 和 Btime 字段(两个条形).

I want to create a grouped bar chart from this data such that x-axis contains Input field (as groups) and y axis represent the log scale for the Rtime and Btime fields (the two bars).

我在网上查看的所有解决方案/示例都将类似的数据放入三列布局中.我不知道如何使用我必须生成分组条形图的数据.或者,如果有办法将这些数据(手动转换不是一种选择,因为它是一个包含很多行的巨大文件)转换为 R 和 ggplot 兼容的数据格式.

All solutions/examples I checked online had similar data put into a three column layout. I do not know how to use the data I have to generate the grouped bar-chart. Or if there is a way to convert this data (manually converting is not an options because it is a huge file with a lot of rows) into a R and ggplot compatible data format.

使用 gncs 解决方案生成的图形

Graph generated using gncs solution

推荐答案

根据要求,一个同样使用 reshape2 的 ggplot2 解决方案:

As requested, a ggplot2 solution that also uses reshape2:

library(reshape2) df <- read.table(text = " Input Rtime Rcost Rsolutions Btime Bcost 1 12-proc. 1 36 614425 40 36 2 15-proc. 1 51 534037 50 51 3 18-proc 5 62 1843820 66 66 4 20-proc 4 68 1645581 104400 73 5 20-proc(l) 4 64 1658509 14400 65 6 21-proc 10 78 3923623 453600 82",header = TRUE,sep = "") dfm <- melt(df[,c('Input','Rtime','Btime')],id.vars = 1) ggplot(dfm,aes(x = Input,y = value)) + geom_bar(aes(fill = variable),stat = "identity",position = "dodge") + scale_y_log10()

注意这里的样式差异,因为 log(1) = 0,ggplot2 将其视为零高度的条并且不绘制任何内容,而 barplot 绘制了一个小存根(在我看来这有点误导).

Note a style difference here, where since log(1) = 0, ggplot2 treats that as a bar of zero height and doesn't plot anything, whereas barplot plots a little stub (which in my opinion is a little misleading).

更多推荐

在 R 中创建多列数据的分组条形图

本文发布于:2023-07-18 00:48:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1139250.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   条形图

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!