1

I have been attempting to tilt my x axis labels to 45 degrees. Currently when I run the program, all the titles are stacked on top of each other at each axis.

I expected my code to display my x axis labels at a 45 degree angle, one label per tick, not every label on every tick.

boxplot(x ~ y, data=df, pars=list(xaxt="n")  ## boxplot with x axis labels eliminated
axis(1, at=seq(1, 15, by=1), labels=FALSE)  ## x axis ticks with no labels
text(seq(1, 15, by=1), par("usr")[3] - 1, labels=df$name, srt=45, pos=1, xpd=TRUE)  ## x axis labels at 45 degrees, but they are stacked on top of each other.

Do I need to relabel my dataset, specifically in the df$name? Does the labels=df$name have too many names, which stack on top of each other?

This code is from this source: https://stats.oarc.ucla.edu/r/faq/how-can-i-change-the-angle-of-the-value-labels-on-my-axes/

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • 6
    Welcome to Stack Overflow. This question is difficult to answer without seeing the data. Please [make this question reproducible](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) by including a small representative dataset in a plain text format - for example the output from `dput(df)`, if that is not too large. – neilfws Dec 06 '22 at 22:15
  • One problem is that you are subtracting too much from the y-position. The example you cite uses 0.2: `par("usr")[3] - 0.2`. Depending on the length of your labels, you may need to fiddle with the x-position (e.g. `1:15 - .2` or `seq(15) - .2`. – dcarlson Dec 06 '22 at 23:16

1 Answers1

0

You just want the unique elements of the names.

Example, using builtin InsectSprays dataset:

boxplot(count ~ spray, data=InsectSprays, xaxt="n")
sq <- seq_along(unique(InsectSprays$spray))
axis(1, at=sq, labels=FALSE) 
text(sq, par("usr")[3] - 1.5, labels=unique(InsectSprays$spray), 
     srt=45, pos=1, xpd=TRUE)

enter image description here

jay.sf
  • 60,139
  • 8
  • 53
  • 110
  • Thank you. That does address my problem, but my labels now don't correspond with my graphs. The labels have been placed in order as per my dataset, not the order of the boxplots. How can I change this? – Warren McAuley Dec 07 '22 at 20:38