如何在 R 中正确创建具有百分比刻度和多行的辅助 y 轴?

问题描述 投票:0回答:1

这个问题可能是重复的。对此感到抱歉。然而,所提供的解决方案都不适用于我的情况。

在主轴上,我需要将每个月的 broj 总和作为条形图。 在辅助轴上,我需要将每个月的每个 obdukcija 的百分比作为一条线。然而,很难为辅助 y 轴创建适当的限制。

data_df <- structure(
  list(
    year_month = c(
      "2017-01",
      "2017-01",
      "2017-01",
      "2017-01",
      "2017-02",
      "2017-02",
      "2017-02",
      "2017-02",
      "2017-03",
      "2017-03",
      "2017-03",
      "2017-03",
      "2017-04",
      "2017-04",
      "2017-04",
      "2017-04",
      "2017-05",
      "2017-05",
      "2017-05",
      "2017-05",
      "2017-06",
      "2017-06",
      "2017-06",
      "2017-06",
      "2017-07",
      "2017-07",
      "2017-07",
      "2017-07",
      "2017-08",
      "2017-08",
      "2017-08",
      "2017-08",
      "2017-09",
      "2017-09",
      "2017-09",
      "2017-09",
      "2017-10",
      "2017-10",
      "2017-10",
      "2017-10",
      "2017-11",
      "2017-11",
      "2017-11",
      "2017-11",
      "2017-12",
      "2017-12",
      "2017-12",
      "2017-12",
      "2018-01",
      "2018-01",
      "2018-01",
      "2018-01",
      "2018-02",
      "2018-02",
      "2018-02",
      "2018-02",
      "2018-03",
      "2018-03",
      "2018-03",
      "2018-03",
      "2018-04",
      "2018-04",
      "2018-04",
      "2018-04",
      "2018-05",
      "2018-05",
      "2018-05",
      "2018-05",
      "2018-06",
      "2018-06",
      "2018-06",
      "2018-06",
      "2018-07",
      "2018-07",
      "2018-07",
      "2018-07",
      "2018-08",
      "2018-08",
      "2018-08",
      "2018-08",
      "2018-09",
      "2018-09",
      "2018-09",
      "2018-09",
      "2018-10",
      "2018-10",
      "2018-10",
      "2018-10",
      "2018-11",
      "2018-11",
      "2018-11",
      "2018-11",
      "2018-12",
      "2018-12",
      "2018-12",
      "2018-12",
      "2019-01",
      "2019-01",
      "2019-01",
      "2019-01",
      "2019-02",
      "2019-02",
      "2019-02",
      "2019-02",
      "2019-03",
      "2019-03",
      "2019-03",
      "2019-03",
      "2019-04",
      "2019-04",
      "2019-04",
      "2019-04",
      "2019-05",
      "2019-05",
      "2019-05",
      "2019-05",
      "2019-06",
      "2019-06",
      "2019-06",
      "2019-06",
      "2019-07",
      "2019-07",
      "2019-07",
      "2019-07",
      "2019-08",
      "2019-08",
      "2019-08",
      "2019-08",
      "2019-09",
      "2019-09",
      "2019-09",
      "2019-09",
      "2019-10",
      "2019-10",
      "2019-10",
      "2019-10",
      "2019-11",
      "2019-11",
      "2019-11",
      "2019-11",
      "2019-12",
      "2019-12",
      "2019-12",
      "2019-12"
    ),
    obdukcija = c(
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne",
      "Da - klini�\u008dka",
      "Da - leš je oslobo�\u0091en obdukcije",
      "Da - sudsko-medicinska",
      "Ne"
    ),
    broj = c(
      78L,
      32L,
      20L,
      96L,
      46L,
      26L,
      17L,
      57L,
      44L,
      22L,
      20L,
      78L,
      42L,
      24L,
      27L,
      76L,
      44L,
      25L,
      21L,
      97L,
      37L,
      20L,
      13L,
      69L,
      44L,
      21L,
      19L,
      66L,
      43L,
      27L,
      27L,
      91L,
      39L,
      20L,
      19L,
      67L,
      48L,
      30L,
      13L,
      85L,
      43L,
      28L,
      16L,
      80L,
      40L,
      30L,
      23L,
      98L,
      50L,
      12L,
      31L,
      101L,
      32L,
      11L,
      28L,
      74L,
      41L,
      7L,
      36L,
      101L,
      40L,
      15L,
      36L,
      95L,
      45L,
      9L,
      29L,
      76L,
      33L,
      9L,
      26L,
      79L,
      47L,
      10L,
      34L,
      73L,
      47L,
      13L,
      29L,
      59L,
      38L,
      4L,
      30L,
      67L,
      37L,
      6L,
      32L,
      64L,
      37L,
      10L,
      25L,
      77L,
      33L,
      21L,
      13L,
      90L,
      55L,
      8L,
      29L,
      99L,
      34L,
      9L,
      36L,
      90L,
      49L,
      3L,
      26L,
      104L,
      34L,
      10L,
      26L,
      74L,
      27L,
      9L,
      34L,
      113L,
      36L,
      6L,
      20L,
      96L,
      27L,
      8L,
      19L,
      109L,
      33L,
      17L,
      28L,
      132L,
      39L,
      14L,
      28L,
      140L,
      35L,
      23L,
      26L,
      178L,
      35L,
      10L,
      19L,
      180L,
      56L,
      21L,
      16L,
      158L
    ),
    percent = c(
      0.345132743362832,
      0.141592920353982,
      0.0884955752212389,
      0.424778761061947,
      0.315068493150685,
      0.178082191780822,
      0.116438356164384,
      0.39041095890411,
      0.268292682926829,
      0.134146341463415,
      0.121951219512195,
      0.475609756097561,
      0.248520710059172,
      0.142011834319527,
      0.159763313609467,
      0.449704142011834,
      0.235294117647059,
      0.133689839572193,
      0.112299465240642,
      0.518716577540107,
      0.266187050359712,
      0.143884892086331,
      0.0935251798561151,
      0.496402877697842,
      0.293333333333333,
      0.14,
      0.126666666666667,
      0.44,
      0.228723404255319,
      0.143617021276596,
      0.143617021276596,
      0.484042553191489,
      0.268965517241379,
      0.137931034482759,
      0.131034482758621,
      0.462068965517241,
      0.272727272727273,
      0.170454545454545,
      0.0738636363636364,
      0.482954545454545,
      0.25748502994012,
      0.167664670658683,
      0.0958083832335329,
      0.479041916167665,
      0.209424083769634,
      0.157068062827225,
      0.120418848167539,
      0.513089005235602,
      0.257731958762887,
      0.0618556701030928,
      0.15979381443299,
      0.520618556701031,
      0.220689655172414,
      0.0758620689655172,
      0.193103448275862,
      0.510344827586207,
      0.221621621621622,
      0.0378378378378378,
      0.194594594594595,
      0.545945945945946,
      0.21505376344086,
      0.0806451612903226,
      0.193548387096774,
      0.510752688172043,
      0.283018867924528,
      0.0566037735849057,
      0.182389937106918,
      0.477987421383648,
      0.224489795918367,
      0.0612244897959184,
      0.17687074829932,
      0.537414965986395,
      0.286585365853659,
      0.0609756097560976,
      0.207317073170732,
      0.445121951219512,
      0.317567567567568,
      0.0878378378378378,
      0.195945945945946,
      0.398648648648649,
      0.273381294964029,
      0.0287769784172662,
      0.215827338129496,
      0.482014388489209,
      0.266187050359712,
      0.0431654676258993,
      0.23021582733813,
      0.460431654676259,
      0.248322147651007,
      0.0671140939597315,
      0.167785234899329,
      0.516778523489933,
      0.210191082802548,
      0.133757961783439,
      0.0828025477707006,
      0.573248407643312,
      0.287958115183246,
      0.0418848167539267,
      0.151832460732984,
      0.518324607329843,
      0.201183431952663,
      0.0532544378698225,
      0.21301775147929,
      0.532544378698225,
      0.269230769230769,
      0.0164835164835165,
      0.142857142857143,
      0.571428571428571,
      0.236111111111111,
      0.0694444444444444,
      0.180555555555556,
      0.513888888888889,
      0.147540983606557,
      0.0491803278688525,
      0.185792349726776,
      0.617486338797814,
      0.227848101265823,
      0.0379746835443038,
      0.126582278481013,
      0.607594936708861,
      0.165644171779141,
      0.049079754601227,
      0.116564417177914,
      0.668711656441718,
      0.157142857142857,
      0.080952380952381,
      0.133333333333333,
      0.628571428571429,
      0.176470588235294,
      0.0633484162895928,
      0.126696832579186,
      0.633484162895928,
      0.133587786259542,
      0.0877862595419847,
      0.099236641221374,
      0.679389312977099,
      0.14344262295082,
      0.040983606557377,
      0.0778688524590164,
      0.737704918032787,
      0.223107569721116,
      0.0836653386454183,
      0.0637450199203187,
      0.629482071713147
    )
  ),
  class = c("grouped_df", "tbl_df", "tbl", "data.frame"),
  row.names = c(NA, -144L),
  groups = structure(
    list(
      year_month = c(
        "2017-01",
        "2017-02",
        "2017-03",
        "2017-04",
        "2017-05",
        "2017-06",
        "2017-07",
        "2017-08",
        "2017-09",
        "2017-10",
        "2017-11",
        "2017-12",
        "2018-01",
        "2018-02",
        "2018-03",
        "2018-04",
        "2018-05",
        "2018-06",
        "2018-07",
        "2018-08",
        "2018-09",
        "2018-10",
        "2018-11",
        "2018-12",
        "2019-01",
        "2019-02",
        "2019-03",
        "2019-04",
        "2019-05",
        "2019-06",
        "2019-07",
        "2019-08",
        "2019-09",
        "2019-10",
        "2019-11",
        "2019-12"
      ),
      .rows = structure(
        list(
          1:4,
          5:8,
          9:12,
          13:16,
          17:20,
          21:24,
          25:28,
          29:32,
          33:36,
          37:40,
          41:44,
          45:48,
          49:52,
          53:56,
          57:60,
          61:64,
          65:68,
          69:72,
          73:76,
          77:80,
          81:84,
          85:88,
          89:92,
          93:96,
          97:100,
          101:104,
          105:108,
          109:112,
          113:116,
          117:120,
          121:124,
          125:128,
          129:132,
          133:136,
          137:140,
          141:144
        ),
        ptype = integer(0),
        class = c("vctrs_list_of", "vctrs_vctr", "list")
      )
    ),
    class = c("tbl_df", "tbl", "data.frame"),
    row.names = c(NA, -36L),
    .drop = TRUE
  )
)

