How to place values in faceted bar charts in R?

Asked

Viewed 2,622 times

2

I am trying to generate a bar chart faceted with R, according to data frame and below commands:

   ###############################################
    ######___PACKGES UTILIZADOS_____###############
    ###############################################

    install.packages("plyr")
    library(plyr)

    install.packages("ggplot2")
    library(ggplot2)

    install.packages("ggthemes")
    library(ggthemes)

###############################################
######____DATA FRAME____________###############
###############################################

uf <- c("AC","AC","AC","AC","AC","AC","AC","AC","AM","AM","AM","AM","AM","AM","AM","AM")
da <- c("Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada","Federal", "Estadual", "Municipal", "Privada")
tr <- c(97,99,90.5,78.6,3,1,9.5,21.4,97.2,99.1,96.8,98.7,2.8,0.9,3.2,1.3)
resposta <- c("resposta","resposta","resposta","resposta","não resposta","não resposta","não resposta","não resposta","resposta","resposta","resposta","resposta","não resposta","não resposta","não resposta","não resposta")

taxa <- data.frame(uf, da, tr,resposta)

O primeiro gráfico em facetas é gerado, conforme abaixo.

###############################################
######___BASIC GRAPHICS_____###############
###############################################

g1 <- ggplot() + geom_bar(aes(y = tr, x = da, fill = resposta), data = taxa, 
                          stat="identity")
g1 <- g1 + facet_grid(.~uf)
g1

[![Dependência administrativa por taxa de resposta][1]][1]


Depois eu acrescento os valores dentro das barras, mas ficam desajustados

###############################################
######___Adding data labels_____###############
###############################################

g1 <- g1 + geom_text(data=taxa, aes(x = da, y = tr, label = tr), size=4)
g1 <- g1 + facet_grid(.~uf)
g1

Gráfico de barras com valores desalinhados

Finally, I try to centralize the values, by means of plyr dpply function, as below:

#####################################################
######_Adjusting data labels position_###############
#####################################################

taxa <- ddply(taxa, .(da), transform, pos = cumsum(tr) - (0.5 * tr)) # Ajusta a posição

g1 <- ggplot()+ geom_bar(aes(y = tr, x = da, fill = resposta), data = taxa, 
                          stat="identity") +
                facet_wrap(~uf) +
                geom_text(data=taxa, aes(x = da, y = pos, label = tr), size=4) + 
                theme(legend.position="bottom", legend.direction="horizontal", 
                 legend.title = element_blank())

g1

However, the graph is unnoticed, with values outside the bar and the variable y going up to 200. What is wrong?

Gráfico em barras facetado, mas desconfigurado

Thanks in advance

1 answer

0


What you need to do in this case is to calculate the central position of each column before making the graph and then use this value as y of geom_text. This calculation is simple but can be boring to derive, I copied the formuleta from here.

Using dplyr makes it easier. You need to group by the factors that separate the columns, and then create a new column (using the data frame taxa before the ddply):

taxa %<>% group_by(uf, da) %>%
  mutate(pos = cumsum(tr) - 0.5*tr)

Then it’s just plotting. I tweaked your code ggplot to be more succinct, but it’s essentially the same, except for the change in y of geom_text:

ggplot(taxa, aes(x = da, y = tr)) + 
  geom_bar(aes(fill = resposta), stat = "identity") + 
  geom_text(aes(y = pos, label = tr), size = 4) + 
  facet_grid( . ~ uf) 

inserir a descrição da imagem aqui

PS.: To do this you won’t need the plyr and at no time in this code did you need the ggthemes.

Browser other questions tagged

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