I am trying to plot a facet grid of growth plots and my labels at the ends of each plot are overlapping with the each other. Here is sample code using the mpg data:
print(ggplot(data = aggregate(hwy~class+year, data=mpg, mean), aes(x = year, y=hwy))+
geom_line(aes(group = 1))+
geom_point()+
facet_wrap(~class, nrow = 2)+
xlab("Year")+
scale_x_discrete(limits=unique(mpg$year)))
How do I prevent this overlapping, perhaps by moving the tick marks and labels in from the edge of the plot. I tried using the margin within theme, but I did not have success with this either. Thank you for your help.
To avoid overlapping labels in ggplot2, we use guide_axis() within scale_x_discrete().
0 A common problem in making plots, say a barplot or boxplot with a number of groups is that, names of the groups on x-axis label often overlap with each other. Till now, one of the solutions to avoid overlapping text x-axis is to swap x and y axis with coord_flip() and make a horizontal barplot or boxplot.
Stop Labels overlapping chartRight click on the Axis. Choose the Format Axis option. Open the Labels dropdown. For label position change it to 'Low'
To alter the labels on the axis, add the code +labs(y= "y axis name", x = "x axis name") to your line of basic ggplot code. Note: You can also use +labs(title = "Title") which is equivalent to ggtitle .
I presume what you are after is adjusting the horizontal spacing between facet panels with panel.spacing.x
in theme
(tested with ggplot2_3.0.0).
ggplot(data = aggregate(hwy~class+year, data=mpg, mean), aes(x = year, y=hwy))+
geom_line(aes(group = 1))+
geom_point()+
facet_wrap(~class, nrow = 2)+
xlab("Year")+
scale_x_discrete(limits=unique(mpg$year)) +
theme(panel.spacing.x = unit(4, "mm"))
using panel.spacing.x()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With