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)
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)
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?
– Vitor Hugo
@Vitorhugo Feito, see the edition.
– Rui Barradas
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?
– Vitor Hugo
@Vitorhugo try to
labels = c('A', rep('', nrow(dados) - 1))
. That is, a vector of spaces and letters, with spaces where you want nothing.– Rui Barradas