Pergunta

Estou procurando uma maneira de adicionar rótulos, ou seja, valores absolutos, em um gráfico de barras empilhadas usando as funções básicas da plotagem de R. Os rótulos devem estar dentro das barras empilhadas.

Obrigada!

Foi útil?

Solução

barplot retornará a posição Mid x das barras, para que você possa fazer

mydata <- matrix(c(10, 21, 22, 33, 45, 23, 22, 43, 33), nrow=3)

# b will contain the x midpoints of the bars
b <- barplot(mydata)

# This will write labels in the middle of the bars, horizontally and vertically
text(b, colMeans(mydata), c("Label1", "Label2", "Label3"))

# This will write labels in the middle of the middle block
text(b, mydata[1,]+mydata[2,]/2, c("LabelA", "LabelB", "LabelC"))

EDITAR: Relendo sua pergunta, acho que é isso que você quer (ou talvez não, mas vou escrever de qualquer maneira: D)

# Find the top y position of each block 
ypos <- apply(mydata, 2, cumsum)
# Move it downwards half the size of each block
ypos <- ypos - mydata/2
ypos <- t(ypos)

text(b, ypos, mydata)

Outras dicas

Que tal a função simples text()?

Você pode simplesmente adicionar uma string onde quiser, por exemplo:

text (x = ..., y = ..., labels = c("foo bar 1000"))

Talvez você possa usar ou inspecionar o Barp função do plotrix pacote

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top