Home > Enterprise >  Creating a stacked bar chart (using data from 2 columns)
Creating a stacked bar chart (using data from 2 columns)

Time:11-03

I am trying to create a stacked bar chart but unable to create the 'stack part'. I wish to have my bar height as the gdp_per_capita column and I then wish to show the gdp_per_capita_agg_percen column as part of each column (this is as a percentage of my gdp_per_capita column). Just to be clearer here for country 1 i need a column value of 3281 then the stack part inside it to be 676 (20.6% of it).

Data and code used below;

data

 df2
   Country_Name            gdp_per_capita     `Agriculture_GDP%`  gdp_per_capita_agg_percen
 1 Albania                         3281               20.6                      676
 2 Algeria                         3515               9.86                      346
 3 Bosnia and Herzegovina          3828               8.21                      314
 4 Croatia                        11285               3.90                      440
 5 Cyprus                         24686               2.60                      643
 6 Egypt, Arab Rep.                2192               13.3                      292 

current code with out stacks; I read about using position="stack" in the geom_bar argument but wasnt sure how to add in my gdp_per_capita_agg_percen data for the stack

ggplot(df2, aes(x = as.factor(Country_Name), y = gdp_per_capita, fill = as.factor(Country_Name)))  
geom_bar(stat = "identity")       

CodePudding user response:

Wouldn't call this stacking. But you could achieve your desired result via a second geom_col/bar:

library(ggplot2)

ggplot(df2, aes(x = as.factor(Country_Name), y = gdp_per_capita, fill = "GDP"))  
  geom_col()  
  geom_col(aes(y = gdp_per_capita_agg_percen, fill = "Agriculture"))   

enter image description here

DATA

structure(list(Country_Name = c("Albania", "Algeria", "Bosnia and Herzegovina", 
"Croatia", "Cyprus", "Egypt, Arab Rep."), gdp_per_capita = c(3281L, 
3515L, 3828L, 11285L, 24686L, 2192L), `Agriculture_GDP%` = c(20.6, 
9.86, 8.21, 3.9, 2.6, 13.3), gdp_per_capita_agg_percen = c(676L, 
346L, 314L, 440L, 643L, 292L)), class = "data.frame", row.names = c("1", 
"2", "3", "4", "5", "6"))

CodePudding user response:

It's not a good idea to have multiple calls to the same geom; that usually means that you needed to reshape your data before plotting it. See below;

library(tidyverse)

df2 %>% 
  select(-`Agriculture_GDP%`) %>% 
  pivot_longer(cols = -"Country_Name") %>% 
ggplot()  
  geom_col(aes(x = as.factor(Country_Name), y = value, fill = name))  
  scale_fill_manual(labels = c("GDP", "Aggriculture"), 
                    values = scales::hue_pal()(2))  
  labs(y = "Country", x = "GDP")

  • Related