Display data labels in Column Chart in R (ggplot2)

I'm new to R and I'm trying to create a bar chart and I can't display the labels of the data on each bar.

Follow link of the data frame used: https://drive.google.com/open?id=1P6RLgzOgnZisI4BUYFHsEZ3b-Ca4XXd-uEg7t-_vASI

I am using the following code:

library(ggplot2)
library(readxl)

Vendas <- read_excel("D:/BDados.xlsx", sheet = "Plan1")

ggplot(Vendas)+
  stat_count(aes(ID_LOC)) +
  labs(title = "Vendas", x="ID_Regioes", y="Total") +
  theme_grey(base_size = 10)

My chart is looking like this:

Graphics

Author: João Martins, 2019-05-24

2 answers

This solution uses the dplyr package to preprocess the data.

library(ggplot2)
library(dplyr)

Vendas %>%
  group_by(ID_LOC) %>%
  summarise(n = n()) %>%
  ggplot(aes(ID_LOC, n, label = n)) +
  geom_col() +
  geom_text(aes(y = n), vjust = -0.1) +
  labs(title = "Vendas", x = "ID_Regioes", y = "Total") +
  theme_grey(base_size = 10)

insert the description of the image here

 1
Author: Rui Barradas, 2019-05-25 07:58:39

You can use tando the geom_text()qunato geom_label(). See the example using geom_text(). First I'll give you a dataframe to use as an example.

library(ggplot2)
vendas = data.frame(total =c(1150, 900, 850, 530, 600),
                    ID_Regioes = c("A", "B", "C", "D", "E"))

vendas %>% ggplot(aes(x = ID_Regioes, y = total)) +
  geom_col() +
  scale_y_continuous(limits = c(0, 1300))+
  geom_text(aes(label = total), 
            vjust = -1) +
  labs(title = "Vendas", x="ID_Regioes", y="total") +
  theme_grey(base_size = 10)

insert the description of the image here

I used scale_y_continuous(limits = c(0, 1300)) to adjust the Y-axis limits.also test as geom_label() to see the difference. I hope I helped.

 4
Author: saulogr, 2019-05-24 19:01:48