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!

6
  • 1
    maybe try rotating the x-axis labels? stackoverflow.com/questions/15838533/… Commented Apr 25, 2020 at 22:49
  • 2
    ... or increase the width of your output device Commented Apr 25, 2020 at 22:51
  • you can rotate the axis label by setting the theme theme(axis.text.x = element_text(angle = <angle>)) Commented Apr 26, 2020 at 0:59
  • apparently, since ggplot2 3.3.0 you can also dodge your x labels - datavizpyr.com/… 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. Commented Apr 26, 2020 at 15:56
  • 1
    @user20650 Thanks. Answer added! Commented Apr 30, 2020 at 13:27

1 Answer 1

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)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.