【问题标题】:R: Stacked area chart with line and secondary axisR:带折线和次轴的堆积面积图
【发布时间】:2019-08-08 02:00:24
【问题描述】:

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

这是数据框:

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))

我现在的图表是这样的:

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

之后,我可以细化图表:

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))

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    不确定是否能完全解决整个问题。我的回答是基于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 年 3 月 17 日创建

    【讨论】:

    • 感谢您的第一个建议。然而,辅助轴现在清楚地显示了错误的尺寸,所以我将最后一行更改为scale_y_continuous(sec.axis = sec_axis(name = "n", ~. / normalizer)),这导致轴的尺寸正确。我的基本错误是我不知道我必须在scale_y_continuous 函数中添加sec.axis inside,而不是作为单独的函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-20
    • 1970-01-01
    • 1970-01-01
    • 2012-04-06
    相关资源
    最近更新 更多