标记由axis()函数生成的轴

时间:2018-06-04 23:11:13

标签: r

我在r base中绘制双轴图,我无法在辅助轴上添加标签。以下是我正在使用的内容:

out <- unlist(lapply(strsplit(example.stem, "[|]"), function(x){
  lapply(x[2], function(y){
    as.numeric(paste(x[1], unlist(strsplit(y, "")), sep = "."))
  })
}))

> identical(solution, out)
[1] TRUE

它为我提供了以下图表graph_example

我试图标记右轴,但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) 用于其他内容而axis(side=4,labels="labels")无效。

由于

1 个答案:

答案 0 :(得分:1)

您可以使用函数mtext将标签添加到辅助轴,使用line=2参数将其移动到轴的适当距离。

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

axis label with mtext

或者,您可以使用text进行更精细的控制,如果您想要重新定位标签。您可以从par("usr")拉出绘图尺寸并进行相应调整。

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

axis label with text