'Adjust position of tick marks in ggplot faceted barplot
I have a dataset split into 20 groups (strictly 2 groups with 10 iterations each), 1-10 A and B. Each group has some features that are correlated with dimensions 1-5. So e.g. in group 1A, feature X and Y correlate with dimension 1, and features Z and Q with dimension 3, but in group 2A only feature X correlates with dimension 1 and feature Z and Y correlate with dimension 2.
ETA: the dput output, sorry, not done this before!
structure(list(group = structure(c(2L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), iteration = structure(c(10L,
4L, 10L, 3L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 2L, 4L, 5L, 5L,
6L, 7L, 8L, 9L, 10L, 2L, 2L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 1L,
10L, 10L, 2L, 3L, 5L, 7L, 8L, 9L, 1L, 10L, 10L, 2L, 3L, 4L, 6L,
6L, 9L, 1L, 10L, 2L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 1L,
10L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 1L, 2L, 2L, 3L, 4L, 5L,
7L, 8L, 9L, 9L, 1L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 2L, 3L, 5L), .Label = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10"), class = "factor"),
`group name` = c("B10", "B4", "A10", "A3", "A3", "A4", "A5",
"A6", "A7", "A8", "A9", "B10", "B2", "B4", "B5", "B5", "B6",
"B7", "B8", "B9", "A10", "A2", "A2", "A4", "A5", "A6", "A7",
"A8", "A9", "B1", "B1", "B10", "B10", "B2", "B3", "B5", "B7",
"B8", "B9", "A1", "A10", "A10", "A2", "A3", "A4", "A6", "A6",
"A9", "B1", "B10", "B2", "B2", "B3", "B4", "B5", "B6", "B7",
"B8", "B9", "A1", "A1", "A10", "A2", "A3", "A4", "A5", "A6",
"A7", "A8", "A9", "B1", "B2", "B2", "B3", "B4", "B5", "B7",
"B8", "B9", "B9", "A1", "A3", "A4", "A5", "A6", "A7", "A8",
"A9", "B2", "B3", "B5"), feature = c("AR", "AR", "f0", "f0",
"f0", "f0", "f0", "f0", "f0", "f0", "f0", "f0", "f0", "f0",
"f0", "f0", "f0", "f0", "f0", "f0", "f1", "f1", "f1", "f1",
"f1", "f1", "f1", "f1", "f1", "f1", "f1", "f1", "f1", "f1",
"f1", "f1", "f1", "f1", "f1", "f2", "f2", "f2", "f2", "f2",
"f2", "f2", "f2", "f2", "f2", "f2", "f2", "f2", "f2", "f2",
"f2", "f2", "f2", "f2", "f2", "f3", "f3", "f3", "f3", "f3",
"f3", "f3", "f3", "f3", "f3", "f3", "f3", "f3", "f3", "f3",
"f3", "f3", "f3", "f3", "f3", "f3", "f4", "f4", "f4", "f4",
"f4", "f4", "f4", "f4", "f4", "f4", "f4"), measure = c("AR mean global",
"AR mean global", "f0 mean short term", "f0 mean short term",
"f0 mean short term", "f0 mean short term", "f0 mean short term",
"f0 mean short term", "f0 mean short term", "f0 mean short term",
"f0 mean short term", "f0 mean short term", "f0 mean short term",
"f0 mean short term", "f0 mean short term", "f0 mean short term",
"f0 mean short term", "f0 mean short term", "f0 mean short term",
"f0 mean short term", "f1 mean", "f1 mean", "f1 mean", "f1 mean",
"f1 mean", "f1 mean", "f1 mean", "f1 mean", "f1 mean", "f1 mean",
"f1 mean", "f1 mean", "f1 mean", "f1 mean", "f1 mean", "f1 mean",
"f1 mean", "f1 mean", "f1 mean", "f2 mean", "f2 mean", "f2 mean",
"f2 mean", "f2 mean", "f2 mean", "f2 mean", "f2 mean", "f2 mean",
"f2 mean", "f2 mean", "f2 mean", "f2 mean", "f2 mean", "f2 mean",
"f2 mean", "f2 mean", "f2 mean", "f2 mean", "f2 mean", "f3 mean",
"f3 mean", "f3 mean", "f3 mean", "f3 mean", "f3 mean", "f3 mean",
"f3 mean", "f3 mean", "f3 mean", "f3 mean", "f3 mean", "f3 mean",
"f3 mean", "f3 mean", "f3 mean", "f3 mean", "f3 mean", "f3 mean",
"f3 mean", "f3 mean", "f4 mean", "f4 mean", "f4 mean", "f4 mean",
"f4 mean", "f4 mean", "f4 mean", "f4 mean", "f4 mean", "f4 mean",
"f4 mean"), score = c(0.555, 0.521, 0.782, 0.561, 0.534,
0.539, 0.539, 0.552, 0.768, 0.768, 0.607, 0.581, 0.584, 0.529,
0.546, 0.52, 0.583, 0.523, 0.559, 0.627, 0.55, 0.587, 0.56,
0.581, 0.582, 0.558, 0.623, 0.623, 0.62, 0.635, 0.516, 0.517,
0.571, 0.603, 0.609, 0.522, 0.617, 0.544, 0.543, 0.751, 0.604,
0.546, 0.518, 0.686, 0.665, 0.56, 0.663, 0.566, 0.626, 0.608,
0.61, 0.577, 0.652, 0.677, 0.667, 0.553, 0.588, 0.62, 0.722,
0.586, 0.539, 0.615, 0.602, 0.619, 0.694, 0.528, 0.814, 0.654,
0.654, 0.551, 0.699, 0.609, 0.541, 0.688, 0.546, 0.686, 0.695,
0.532, 0.517, 0.528, 0.615, 0.516, 0.672, 0.521, 0.547, 0.584,
0.584, 0.595, 0.598, 0.591, 0.528), dimension = structure(c(5L,
4L, 4L, 1L, 2L, 1L, 4L, 1L, 5L, 5L, 4L, 1L, 1L, 1L, 1L, 3L,
2L, 3L, 1L, 4L, 1L, 1L, 4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L,
1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 3L, 1L, 1L, 1L,
2L, 4L, 2L, 1L, 1L, 2L, 3L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 2L,
2L, 2L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 2L, 2L,
2L, 3L, 2L, 3L, 2L, 3L, 3L, 5L, 2L, 2L, 2L, 4L, 2L, 3L, 2L
), .Label = c("dim1", "dim2", "dim3", "dim4", "dim5"), class = "factor")), row.names = c(NA,
-91L), class = c("tbl_df", "tbl", "data.frame"))
I've plotted each of these groups and their correlations in barplots, but as you can see in the image linked below, the axis tick for each dimension is really weirdly aligned. I don't understand how they're spaced. I would ideally like an axis tick centred for each set of bars for that dimension, or failing that a separate axis tick for each bar.
Here's the code i've tried:
ggplot(data=corr, aes(x=dimension, y=score)) +
geom_col(aes(fill=feature),width=4,position=position_dodge2(width=1,
preserve=c('total')), na.rm=TRUE) +
facet_wrap(`iteration`~`group`, scales="free_x",nrow=5) +
labs(x="Dimension", y="Correlation Coefficient (Abs. value)")+
theme(axis.text.x = element_text(hjust=0.5))
here's the barplot that comes from that code
Adding scale_x_discrete with breaks named has not helped. At this point I have been dipping in and out of working on this since before Christmas so I'm not sure of everything I've tried, but would appreciate the help!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
