Jacob White
Jacob White

Reputation: 27

facet_grid in ggplot2 returning a graph for (all), how to remove

I am trying to create a facet_grid that contains histograms for measurements for three different measuring methods for three different objects. However, my code returns a fourth "(all)" graph.

How can I remove the (all) graph and return a 3x3 facet_grid with the clavicle, phalanx, and sacrum as the columns, and calipers (cm), ruler (in), and your knuckle as the rows?

The following is a sample data frame and the ggplot2 code to create the histogram facet_grid.

Object <- c("Sacrum", "Clavicle", "Phalanx","Sacrum", "Clavicle", "Phalanx","Sacrum", "Clavicle", "Phalanx","Sacrum", "Clavicle", "Phalanx","Sacrum", "Clavicle", "Phalanx","Sacrum", "Clavicle", "Phalanx")

Object_Length <- c(13.5, 15, 17,5,5,6,4,4,4,10,9.75,13,2.5,9.75,6,4,4,4)

Measuring_Method <- c("Calipers (cm)","Calipers (cm)","Calipers (cm)","Ruler (in)","Ruler (in)","Ruler (in)","Your Knuckle","Your Knuckle","Your Knuckle","Calipers (cm)","Calipers (cm)","Calipers (cm)","Ruler (in)","Ruler (in)","Ruler (in)","Your Knuckle","Your Knuckle","Your Knuckle")

observer_error <- data.frame(Object, Object_Length, Measuring_Method)

library(ggplot2)

ggplot(observer_error, aes(x = Object_Length)) +
  geom_histogram(alpha = 0.5, position = "identity", color = "blue", fill = "blue", bins = 30) +
  facet_grid(Measuring_Method ~ Object, scales = "free", margins = TRUE)

Upvotes: 1

Views: 106

Answers (1)

TarJae
TarJae

Reputation: 79261

Set margins = FALSE in facet_grid:

ggplot(observer_error, aes(x = Object_Length)) +
  geom_histogram(alpha = 0.5, position = "identity", color = "blue", fill = "blue", bins = 30) +
  facet_grid(Measuring_Method ~ Object, scales = "free", margins = FALSE)

enter image description here

Upvotes: 1

Related Questions