r ggplot2使用交互aes修改堆积条形图的x轴标签

时间:2018-10-16 17:07:29

标签: r ggplot2

以下数据和代码生成堆积的条形图。数据包含三个国家,每个国家有6个场景。我想更改两个元素,但不知道如何。

首先,我想拆分列名称的两个部分,它们当前由两个元素(场景和国家)组成。我只想将国家名称的一个实例放在其六个小节的下方。每个栏都应仅标明其方案名称

第二,我想为每个国家/地区在一组酒吧之间留出一些空间。

数据和代码:

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 个答案:

答案 0 :(得分:3)

将区域放在顶部怎么样? facet_wrap(~ region_name)可以解决问题。

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