how to make single stacked bar chart in ggplot2

YQC
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

Tung

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).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Plotly: How to make stacked bar chart from single trace?

How to created a grouped and stacked bar chart in R with ggplot2

How modify stacked bar chart in ggplot2 so it is diverging

Advanced stacked bar chart ggplot2

How to make a stacked bar chart in matplotlib?

How to make stacked bar chart with annotations

Can you adjust the width of a single stacked bar chart with ggplot2?

ggplot2. How to make geom_bar stacked chart y-range 0-100%?

Single Stacked Bar Chart Matplotlib

How to plot a Stacked and grouped bar chart in ggplot?

How to create a stacked bar chart in r with ggplot

How to control ordering of stacked bar chart using identity on ggplot2

How to organize percentage values on top of a stacked bar chart ggplot2

How to modify the limits of y axis of a stacked bar chart in polar coordinates using ggplot2?

how to change the stacked bar chart using ggplot2 (percentage, sort) in R

How do I reorder a stacked bar chart in ggplot2 by the value of one portion of the stack?

How to make a stacked bar bar chart year over year in Tableau?

How to change the position of stacked stacked bar chart in ggplot in R?

use ggplot2 to make a bar chart

Absolute labels for proportional stacked bar chart in ggplot2

Add percentage labels to stacked bar chart ggplot2

plot confusion matrix as stacked bar chart with ggplot2

reorder stacked bar chart with two levels ggplot2

ggplot2 put labels on a stacked bar chart

Showing data values on stacked bar chart in ggplot2

ggplot2 group and sort for stacked bar chart

Stacked Bar Chart in R using ggplot2 (not possible in excel)

ggplot2: Trim wasted space on stacked horizontal bar chart

Set border in a stacked bar chart using ggplot2