user13589693
user13589693

Reputation: 369

Facet of ggplot showing single variable

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

Upvotes: 2

Views: 115

Answers (1)

zx8754
zx8754

Reputation: 56004

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

Upvotes: 3

Related Questions