我想创建一个具有三个不同图例的堆叠barplot,用于不同的数据分组。多个传说中的barplot填充ggplot
例如,给出以下数据框:
Year <- as.factor(c(rep(1, 10), rep(2, 10), rep(3,10), rep(4,10), rep(5,10), rep(6,10)))
Category <- as.factor(c(rep("a", 15), rep("b", 10), rep("c", 10), rep("d",10), rep("e", 10), rep("f", 5)))
Region <- as.factor(c(rep("region1", 25), rep("region2", 20), rep("region3",15)))
data <- data.frame(Year, Category, Region)
我想绘制逐年每个类别的计数堆叠barplot。
ggplot() + geom_bar(data=data,aes(x=Year, fill=Category))
然而,而不必为类别(如上面)一个传奇,我想通过地区有三个传说与类别的子集(即标题为“REGION1”会显示“A”类和“b”的传说;标题为“region2”的图例将显示类别“c”和“d”;标题为“region3”的图例将显示类别“e”和“f”。 Legends for multiple fills in ggplot和 R: Custom Legend for Multiple Layer ggplot。 但是,我没有运气将它们应用到一个barplot。任何帮助将不胜感激!
谢谢 - 很欣赏的帮助! – Powege