Adrian
Adrian

Reputation: 9803

How to make single stacked bar chart in ggplot2?

Ancestry <- data.frame(Race = c("European", "African American", "Asian", "Hispanic", "Other"), Proportion = c(40, 30, 10, 15, 5))
Ancestry %>% 
ggplot(aes(y = Proportion, fill = Race)) + 
  geom_bar(stat="identity", colour="white")

Running the above code gives me the following error:

Warning in min(x, na.rm = na.rm) :
  no non-missing arguments to min; returning Inf
Warning in max(x, na.rm = na.rm) :
  no non-missing arguments to max; returning -Inf
Warning in min(diff(sort(x))) :
  no non-missing arguments to min; returning Inf
Warning in x - width/2 :
  longer object length is not a multiple of shorter object length
Warning in x + width/2 :
  longer object length is not a multiple of shorter object length
Error in data.frame(list(y = c(40, 30, 10, 15, 5), fill = c(3L, 1L, 2L,  : 
  arguments imply differing number of rows: 5, 2947

I'm looking to create a stacked barchart similar to this:

enter image description here

Upvotes: 16

Views: 17748

Answers (3)

Geek On Acid
Geek On Acid

Reputation: 6410

You don't need to create a dummy grouping variable for x-axis to create a single stacked bar. You can just add an empty string within aes() for specific axis. Also, theme_blank() removes all unnecessary grid keeping it tidy.

library(tidyverse)
Ancestry <- tibble(Race = c("European", "African American", "Asian", "Hispanic", "Other"), 
                   Proportion = c(40, 30, 10, 15, 5))
ggplot(Ancestry, aes(x = "", y = Proportion, fill = Race)) +
       geom_col() +
       geom_text(aes(label = paste0(Proportion, "%")), 
                     position = position_stack(vjust = 0.5)) +
       scale_fill_brewer(palette = "Set2") +
       theme_blank() +
       labs(x="", y="Percentage")

Single line stacked barchart

Upvotes: 1

Tung
Tung

Reputation: 28451

You need to create a dummy variable for x-axis. Then use geom_col which is similar to geom_bar(stat = "identity") to plot the stacked barplot + geom_text to put the text on the bar.

The plot you showed used theme_economist from the ggthemes package.

library(tidyverse)

Ancestry <- data.frame(Race = c("European", "African American", "Asian", "Hispanic", "Other"), 
                       Proportion = c(40, 30, 10, 15, 5))

Ancestry <- Ancestry %>% 
  mutate(Year = "2006")

ggplot(Ancestry, aes(x = Year, y = Proportion, fill = Race)) +
  geom_col() +
  geom_text(aes(label = paste0(Proportion, "%")),
            position = position_stack(vjust = 0.5)) +
  scale_fill_brewer(palette = "Set2") +
  theme_minimal(base_size = 16) +
  ylab("Percentage") +
  xlab(NULL)

library(ggthemes)

ggplot(Ancestry, aes(x = Year, y = Proportion, fill = Race)) +
  geom_col() +
  geom_text(aes(label = paste0(Proportion, "%")),
            position = position_stack(vjust = 0.5)) +
  theme_economist(base_size = 14) +
  scale_fill_economist() +
  theme(legend.position = "right", 
        legend.title = element_blank()) +
  theme(axis.title.y = element_text(margin = margin(r = 20))) +
  ylab("Percentage") +
  xlab(NULL)

Created on 2018-08-26 by the reprex package (v0.2.0.9000).

Upvotes: 17

Ronak Shah
Ronak Shah

Reputation: 389325

We can create a dummy variable to group all enteries into one group and then use geom_bar to create a stacked bar plot and use geom_text to name the Proportions.

library(ggplot2)

#Dummy group variable
Ancestry$row <- 1

#Create a label variable
Ancestry$percent <- (Ancestry$Proportion/sum(Ancestry$Proportion)) * 100

ggplot(Ancestry, aes(x = row,y = Proportion, fill = Race)) +
    geom_bar(stat="identity") + 
    geom_text(aes(label = percent), 
        position = position_stack(vjust = 0.5))

enter image description here

Upvotes: 5

Related Questions