Set percentage values in bars in ggplot

Asked

Viewed 441 times

1

inserir a descrição da imagem aquiI would like to know how to add the percentage values in the bars of the ggplot graph. On the x axis I was able to add, but not on the bars.

I would like to add in the same percentage format. The code used was this:

SP=data.frame(

  Setores=c("Extrativa mineral", "Indústria de transformação", "Serviços Industr de Utilidade Pública", "Construção Civil","Comércio", "Serviços","Adm. Pública", "Agro., extr. vegetal, caça e pesca"),

  Percentual=c(0.005,0.0399,0.001,0.024,0.3234,0.5769,0,0.028)
)

ggplot(SP, aes(x=Setores, y=Percentual)) +

  geom_bar(aes(fill = Setores), stat="identity") + theme_minimal()+

  coord_flip()+ scale_y_continuous(labels = percent_format())+

  labs(title="Total percentual por setor do trabalho parcial em Minas")

Follow the figure I’ve circled so far.

Thanks.

1 answer

1

Use the geom_text to insert the text and function percent package scales to make the transformation.

library(ggplot2)
library(scales)

SP=data.frame(

  Setores=c("Extrativa mineral", "Indústria de transformação", "Serviços Industr de Utilidade Pública", "Construção Civil","Comércio", "Serviços","Adm. Pública", "Agro., extr. vegetal, caça e pesca"),

  Percentual=c(0.005,0.0399,0.001,0.024,0.3234,0.5769,0,0.028)
)

ggplot(SP, aes(x=Setores, y=Percentual)) +

  geom_bar(aes(fill = Setores), stat="identity") + theme_minimal()+

  coord_flip()+ scale_y_continuous(labels = percent_format())+

  labs(title="Total percentual por setor do trabalho parcial em Minas") +
  geom_text(aes(label = percent(Percentual)),
              position = position_dodge(1),
              hjust = 0)

Browser other questions tagged

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