Say I have a facetted ggplot like this:
data(iris)
ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) +
facet_grid(. ~ Species) +
geom_point()
Question:
Is it possible to align only the 1st facet label ("setosa") to the left, but keep the others central?
I have not had any luck with straight ggplot
, so I tried converting to a gtable (code below), and I can see that the element I probably need to modify is tableGrb #13 - strip-t-1, but I'm not sure how to do so. I tried changing the gt$layout$l
and gt$layout$r
values to 4 (was 5) but that moves the heading off the facet entirely.
pl <- ggplot(iris, aes(x = Petal.Width, y = Sepal.Length)) +
facet_grid(. ~ Species) +
geom_point()
gt <- ggplotGrob(pl)
gt$layout[gt$layout$name == 'strip-t-1', c('l', 'r')] <- c(4, 4) # my attempt
grid.newpage()
grid.draw(gt)
Does anyone know if this is possible?
You have the right idea but not the right spot. Basically you need to do something like this:
But this won't work for you... it won't even work for me a second time since the
titleGrob.xxx
andGRID.text.xxx
change each time you usegt <- ggplotGrob(pl)
So try a different method and knowing the location you need to edit as you have pointed out (it's in the first
strip
):You can use
gPath
to get the path without knowing theGRID.text.xxx
in advance. Note that in your example, we can just edit the firstGRID.text
and it will work ifglobal = FALSE
, ie, ifglobal = TRUE
, all of them would change.However, you might need a very specific path, so follow the
strip-t-1
down to yourGRID.text
(note thatglobal = TRUE
, and it only affects one strip)