Boxplot - how to rotate x-axis labels to 45°?

Hey all,

R-beginner question!

boxplot(disprt, main = "", xlab = "")

My x-axis labels are pretty long, so they or overlap or they are not shown competely in the plot (las=2).
I would like to rotate them to 35° or 45°, so they are not cut off anymore.
Adding par(srt=45)and theme(axis.text.x=element_text(angle=45)) was not successful and I run out of ideas now.

How is it possible to or rotate the labels or to tell R to show them completely?

Thank you!

This should do it,

Thank you!

I used las=2 before. Results were vertical labels, which are too long and cut off in the output. Therefore I need to know or how to make 35-45° labels or how to show the whole text of the x-axis labels in the output.
And theme just won't work.
I can't run ggplot either:

disprt <- ggplot(aes(x=group, y=distances)) +
  geom_boxplot()

**Error:data must be a data frame, or other object coercible by fortify(), not an S3 object with class uneval
Did you accidentally pass aes() to the data argument?```

You have to pass the data to ggplot2. That is what is causing the error.
Here is an example of rotating the x axis text by 45 degrees. The text spacing is not quite right.

library(ggplot2)
disprt <- data.frame(group = rep(c("AAAAAAAA", "BBBBBBBBBB"), 50), distances = rnorm(100))
ggplot(disprt, aes(group, distances)) + geom_boxplot() +
  theme(axis.text.x = element_text(angle = 45, vjust = 1, hjust = 1))

Created on 2020-11-07 by the reprex package (v0.3.0)

Thank you!

I tried to convert my data into a data frame by dis <- as.data.frame(disprt)
** Error in as.data.frame.default(disprt) :
cannot coerce class ‘"betadisper"’ to a data.frame**

It is a dispersion test and plot with vegan.

I have never used the vegan package. Looking at the documentation, you might be able to make a data frame with

DF <- data.frame(group = disprt$group, distances = disprt$distances)

and then use DF in place of disprt in ggplot().

It is possible to rotate the labels in the boxplot() fucntion. I got this code from https://stats.idre.ucla.edu/r/faq/how-can-i-change-the-angle-of-the-value-labels-on-my-axes/

boxplot(distances ~ group, data = disprt, pars  =  list(xaxt = "n"))
axis(1, at=c(1,2), labels = FALSE)
text(c(1,2), par("usr")[3] - 1, labels = disprt$group, srt = 45, pos = 1, xpd = TRUE)

YES!!! It worked! Thank you!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.