Rotate names on x-axis

Asked

Viewed 250 times

2

Good afternoon how do I plot the graph generated by the code below so that the treatments name V10RC4, V15RC3, V20RC2 and V25RC1 appear diagonally?

barplot(1:4, names.arg = c("V10RC4","V15RC3", "V20RC2", "V25RC1"), las=2)

The las=2 command displays the treatments vertically. Is there any other command I can use?

1 answer

4

The trick is not to plot the axis in question, in this case the axis of x with xaxt = "n" and then use the return value of barplot to annotate the axle with text. Note the argument par('usr')[3], is he who gives the correct position of labels. The angle is given by srt = 45.

labels <- c("V10RC4","V15RC3", "V20RC2", "V25RC1")
bp <- barplot(1:4, xaxt = "n", axisnames = FALSE)
text(bp, par('usr')[3], labels = labels, 
     srt = 45, adj = c(1.1,1.1), xpd = TRUE, cex = 0.9)

inserir a descrição da imagem aqui

Editing.

To reply to the request to have letters on top of the bars, in comment, the function is once again used text with the return value bp above as coordinates of the axis of x.

The most complicated part is to be careful of the length of the axis of the y. To increase it enough that you can see all the letters, I use the function strheight. This function gives an approximate value of the height of the letter’M'. Below the code duplicates this value and adds the maximum of the bar values.

bp <- barplot(dados$values, xaxt = "n", axisnames = FALSE,
              ylim = c(0, max(dados$values) + 2*strheight('M')))
text(bp, dados$values, labels = LETTERS[1:nrow(dados)], pos = 3)
text(bp, par('usr')[3], labels = dados$labels, 
     srt = 45, adj = c(1.1, 1.1), xpd = TRUE, cex = 0.9)

inserir a descrição da imagem aqui

Code to create data.

set.seed(123)
values <- sample(10, 4, TRUE)
labels <- c("V10RC4","V15RC3", "V20RC2", "V25RC1")
dados <- data.frame(values, labels)
  • Thanks worked out. Rui you know how to insert letters on top of the bars of the chart?

  • @Vitorhugo Feito, see the edition.

  • What I modify in the code to insert only one letter in a single bar. For example insert the letter A in the bar for V25C1 and the others leave no letters?

  • @Vitorhugo try to labels = c('A', rep('', nrow(dados) - 1)). That is, a vector of spaces and letters, with spaces where you want nothing.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.