How to set face axis expressions in ggplot2?

I am trying to set expressions as x axis text in a face environment in ggplot2 with unequal label lengths. For instance:

 dat <- structure(list(Species = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), Individual = structure(c(1L, 2L, 3L, 5L, 1L, 2L, 3L, 4L, 5L), .Label = c("1", "2", "3", "4", "expression(bar(\"x\"))"), class = "factor"), mean = c(45, 32, 100, 59, 65, 110, 87, 93, 88.75), min = c(34, 20, 89, 47.66666667, 54, 100, 67, 85, 76.5), max = c(54, 42, 110, 68.66666667, 76, 120, 99, 105, 100)), .Names = c("Species", "Individual", "mean", "min", "max"), class = "data.frame", row.names = c(NA, -9L)) require(ggplot2) 

enter image description here

This answer describes how to do this without facets. I control the installation of labels , but I cannot find how to set labels for each face separately:

 ggplot(dat, aes(x = Individual, y = mean, ymin = min, ymax = max, color = Species)) + geom_pointrange() + facet_wrap(~Species, scales = "free") + scale_x_discrete(labels = c("1", "2", "3", expression(bar("x")))) + theme_grey(base_size = 18) 

enter image description here

A long vector or list indicating labels does not seem to work:

 ggplot(dat, aes(x = Individual, y = mean, ymin = min, ymax = max, color = Species)) + geom_pointrange() + facet_wrap(~Species, scales = "free") + scale_x_discrete(labels = c("1", "2", "3", expression(bar("x")), "1", "2", "3", "4", expression(bar("x")))) + theme_grey(base_size = 18) 

enter image description here

 ggplot(dat, aes(x = Individual, y = mean, ymin = min, ymax = max, color = Species)) + geom_pointrange() + facet_wrap(~Species, scales = "free") + scale_x_discrete(labels = list(c("1", "2", "3", expression(bar("x")), c("1", "2", "3", "4", expression(bar("x")))))) + theme_grey(base_size = 18) 

enter image description here

Is there a way to do this in ggplot2 ?

+6
source share
2 answers

You can add breaks arguments to scale_x_discrete:

 ggplot(dat, aes(x=Individual, y = mean,ymin = min, ymax = max, color=Species)) + geom_pointrange() + facet_wrap(~Species, scales = "free_x") + scale_x_discrete(breaks=levels(factor(dat$Individual)), labels = c("1", "2", "3", "4", expression(bar("x")))) + theme_grey(base_size = 18) 

enter image description here

+5
source

As a workaround, you can use scales = "free_y" :

 ggplot(dat, aes(x = Individual, y = mean, ymin = min, ymax = max, color = Species)) + geom_pointrange() + facet_wrap(~Species, scales = "free_y") + scale_x_discrete(labels = c("1", "2", "3", "4", expression(bar("x")))) + theme_grey(base_size = 18) 

enter image description here

+2
source

All Articles