[英]How to Plot a Bar Graph with Two Categorical Variables on X-Axis (one nested by the other, but both visible), and other variables as fill?
I have a dataset with two categorical variables, region and function .我有一个包含两个分类变量region和function的数据集。 In total there are 92 regions, divided into 13 functional categories.共有92个区域,分为13个功能类别。 In addition to these regions belonging to a functional category, they are also identified as either white , gray , or other .除了属于功能类别的这些区域之外,它们还被标识为white 、 gray或other 。 Each region is associated with a density value that will be plotted on the y-axis.每个区域都与一个密度值相关联,该密度值将绘制在 y 轴上。 For Example:例如:
Region地区 | Function Function | Matter事情 | Density密度 |
---|---|---|---|
AMB AMB | Cranial颅骨 | Gray灰色的 | 4 4个 |
bsc平衡计分卡 | Visual视觉的 | White白色的 | 0.4 0.4 |
LG LG | Visual视觉的 | Gray灰色的 | 30.5 30.5 |
RET休息时间 | Thalamic丘脑 | Gray灰色的 | 25 25 |
eml电子邮件 | Thalamic丘脑 | White白色的 | 15 15 |
cpt cpt | Motor马达 | Other其他 | 5 5个 |
I wish to create a graph similar to this, where the year dates at the top would be my functional categories, the x-axis labels would be my region names, and the bar fill would denote white/gray.我希望创建一个与此类似的图表,其中顶部的年份日期将是我的功能类别,x 轴标签将是我的区域名称,填充条将表示白色/灰色。 However, an important distinction is I would want all of my 92 regions to be present along the x-axis once, divided into functional groups.然而,一个重要的区别是我希望我的所有 92 个区域都沿着 x 轴出现一次,分为功能组。
I've created graphs showing just the total averages of the regions within my functional groups as well as a graph of all regions averages separately.我创建了仅显示我的功能组内区域的总平均值的图表,以及单独显示所有区域平均值的图表。 I would like to create something similar, but with all the functional group's associated regions present in the graph and grouped by function. Essential a merger between these two types of graphs, in addition to a fill variable for the bars.我想创建一些类似的东西,但所有功能组的相关区域都出现在图中并按 function 分组。除了条形的填充变量之外,这两种类型的图之间的必要合并。
Graph of Region Averages区域平均值图
Graph of Functional Group Averages功能组平均值图
This is a pretty barebones question as I am fairly new to R and stackoverflow.这是一个非常简单的问题,因为我对 R 和 stackoverflow 还很陌生。 I will be busy today but can provide any additional information necessary as soon as I am able to.我今天会很忙,但可以尽快提供任何必要的额外信息。 Any help is appreciated.任何帮助表示赞赏。 Thank you!谢谢!
Six data is not much.六个数据不多。 I don't have an example to pull a good example from, but maybe you're looking for something like that:我没有一个例子可以从中得出一个很好的例子,但也许你正在寻找这样的东西:
library(ggplot2)
ggplot(data, aes(Density, fill= Matter)) +
geom_bar(width=.5) +
facet_grid(~Function + Region, scales="free_x", space="free")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.