R:具有折线和辅助轴的堆积面积图

时间:2019-03-17 15:47:02

标签: r ggplot2

我的目标是制作一个堆叠的面积图,其中的辅助轴引用面积图中以线表示的数据点的案例编号。

这是数据框:

df_prep <- data.frame(year = rep(1990:2017, 2), 
n = rep(c(427, 428, 448, 504, 499, 500, 654, 786, 875, 862, 875, 835, 787, 781, 784, 800, 796, 793, 790, 760, 731, 711, 677, 607, 568, 537, 485, 475), 2), 
values = c(0.04498506, 0.04710173, 0.04807318, 0.04995567, 0.04130670, 0.05112505, 0.04881195, 0.04836573, 0.05010657, 0.04147296, 0.04196852, 0.04856052, 0.05232518, 0.05043970, 0.04506930, 0.03781005, 0.03477578, 0.03190302, 0.03957772, 0.03223425, 0.03292258, 0.03507017, 0.03270658, 0.03248077, 0.02896229, 0.03023061, 0.03281597, 0.03035403, 0.05607519, 0.06112640, 0.06138485, 0.06867888, 0.06051572, 0.06281798, 0.06956896, 0.05799622, 0.05921514, 0.04770948, 0.03884279, 0.05292109, 0.06081987, 0.07052489, 0.05634209, 0.05422748, 0.05292595, 0.07114513, 0.07859847, 0.07719619, 0.08114787, 0.07771912, 0.08199433, 0.08366473, 0.10369285, 0.11383821, 0.11015112, 0.10004794),
type = c(rep("st.debt", 28), rep("lt.debt", 28)))

为进一步准备,我计算了变量nvalues之间的归一化比率:

normalizer <- sum(aggregate(values ~ type, data = df_prep, max)$values / max(df_prep$n))

我当前的图形是:

enter image description here

library(ggplot2)
ggplot() +
geom_area(data = df_prep, aes(y = values, x = year, fill = type)) + 
geom_line(data = df_prep[1:28, ], aes(y = (n * normalizer), x = year)) + 
scale_fill_manual(values = c("grey50", "black")) 

重要的是,最终图形应包括:

  • 两个y轴:第一个(左侧)代表values变量,第二个(右侧)代表n变量;
  • 图例中显示了图中显示的所有三个变量。

我查了几个类似的问题,但无法根据我的问题改编相应的答案。非常感谢您的帮助。

修改:

多亏了amrrs的回答,我才能够修改代码。但是,两个轴现在彼此不一致,所以我首先必须手动更改normalizer

normalizer <- 0.2 / 1000

然后,我可以细化图表:

enter image description here

ggplot() +
geom_area(data = df_prep, aes(y = values, x = year, fill = type)) + 
geom_line(data = df_prep[1:28, ], aes(y = (n * normalizer), x = year, linetype = "n")) + 
scale_fill_manual(values = c("grey50", "black"))  +
scale_y_continuous(name = "% total assets", limits = c(0, 0.2), breaks = seq(0, 0.2, 0.05), labels = seq(0, 20, 5), sec.axis = sec_axis(name =  "n", ~. / normalizer, breaks = seq(0, 1000, 250), labels = seq(0, 1000, 250))) + 
scale_x_continuous(name = "year", breaks = seq(1990, 2017, 3)) +
ggtitle("DEU") + 
theme(plot.title = element_text(hjust = 0.5))

1 个答案:

答案 0 :(得分:0)

不确定是否可以完全解决整个问题。我的答案基于此documentation,部分基于this answer

df_prep <- data.frame(year = rep(1990:2017, 2), 
                      n = rep(c(427, 428, 448, 504, 499, 500, 654, 786, 875, 862, 875, 835, 787, 781, 784, 800, 796, 793, 790, 760, 731, 711, 677, 607, 568, 537, 485, 475), 2), 
                      values = c(0.04498506, 0.04710173, 0.04807318, 0.04995567, 0.04130670, 0.05112505, 0.04881195, 0.04836573, 0.05010657, 0.04147296, 0.04196852, 0.04856052, 0.05232518, 0.05043970, 0.04506930, 0.03781005, 0.03477578, 0.03190302, 0.03957772, 0.03223425, 0.03292258, 0.03507017, 0.03270658, 0.03248077, 0.02896229, 0.03023061, 0.03281597, 0.03035403, 0.05607519, 0.06112640, 0.06138485, 0.06867888, 0.06051572, 0.06281798, 0.06956896, 0.05799622, 0.05921514, 0.04770948, 0.03884279, 0.05292109, 0.06081987, 0.07052489, 0.05634209, 0.05422748, 0.05292595, 0.07114513, 0.07859847, 0.07719619, 0.08114787, 0.07771912, 0.08199433, 0.08366473, 0.10369285, 0.11383821, 0.11015112, 0.10004794),
                      type = c(rep("st.debt", 28), rep("lt.debt", 28)))

normalizer <- sum(aggregate(values ~ type, data = df_prep, max)$values / max(df_prep$n))


library(ggplot2)
ggplot() +
  geom_area(data = df_prep, aes(y = values, x = year, fill = type)) + 
  geom_line(data = df_prep[1:28, ], aes(y = (n * normalizer), x = year, linetype = "n")) + 
  scale_fill_manual(values = c("grey50", "black"))  +
  scale_y_continuous(sec.axis = sec_axis(name =  "n", ~.))

reprex package(v0.2.1)于2019-03-17创建