Fino
Fino

Reputation: 1784

Label an axis generated by axis() function

I'm plotting a dual axis graph in r base and I can't add a label to the secondary axis. Here's what I'm working with:

dados = structure(list(IDADE = c(65L, 35L, 65L, 42L, 50L, 44L, 0L, 58L, 
                             22L, 27L, 34L, 31L, 0L, 24L, 34L, 20L, 4L, 34L, 20L, 27L, 8L, 
                             2L, 20L, 31L, 45L, 26L, 26L, 40L, 50L, 34L), ESTADO = c(1L, 1L, 
                                                                                     1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 
                                                                                     1L, 1L, 1L, 0L, 1L, 1L, 1L, 1L, 0L, 1L, 0L, 0L), TIPINT = c(3L, 
                                                                                                                                                 1L, 3L, 2L, 3L, 2L, 2L, 3L, 1L, 1L, 1L, 1L, 2L, 1L, 2L, 1L, 2L, 
                                                                                                                                                 3L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 1L, 3L, 2L, 3L, 1L), DIARIAS = c(2L, 
                                                                                                                                                                                                                  2L, 4L, 1L, 1L, 1L, 10L, 1L, 2L, 2L, 2L, 3L, 3L, 1L, 2L, 3L, 
                                                                                                                                                                                                                  6L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 6L, 1L, 1L, 2L)), .Names = c("IDADE", 
                                                                                                                                                                                                                                                                                        "ESTADO", "TIPINT", "DIARIAS"), row.names = c(NA, 30L), class = "data.frame")

#Define boxplot base
bx = boxplot(dados$IDADE~dados$TIPINT,axes=F, xlab=NA,ylab=NA,col=heat.colors(3,0.6))

#Posiciona eixos
par(mar = c(5,5,2,5))

#Plota bar
barplot(table(dados$TIPINT),col=heat.colors(3,0.4),names=c("Tipo 1","Tipo 2","Tipo 3"),
    ylab = "Quantidade de pacientes")

#Plota box
par(new=T)
bx2 = bxp(bx,boxwex = 0.50, at = c(0.9, 2,3.1),axes=F, xlab=NA, ylab="l",boxfill=heat.colors(3,0.7))
axis(side=4)

Which gets me the following graphgraph_example

I'm trying to label the right axis, but axis(side=4,labels="labels") is for something else and axis(side=4,ylab="label") doesn't work.

Thanks

Upvotes: 1

Views: 40

Answers (1)

Esther
Esther

Reputation: 1115

You can use the function mtext to add a label to a secondary axis, using the line=2 argument to move it the appropriate distance from the axis.

mtext("label", 4, line=2)

axis label with mtext

Alternatively you could use just text to have finer control, if say, you want to reorient the label. You can pull the plot dimensions from par("usr") and adjust accordingly.

p <- par("usr")
text(p[2]+0.4, mean(p[3:4]), labels = "label", xpd=NA, srt = -90)

axis label with text

Upvotes: 1

Related Questions