Below is a simplified version of my data. I want to plot the Means using vertical bars with ggplot() + geom_bar. There are a few things I want to do.
I want to separate the data where I have the data grouped vertically (=side by side/beside each other/in columns) according to Vfront (all "front" data on left, all "back" data on right).
I also want to separate the data where I have the data grouped horizontally (on top of each other/in rows) according to Manner (all "Stop" on top row, all "Fric" on bottom row).
Additionally, I want to have the different MedDors grouped per Target (in the NA, cor, incor, X order).
Target Manner Vfront MedDor Mean
p Stop front NA 0.60
p Stop back NA 0.59
p Stop front cor 0.58
p Stop back cor 0.56
p Stop front incor 0.52
p Stop back incor 0.52
p Stop front X 0.24
p Stop back X 0.52
b Stop front NA 0.42
b Stop back NA 0.39
b Stop front cor 0.37
b Stop back cor 0.42
b Stop front incor 0.36
b Stop back incor 0.33
b Stop front X 0.14
b Stop back X 0.39
f Fric front NA 0.27
f Fric back NA 0.23
f Fric front cor 0.19
f Fric back cor 0.40
f Fric front incor 0.13
f Fric back incor 0.32
f Fric front X 0.08
f Fric back X 0.13
v Fric front NA 0.24
v Fric back NA 0.18
v Fric front cor 0.17
v Fric back cor 0.11
v Fric front incor 0.25
v Fric back incor 0.16
v Fric front X 0.09
v Fric back X 0.16
So in total I should get something similar to the grouped data that I have below:
Front Back
pNA pcor pincor cX bNA bcor bincor bX pNA pcor pincor cX bNA bcor bincor bX
p b p b
fNA fcor fincor fX vNA vcor vincor vX fNA fcor fincor fX vNA vcor vincor vX
f v f v
I can do some basic filtering of data, and factoring to get certain orders and I know facet_grid(~Vfront) gives me different columns according to Vfront values (based on my data) but the divisions I am asking about is beyond my current R abilities.
We can use
ggh4x::facet_nestedfor nested facets like you want. If you want a certain order of facets, you need to createfactorcolumns withlevelsthat you desire; however,NAwill always come last, so we need to convertNAto"NA".Update:
If you want the x-axis to have a free scale (like what we get in
facet_wrap), you need to addscales = "free_x", independent = "x"to thefacet_nestedfunction.I have also looked at your previous question, and implemented that solution here (not sure whether you want that or not, but it looks nice).
Created on 2024-02-06 with reprex v2.0.2
If you don't want to use
ggh4x, then you need to usegrid,gtable, and/orpatchworkor other similar libraries, get your individual plots and arrange them in a grid. I personally would spend some more time on figuring out whyggh4xis not working, maybe updating my R version, etc., before going down that route. Good luck!