Bar graph text stacked in R

Asked

Viewed 2,625 times

3

I want to create a stacked bar chart similar to the one below

inserir a descrição da imagem aqui

I used the following command

barplot(t(dadosAQI),
        xlab="%",
        col=colAQI,
        horiz = TRUE,
        cex.axis = 0.8,
        cex.names = 0.8,
        las=1
        ) 

But I can’t add the text inside the bars. How to do this?

  • Rodrigo, you can post a reproducible part of your data using dput? You will probably need to use the command text and select position x based on data values.

  • @Moix, I tried to save the result of the barplot to use with the text, it was not efficient. The output of dput was: Structure(list(TMV = c(33.5621055191536, 15.6754220751383, 36.7551724422255, 16.3251895118863), TCF = c(65.562027434103, 83.015129105287, 62.0678120301, 82.3751677678634), Tcz = c(0.875867046743407, 1.30944881957463, 1.1770155067444, 1.29964272025029)), . Names = c("TMV", "TCF", "Tcz"), Row.Names = c("430°C - 1°C/min", "430°C - 5°C/min", "530°C - 1°C/min", "530°C - 5°C/min"), class = "frame date.")

2 answers

2


To do this using barplot() and text(), you need to extract the x and y positions of each value you want to plot.

  • For the values of y, we will use the value returned by the function itself barplot
  • For the values of x, we have to put each value in the center of its bar. So, the position is half the value + the values of the bars on the left.

I only made it for the first two groups, because obviously the third would not fit. But you can edit x manually to choose a position that you like. I also removed the argument col since you did not put your definition, but just enter again.

Complete code:

dadosAQI <- structure(list(TMV = c(33.5621055191536, 15.6754220751383, 36.7551724422255, 16.3251895118863), TCF = c(65.562027434103, 83.015129105287, 62.0678120510301, 82.3751677678634), TCz = c(0.875867046743407, 1.30944881957463, 1.1770155067444, 1.29964272025029)), .Names = c("TMV", "TCF", "TCz"), row.names = c("430°C - 1°C/min", "430°C - 5°C/min", "530°C - 1°C/min", "530°C - 5°C/min"), class = "data.frame") 

par(mar=c(5,7,2,2))
bp <- barplot(t(dadosAQI), xlab="%", horiz = TRUE,
        cex.axis = 0.8, cex.names = 0.8, las=1) 

xpos <- c(dadosAQI[,1]/2, dadosAQI[,1] + dadosAQI[,2]/2)

text(x = xpos, y = bp, label = round(unlist(dadosAQI[,1:2]), 2)) #Temos que arredondar para não plotar várias casas decimais.

Upshot:

inserir a descrição da imagem aqui

2

You can try this using the ggplot2. I made an example that you can try to adapt to your data:

1. Creating the database:

dados <- data.frame(
  nomes = c("A", "A", "B", "B"),
  tipo = c("C","D","C","D"),
  valor = c(10,5,10,5),
  posicao = c(12,7,12,7)
)

2. Graph:

ggplot(dados, aes(x = nomes, y = valor, fill = tipo)) + 
  geom_bar(stat = "identity") + 
  geom_text(aes(y = posicao, label = nomes), colour = "white") +
  coord_flip()

Note that the column position in the database is the one that indicates the beginning of the text. In your case, you may need to adjust it manually.

inserir a descrição da imagem aqui

Browser other questions tagged

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