我需要生成一个包含以下内容的 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 0What 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个值
发布评论