0

Hello I am very new to using coding language and recently made my first couple of figures in R. I used this code to make the figures and they turned out good except that the labels in the x axis were overlapping.

library(ggplot2)
ggplot(LR_density, aes(x=Plant_Lines, y=`Lateral_Root_Density.(root/cm)`, fill=Expression_Type)) + 
    geom_boxplot() +
    geom_jitter(color="black", size=0.4, alpha=0.9) +
    ggtitle("Lateral root density across plant expression types")

The figure produced by the line of code I used

I was wondering if anyone knew how to get the x axis labels to be more spaced out in ggplot2 boxplots. I have been looking around but havent found a clear answer on this. Any help on what to do or where to look would be great!

tjebo
  • 21,977
  • 7
  • 58
  • 94
  • 1
    maybe try rotating the x-axis labels? https://stackoverflow.com/questions/15838533/rotating-axis-labels-in-date-format – StupidWolf Apr 25 '20 at 22:49
  • 2
    ... or increase the width of your output device – user20650 Apr 25 '20 at 22:51
  • you can rotate the axis label by setting the theme `theme(axis.text.x = element_text(angle = ))` – yang Apr 26 '20 at 00:59
  • apparently, since ggplot2 3.3.0 you can also dodge your x labels - https://datavizpyr.com/how-to-dodge-overlapping-text-on-x-axis-labels-in-ggplot2/ I have not yet used this though. I think the best is to size your output device as suggested by @user20650, define the font size accordingly. If the labels still overlap, and you have tried rotating them, you can also flip the plot, so that the labels are better readable on the y axis. – tjebo Apr 26 '20 at 15:56
  • @Tjebo; I think that warrants an answer – user20650 Apr 30 '20 at 11:53
  • 1
    @user20650 Thanks. Answer added! – tjebo Apr 30 '20 at 13:27

1 Answers1

1

As per comment, this thread shows another option to deal with overlapping x axis labels, which one can use since ggplot2 3.3.0

In included a second graph which "squeezes" the axis a bit, which kind of also simulates the effect of changing the viewport/ file size.

library(ggplot2)

ggplot(diamonds, aes(x = cut, y = price)) +
  geom_boxplot() +
  scale_x_discrete(guide = guide_axis(n.dodge = 2)) 


ggplot(diamonds, aes(x = cut, y = price)) +
  geom_boxplot() +
  scale_x_discrete(guide = guide_axis(n.dodge = 2)) +
  coord_fixed(1/10^3.4)

Created on 2020-04-30 by the reprex package (v0.3.0)

tjebo
  • 21,977
  • 7
  • 58
  • 94