【发布时间】:2018-10-16 17:07:29
【问题描述】:
以下数据和代码生成堆积条形图。数据由三个国家组成,每个国家有 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
【问题讨论】: