I have created a plot like the one here with ggplot2
package and facet_wrap
function, and I would like to suppress some of the x-axis text to make it more legible.
For example, here it would be more legible if the x-axis scales appeared only on boxes D, F, H and J.
How could I do that? Thanks in advance!
EDIT : the reproducible code
library(ggplot2)
d <- ggplot(diamonds, aes(carat, price, fill = ..density..)) +
xlim(0, 2) + stat_binhex(na.rm = TRUE) + theme(aspect.ratio = 1)
d + facet_wrap(~ color, nrow = 1)
If you're willing to work at the grid/grob-level, it's definitely doable.
First, we assign a ggplot object with your faceted plot
Then, we load up gtable so we can use/manipulate the lower-level objects.
Now, we extract the ggplot object into a TableGrob (apologies for the long-ish output, but I think it helps show the underlying structure of the facet plots):
My negating-look-ahead-regex-fu is not working this morning, so if anyone with a shorter regex for this could edit it or comment that'd be awesome. Basically, we're filtering out the x-axis elements you don't want (print it again locally to see what's gone).
And, now we do the actual plotting:
Is there a greater reason, why you decided to only display the plots in one row? Just set the parameter nrow = 2 of
facet_wrap()
and turn the x-axis text: changetheme(aspect.ratio=1)
totheme(aspect.ratio = 1,axis.text.x = element_text(angle = 90, hjust = 0,vjust=.5))
So the result would look like this:
I know that might not answer your Question excactly, but for my understanding I thought you might be looking forward to make the plot less overloaded.
Does this work for you?
This answer has
gtable_filter_remove()
function for simple negative subsetting. Based on the output fromp_tab$layout$name
, we would have to removeaxis-b-2-1
,axis-b-4-1
andaxis-b-6-1
to get what we want.Created on 2018-08-26 by the reprex package (v0.2.0.9000).