How to remove only some facet labels?
It's not at all perfect, but I'll post this hoping is still better than nothing.
The use of as_labeller()
and labeller()
may get you what you need.
Update
Easiest solution was to split Species
and var
in two labellers
functions.
facet_labeller_top <- function(variable, value) {
c(
"Setosa",
"",
"",
"",
"Versicolor",
"",
"",
"",
"Virginica",
"",
"",
""
)
}
facet_labeller_bottom <- function(variable, value) {
c(
"Petal.Length",
"Petal.Width",
"Sepal.Length",
"Sepal.Width",
"Petal.Length",
"Petal.Width",
"Sepal.Length",
"Sepal.Width",
"Petal.Length",
"Petal.Width",
"Sepal.Length",
"Sepal.Width"
)
}
Result:
ggplot(dat) +
geom_point(aes(x = 1, y = val)) +
facet_wrap(Species~var, labeller = labeller(Species=as_labeller(facet_labeller_top),
var = as_labeller(facet_labeller_bottom)))
Data example:
library(tidyr)
library(ggplot2)
dat <- iris %>%
gather(var, val, Sepal.Length:Petal.Width)
I don't know if I understand well but I will try:
You can use facet_grid instead of facet_wrap
this is the code:
data(iris)
library(tidyr)
library(ggplot2)
dat <- iris %>%
gather(var, val, Sepal.Length:Petal.Width)
ggplot(dat) +
geom_point(aes(x = 1, y = val)) +
facet_grid(Species~var)
This is the result: