1
votes

The following data and code produce a stacked bar chart. The data consist of three countries, and for each country, 6 scenarios. I want to change two elements but can't figure out how.

First, I'd like to split up the two components of the column name, which currently consist of the two elements (scenario and country). I'd like to put just one instance of the country name below its six bars. Each bar should be labeled with just its scenario name

Second, I'd like to put a bit of space between the set of bars for each country.

The data and code:

library(data.table)
library(ggplot2)
plotTitle <- "Small data set"
temp <- structure(list(
  scenario = c("Highpes_CC", "Highpes_CC", "Highpes_CC", "Highpes_CC", "Highpes_CC", "Highpes_CC", 
               "Highpes_CC", "Highpes_CC", "Highpes_CC", "Lowopt_CC", 
               "Lowopt_CC", "Lowopt_CC", "Lowopt_CC", "Lowopt_CC", 
               "Lowopt_CC", "Lowopt_CC", "Lowopt_CC", "Lowopt_CC", 
               "2010", "2010", "2010", "Med_base_CC", "Med_base_CC", "Med_base_CC", 
               "2010", "2010", "2010", "Med_base_CC", "Med_base_CC", "Med_base_CC", 
               "2010", "2010", "2010", "Med_base_CC", "Med_base_CC", "Med_base_CC", 
               "Med_base_NoCC", "Med_base_NoCC", "Med_base_NoCC", "Med_base_NoCC", 
               "Med_base_NoCC", "Med_base_NoCC", "Med_base_NoCC", "Med_base_NoCC", 
               "Med_base_NoCC", "Med_opt_CC", "Med_opt_CC", "Med_opt_CC", "Med_opt_CC", 
               "Med_opt_CC", "Med_opt_CC", "Med_opt_CC", "Med_opt_CC", "Med_opt_CC", 
               "Med_pes_CC", "Med_pes_CC", "Med_pes_CC", "Med_pes_CC", "Med_pes_CC", 
               "Med_pes_CC", "Med_pes_CC", "Med_pes_CC", "Med_pes_CC"), 
  nutrient = c("carb", 
               "fat", "protein", "carb", "fat", "protein", "carb", 
               "fat", "protein", "carb", "fat", "protein", "carb", 
               "fat", "protein", "carb", "fat", "protein", "carb", 
               "fat", "protein", "carb", "fat", "protein", "carb", 
               "fat", "protein", "carb", "fat", "protein", "carb", 
               "fat", "protein", "carb", "fat", "protein", "carb", 
               "fat", "protein", "carb", "fat", "protein", "carb", 
               "fat", "protein", "carb", "fat", "protein", "carb", 
               "fat", "protein", "carb", "fat", "protein", "carb", 
               "fat", "protein", "carb", "fat", "protein", "carb", 
               "fat", "protein"), 
  value = c(1386.9, 473.8, 230.6, 1658.4, 300.8, 
            349.2, 2000.7, 654.1, 289.2, 1597.8, 560.8, 272.1, 1867.9, 339.6, 
            398.9, 2165.6, 722.2, 318.4, 1413.7, 425.6, 221.6, 1490.4, 515.5, 
            250.7, 1509.5, 247.2, 289.1, 1761.7, 319.7, 373.6, 2106, 625.5, 
            286.6, 2082.6, 687.4, 303.6, 1624.1, 540.9, 269.1, 1870.2, 334.7, 
            392.1, 2241.7, 726.4, 328.2, 1517.1, 530.4, 256.4, 1783.5, 325.3, 
            379.6, 2096.1, 699.2, 306.9, 1459.8, 499.2, 244.3, 1735.7, 313.4, 
            366.5, 2066.2, 674.5, 299.8), 
  region_name = c("Bots", "Bots", "Bots", "Eth", "Eth", "Eth", "Nigeria", 
                  "Nigeria", "Nigeria", "Bots", "Bots", "Bots", "Eth", 
                  "Eth", "Eth", "Nigeria", "Nigeria", "Nigeria", "Bots", 
                  "Bots", "Bots", "Bots", "Bots", "Bots", "Eth", 
                  "Eth", "Eth", "Eth", "Eth", "Eth", "Nigeria", 
                  "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Nigeria", "Bots", 
                  "Bots", "Bots", "Eth", "Eth", "Eth", "Nigeria", 
                  "Nigeria", "Nigeria", "Bots", "Bots", "Bots", "Eth", 
                  "Eth", "Eth", "Nigeria", "Nigeria", "Nigeria", "Bots", 
                  "Bots", "Bots", "Eth", "Eth", "Eth", "Nigeria", 
                  "Nigeria", "Nigeria")), class = c("data.table", "data.frame"), 
  row.names = c(NA, -63L))

p <- ggplot(data = temp, aes(interaction(scenario,region_name), y = value, fill = nutrient, levels =  region_name, position_dodge(preserve = "total"))) +
  geom_bar(stat = "identity", position = "stack", color = "black", width = .80, group = "region_name") +
  theme(legend.position = "right") +
  labs(x = NULL, y = yLab) +
  theme(axis.text.x = element_text(angle = 70, hjust = 1, family = fontFamily, face = "plain")) +
  theme(axis.title.y = element_text(family = fontFamily, face = "plain")) +
  scale_fill_manual(values = colorList) +
  theme(plot.title = element_text(hjust = 0.5, size = 11, family = fontFamily, face = "plain")) +
  ggtitle(plotTitle) 
p
1

1 Answers

3
votes

How about put the region on top? facet_wrap(~ region_name) will do the trick.

p <- ggplot(data=temp, aes(scenario, y = value, fill = nutrient, levels =  region_name, position_dodge(preserve = "total"))) +
  geom_bar(stat = "identity", position = "stack", color = "black", width = .80, group = "region_name") +
  facet_wrap(~ region_name)+
  theme(legend.position = "right") +
  labs(x = NULL, y = "Y label") +
  theme(axis.text.x = element_text(angle = 70, hjust = 1, family = fontFamily, face = "plain")) +
  theme(axis.title.y = element_text(family = fontFamily, face = "plain")) +
  #scale_fill_manual(values = colorList) +
  theme(plot.title = element_text(hjust = 0.5, size = 11, family = fontFamily, face = "plain")) +
  ggtitle(plotTitle) 

p

enter image description here