I am struggling trying to format the facet_grid of a plot in R. This is a reproducible example of my code with data avaliable from the datasets package in R (ToothGrowth).
library(ggplot2)
df <- ToothGrowth
bp <- ggplot(df, aes(x=dose, y=len, group=dose)) +
geom_boxplot(aes(fill=dose)) +
facet_grid(supp ~ .) +
theme_minimal(base_size = 16) +
theme(legend.position = "none",
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.ticks = element_blank(),
panel.grid.major.x = element_line(size = 0.25,
colour = "grey80"),
strip.text = element_text(size = 22, face = "bold"))
bp
With the code I obtain this first image:
I want to align the labels of the facet_grid to the top of the grid legend, as you can see in the second image:
Thanks.
Add the following to your plot code:
If you want to rotate the labels, you can do:
(Yes, the
vjust
vs.hjust
thing is confusing)