Joey Bela
Joey Bela

Reputation: 1

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. In total there are 92 regions, divided into 13 functional categories. In addition to these regions belonging to a functional category, they are also identified as either white, gray, or other. Each region is associated with a density value that will be plotted on the y-axis. For Example:

Region Function Matter Density
AMB Cranial Gray 4
bsc Visual White 0.4
LG Visual Gray 30.5
RET Thalamic Gray 25
eml Thalamic White 15
cpt Motor Other 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. 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.

Example Graph

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.

Graph of Region Averages

Graph of Functional Group Averages

Upvotes: 0

Views: 1037

Answers (1)

Colibri
Colibri

Reputation: 792

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") 

Upvotes: 1

Related Questions