ggplot2 barplot labels using counts

Manojit :

In the code below, how do I add label to each bar showing its count? Thanks

library(ggplot2)
data(iris)
ggplot(iris) +
    geom_bar(aes_string(x="Species"), fill="steelblue") +
#   geom_text(aes(label="Species")) +          # does not work
    theme(axis.text.y = element_blank(),
          axis.ticks.y = element_blank(),
          axis.title.y = element_blank()
         )

enter image description here

Duck :

You can try this:

library(tidyverse)
#Data
data(iris)
#Plot
ggplot(iris) +
  geom_bar(aes_string(x="Species"), fill="steelblue") +
  geom_text(data= iris %>% group_by(Species) %>% summarise(N=n()),
            aes(x=Species,y=N,label=N),position = position_stack(vjust = 0.5))+
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank()
  )

Output:

enter image description here

You can also try a different position for labels with this:

ggplot(iris) +
  geom_bar(aes_string(x="Species"), fill="steelblue") +
  geom_text(data= iris %>% group_by(Species) %>% summarise(N=n()),
            aes(x=Species,y=N,label=N),position = position_dodge(width = 0.9),vjust=-0.5)+
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank(),
        axis.title.y = element_blank()
  )

Output:

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Stacked Barplot with Frequency Counts ggplot2

Add text on top of a facet dodged barplot using ggplot2

barplot labels in r: issues with displaying rotated labels using text()

automagically using labels (haven semantics) in ggplot2 plots

Stacked barplot in ggplot2: print labels once instead of twice

R ggplot2 stacked barplot, percent on y axis, counts in bars

Using lapply to pass labels to ggplot2

plot counts with intervals in x axis using ggplot2

ggplot2 - How do I add proportion labels to stacked proportion barplot?

Align labels to pyramid barplot in ggplot2

Stacked barplot using ggplot2 - data visualisation

r ggplot2 barplot groups labels don't stack

Using factor levels as Labels in barplot

Mantain order of dataframe for a stacked barplot using ggplot2

ggplot2 barplot

using ggplot2 to show multiple number columns in barplot?

draw barplot with different color with labels, using ggplot

Multiple barplot using ggplot2

5-dimensional stacked barplot using ggplot2

Legend in ggplot2 - using factor labels

ggplot2: Value labels in stacked barplot with geom_bar(position = 'fill')

Modifying text labels on top ggplot percentage barplot

Adding labels to percentage stacked barplot ggplot2

Barplot using ggplot2 for 4 variables

How do I create a non-stacked barplot with data labels using ggplot2 in R?

How to change colour for barplot in ggplot2 using viridis package?

Change labels in a barplot with a ggplot

How to plot a barplot using ggplot2

Add line with secondary axis to barplot using ggplot2