ggplot(data_df)  +  
  geom_bar(aes(x=year_month, 
               y=broj),stat="identity")+ 
  geom_line(aes(x=year_month, 
                y=100*percent,
                colour = obdukcija,
                group = obdukcija),stat="identity",size=2)+ 
  scale_y_continuous(sec.axis=sec_axis(~.*0.01,
                                       name="Percentage",
                                       labels=scales::percent)
                     )+
  theme_classic()+
   theme(legend.position="bottom") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

我希望我做了一个可重现的例子。感谢您的帮助。你能解释一下如何正确编写 y 轴代码吗?

r ggplot2
1个回答
0
投票

你的猜测不太好。绘制辅助轴时需要找到比例因子。由于您的数据框按year_month分组,因此您可以通过以下方式获得

broj
的最大值:

library(dplyr)

summarise(data_df, max=sum(broj)) |>
  summarise(max=max(max))
# 262

但是,您的线路也按

obdukcija
分组,因此您需要考虑到这一点。

ungroup(data_df) |>
  summarise(max_pct=sum(percent), .by=c(year_month, obdukcija)) |>
  summarise(max=max(max_pct))
# 0.738

262/0.738
# 355

这意味着您的主 y 轴(左)将比次 y 轴(右)大 355 倍。这将是您的缩放因子,以最大化绘图区域内两个系列的可见性。

ggplot(data_df)  +  
  geom_bar(aes(x=year_month, y=broj), stat="identity") + 
  geom_line(aes(x=year_month, 
                y=percent*355,   # Here
                colour = obdukcija,
                group = obdukcija), stat="identity",size=2) + 
  scale_y_continuous(sec.axis=sec_axis(~./355,  # And here
                     name="Percentage",                                
                     labels=scales::percent)) +
  theme_classic() +
  theme(legend.position="bottom") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.