2

I have a code that essentially produces a beeswarm plot with a boxplot. The plot p1 looks exactly how I want to, but for p2, I am attempting to facet against the variable in data and would like to have one x axis tick that matches the title of the facet. An example image of my desired output is attached.

data <- structure(list(Sample.Number = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 
11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 2, 3, 4, 5, 6, 7, 
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 2, 3, 4, 
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 1, 
2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 
20), variable = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("Static", "D10 FB", "D12 FB", 
"D14 FB"), class = "factor"), value = c(9.61, 7, 6.59, 6.58, 
6, 5.93, 57.5, 45.5, 39.5, 39, 22.5, 21.5, 128.5, 78.5, 71.5, 
49, 40.5, 40, 36, 35, 390, 478, 298, 524, 474, 406, 478, 1043, 
448, 454, 519, 710, 838, 1481, 737, 305, 668, 1096, 340, 152, 
735, 760, 439, 882, 742, 730, 5923, 3697, 806, 927, 1726, 1436, 
593, 3545, 1669, 814, 1733, 2333, 819, 260, 586, 854, 506, 1067, 
747, 781, 1557, 3807, 1063, 1795, 1534, 2761, 666, 2887, 1737, 
1044, 2211, 2544, 1181, 322), Color = c("black", "black", "black", 
"black", "black", "black", "dodgerblue2", "#E31A1C", "black", 
"#CAB2D6", "#FB9A99", "gold1", "green4", "#6A3D9A", "#FF7F00", 
"black", "palegreen2", "skyblue2", "black", "black", "black", 
"black", "black", "black", "black", "black", "dodgerblue2", "#E31A1C", 
"black", "#CAB2D6", "#FB9A99", "gold1", "green4", "#6A3D9A", 
"#FF7F00", "black", "palegreen2", "skyblue2", "black", "black", 
"black", "black", "black", "black", "black", "black", "dodgerblue2", 
"#E31A1C", "black", "#CAB2D6", "#FB9A99", "gold1", "green4", 
"#6A3D9A", "#FF7F00", "black", "palegreen2", "skyblue2", "black", 
"black", "black", "black", "black", "black", "black", "black", 
"dodgerblue2", "#E31A1C", "black", "#CAB2D6", "#FB9A99", "gold1", 
"green4", "#6A3D9A", "#FF7F00", "black", "palegreen2", "skyblue2", 
"black", "black")), row.names = c(NA, -80L), class = "data.frame")

p1 <- ggplot(data, aes(x = variable, value)) + 
  stat_boxplot(geom ='errorbar', width = 0.5/length(unique(data$variable))) +
  geom_boxplot(outlier.shape = NA, width = 0.5/length(unique(data$variable))) + 
  geom_jitter(aes(color = Color), width = 0.1, size = 2) + 

  scale_color_identity() +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5, size = 20, face = "bold"), 
        axis.title.y = element_text(size = 14, face="bold"), 
        axis.title.x = element_blank(), 
        axis.text.x = element_text(angle = 45, hjust = 1, face = "bold"), 
        axis.text.y = element_text(face = "bold"), 
        panel.background = element_rect(fill = "white"),
        legend.position = "none")
p1


p2 <- p1 + facet_wrap(~variable, scales = "free_y") +
  theme(panel.border = element_rect(fill = NA), 
        strip.background = element_rect(colour = "black", fill = "grey85"), 
        strip.text = element_text(face = "bold"))
p2

enter image description here

1 Answer 1

3

Use "free_x" for scales, and set nrow to one row:

p1 + facet_wrap(~variable, nrow = 1, scales = "free_x") +
  theme(panel.border = element_rect(fill = NA), 
        strip.background = element_rect(colour = "black", fill = "grey85"), 
        strip.text = element_text(face = "bold"))

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I feel dumb. Thanks for the help!
@user13589693 no problems, we all have been there :) good luck.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.