R:具有n个组的条形图,可堆叠2个值

编程入门 行业动态 更新时间:2024-10-27 03:36:11
本文介绍了R:具有n个组的条形图,可堆叠2个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要生成一个包含以下内容的 barplot :

我有一个包含以下内容的csv文件:类别原因时间值

  • 类别有7个可能的名称(c1 ... c7)
  • 原因有8个可能的名称(n1 ... n8)
  • 时间有2个可能的名称(T1,T2)
  • 值是时间值

示例数据集:

类别原因时间值C1 R1 T1 1C2 R1 T2 2C1 R2 T1 3C2 R2 T2 4C1 R3 T1 8C2 R3 T2 0

我想要实现的目标:条形图由3组组成(每个REASON一组),其中每组由2条堆叠的条形图(即每个CATEGORY的条形图)组成,每个条形图在其顶部都表示T1和T2.

我想我需要类似于

感谢您的帮助.

解决方案

我将向您介绍R中的 ggplot 包,它可能为您提供更好的可视化问题的方法.即使不能解决您当前的问题, ggplot 也是在R中启动可视化效果的最简单方法.

首先输入代码:

库将加载 ggplot 和 scales 包

库(ggplot2)图书馆(秤)

生成您的虚拟数据集

df = expand.grid(factor(c("C1","C2","C3","C4","C5","C6","C6","C8")),因子(c("R1","R2","R3","R4","R5","R6","R7")),因子(c("T1","T2")))

在x轴上以 Category ,在y轴上为 Value ,以 Time 作为堆积条的图,需要 geom_bar().代替对条形图本身进行分组, ggplot 可以使用 facets ,这样可以产生更清晰的结果. scale_y_continuous()将您的y轴转换为百分比.

ggplot(数据= df,aes(x =原因,y =值,填充=时间))+geom_bar(stat ='identity')+facet_wrap(〜类别)+scale_y_continuous(labels = percent)+主题(panel.background = element_rect(fill ="white"))

I need to generate a barplot which consists of the following:

I have a csv file with columns: Category Reason Time Value

  • Category has 7 possible names (c1 ... c7)
  • Reason has 8 possible names (n1 ... n8)
  • Time has 2 possible names (T1, T2)
  • Value is the time value

Example dataset:

Category Reason Time Value C1 R1 T1 1 C2 R1 T2 2 C1 R2 T1 3 C2 R2 T2 4 C1 R3 T1 8 C2 R3 T2 0

What i want to achieve: A bar plot which consists of 3 groups (i.e. one group per REASON), where each group consists of 2 stacked bars (i.e. a bar for each CATEGORY), where each bar depicts T1 and T2 on top of it.

I guess i need something similar as R: bar plot with two groups, of which one is stacked, but unfortunately i'm very new to R.

Similar to this picture, which has in terms of my example:

  • 5 categories
  • 3 reasons
  • 4 times values
  • % as a time value

Any help is appreciated.

解决方案

I am going to introduce you to the ggplot package in R which may give you a better way of visualizing your problem. Even if it does not solve your current problem, ggplot would be the easiest for starting your visualizations in R.

First the code:

library will load ggplot and scales package

library(ggplot2) library(scales)

Generating your dummy data set

df = expand.grid(factor(c("C1","C2","C3","C4","C5","C6","C6","C8")), factor(c("R1","R2","R3","R4","R5","R6","R7")), factor(c("T1","T2")))

Plot with Category on x-axis, Value on y-axis, Time as the stacked bar which needs geom_bar(). Instead of grouping on the bar chart itself, ggplot can use facets, which produces cleaner results. scale_y_continuous() converts your y-axis to percent.

ggplot(data = df, aes(x=Reason, y=Value, fill = Time)) + geom_bar(stat='identity') + facet_wrap(~Category) + scale_y_continuous(labels = percent) + theme(panel.background = element_rect(fill = "white"))

更多推荐

R:具有n个组的条形图,可堆叠2个值

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

发布评论

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

>www.elefans.com

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