【问题标题】:ggplot legend for second axis第二轴的ggplot图例
【发布时间】:2021-08-10 08:10:15
【问题描述】:

我希望你们能帮助我。我在用 ggplot 创建图表时遇到问题。我想要第二个 y 轴,将累积降雨总和(= 累积 Regenmenge 10 Tage)显示为点。到现在为止还挺好。然而,代表累积降雨量的蓝点不知何故与图例中的“转换”填充混合在一起。它们应该是图例中的一个单独元素。我希望你明白我的意思。 提前非常感谢。希望你们能帮我解决这个问题。 :) 这是我的数据

BF_1189 <- structure(list(Datum = structure(c(18703, 18703, 18703, 18703, 
18703, 18703, 18703, 18703, 18703, 18703, 18724, 18724, 18724, 
18724, 18724, 18724, 18724, 18724, 18724, 18724, 18730, 18730, 
18730, 18730, 18730, 18730, 18730, 18730, 18730, 18730, 18744, 
18744, 18744, 18744, 18744, 18744, 18744, 18744, 18744, 18744, 
18758, 18758, 18758, 18758, 18758, 18758, 18758, 18758, 18758, 
18758, 18774, 18774, 18774, 18774, 18774, 18774, 18774, 18774, 
18774, 18774), class = "Date"), Soll = c("1189", "1189", "1192", 
"1192", "1202", "1202", "2484", "2484", "552", "552", "1189", 
"1189", "1192", "1192", "1202", "1202", "2484", "2484", "552", 
"552", "1189", "1189", "1192", "1192", "1202", "1202", "2484", 
"2484", "552", "552", "1189", "1189", "1192", "1192", "1202", 
"1202", "2484", "2484", "552", "552", "1189", "1189", "1192", 
"1192", "1202", "1202", "2484", "2484", "552", "552", "1189", 
"1189", "1192", "1192", "1202", "1202", "2484", "2484", "552", 
"552"), Transtyp = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 
1L, 2L, 1L, 2L), .Label = c("2", "5"), class = "factor"), mittelwert = c(34.015625, 
33.8375, 36.15, 36.6518518518519, 36.975, 36.1193548387097, 33.828125, 
33.3625, 36.1625, 36.5032258064516, 27.109375, 26.775, 28.7625, 
30.128125, 29.590625, 28.50625, 25.7258064516129, 25.340625, 
26.6322580645161, 26.446875, 30.5516129032258, 30.4375, 31.903125, 
32.15625, 32.40625, 30.715625, 28.4875, 29.084375, 28.784375, 
29.9387096774194, 12.15625, 12.26875, 14.6290322580645, 14.6516129032258, 
15.90625, 15.428125, 12.884375, 12.89375, 15.1625, 14.4, 11.9171875, 
14.078125, 13.675, 13.7896551724138, 18.5, 17.071875, 10.578125, 
10.21875, 13.021875, 13.225, 17.065625, 19.83125, 16.2586206896552, 
17.441935483871, 19.75625, 19.15, 15.4451612903226, 14.2741935483871, 
17.641935483871, 17.358064516129), kumsum_1189 = c(36.94, 36.94, 
36.94, 36.94, 36.94, 36.94, 36.94, 36.94, 36.94, 36.94, 21.11, 
21.11, 21.11, 21.11, 21.11, 21.11, 21.11, 21.11, 21.11, 21.11, 
40.56, 40.56, 40.56, 40.56, 40.56, 40.56, 40.56, 40.56, 40.56, 
40.56, 7.76, 7.76, 7.76, 7.76, 7.76, 7.76, 7.76, 7.76, 7.76, 
7.76, 25.04, 25.04, 25.04, 25.04, 25.04, 25.04, 25.04, 25.04, 
25.04, 25.04, 31.86, 31.86, 31.86, 31.86, 31.86, 31.86, 31.86, 
31.86, 31.86, 31.86)), row.names = c(NA, -60L), groups = structure(list(
    Datum = structure(c(18703, 18703, 18703, 18703, 18703, 18724, 
    18724, 18724, 18724, 18724, 18730, 18730, 18730, 18730, 18730, 
    18744, 18744, 18744, 18744, 18744, 18758, 18758, 18758, 18758, 
    18758, 18774, 18774, 18774, 18774, 18774), class = "Date"), 
    Soll = c("1189", "1192", "1202", "2484", "552", "1189", "1192", 
    "1202", "2484", "552", "1189", "1192", "1202", "2484", "552", 
    "1189", "1192", "1202", "2484", "552", "1189", "1192", "1202", 
    "2484", "552", "1189", "1192", "1202", "2484", "552"), .rows = structure(list(
        1:2, 3:4, 5:6, 7:8, 9:10, 11:12, 13:14, 15:16, 17:18, 
        19:20, 21:22, 23:24, 25:26, 27:28, 29:30, 31:32, 33:34, 
        35:36, 37:38, 39:40, 41:42, 43:44, 45:46, 47:48, 49:50, 
        51:52, 53:54, 55:56, 57:58, 59:60), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = c(NA, -30L), class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

这是我的代码

solle_1189 = c("1189", "1192", "1202", "2484", "552") 
BF_1189 %>% pull(Datum) %>% unique() -> sdates

for(i in solle_1189 ){
        BF_1189 %>% filter (Soll == i) %>%
          group_by(Datum, Soll, Transtyp) %>%
          ggplot(aes(Datum, mittelwert, fill = Transtyp))+
          geom_bar(stat = "Identity", position = "dodge") + 
          geom_point(aes(Datum, kumsum_1189), shape =18, color = "blue", size = 3) +
          scale_y_continuous(name = "Bodenfeuchte in %", sec.axis = sec_axis(~., name = "Kummulative Regenmenge 10 Tage"))+
          scale_x_date(breaks = sdates)+
          theme_minimal()+
          theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
                axis.text = element_text(size = 9),
                axis.title = element_text(size = 10, face = "bold"))+
          scale_fill_grey()+
          labs (title = paste0("Soll ", i)) ->pb
          print(pb)
          #ggsave(paste0("Z:/FOR/FOR-Projects/IPP_Soelle/Daten/Data_Raw/Max_2021/Grafen/Bodenfeuchte_fdr_kumnied_", i, ".png"))
}

【问题讨论】:

    标签: r ggplot2 legend axis


    【解决方案1】:

    您的问题的一个潜在解决方案是通过mutate() 创建一个新变量(例如“Regen”)并将美学映射到它,例如

    BF_1189 <- BF_1189 %>% 
      mutate(Regen = "Kummulative")
    
    for (i in solle_1189) {
      BF_1189 %>% filter (Soll == i) %>%
        group_by(Datum, Soll, Transtyp) %>%
        ggplot(aes(x = Datum, y = mittelwert))+
        geom_bar(aes(fill = Transtyp), stat = "Identity",
                 position = "dodge") + 
        geom_point(aes(y = kumsum_1189, size = Regen),
                   shape = 18, color = "blue") +
        scale_y_continuous(name = "Bodenfeuchte in %",
                           sec.axis = sec_axis(~., name = "Kummulative Regenmenge 10 Tage")) +
        scale_x_date(breaks = sdates)+
        theme_minimal()+
        theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),
              axis.text = element_text(size = 9),
              axis.title = element_text(size = 10, face = "bold")) +
        scale_fill_grey() +
        labs (title = paste0("Soll ", i)) -> pb
      print(pb)
      #ggsave(paste0("Z:/FOR/FOR-Projects/IPP_Soelle/Daten/Data_Raw/Max_2021/Grafen/Bodenfeuchte_fdr_kumnied_", i, ".png"))
    }
    

    这能解决你的问题吗?

    【讨论】:

    • 是的,确实如此。非常感谢。
    【解决方案2】:

    您可以将col 放在aesgeom_point 参数中:

    BF_1189
    
    solle_1189 = c("1189", "1192", "1202", "2484", "552") 
    BF_1189 %>% pull(Datum) %>% unique() -> sdates
    
    for(i in solle_1189) {
      BF_1189 %>% filter (Soll == i) %>%
        group_by(Datum, Soll, Transtyp) %>%
        ggplot(aes(Datum, mittelwert, fill = Transtyp)) +
        geom_bar(stat = "Identity", position = "dodge") +
        geom_point(
          aes(Datum, kumsum_1189, color = 'blue'), ## Put color inside aes
          shape = 18,
          size = 3,
          inherit.aes =  FALSE
        ) + 
        scale_color_manual('Cumulative', values = 'blue')+
        scale_y_continuous(name = "Bodenfeuchte in %",
                           sec.axis = sec_axis( ~ ., name = "Kummulative Regenmenge 10 Tage"),) +
        scale_x_date(breaks = sdates) +
        theme_minimal() +
        theme(
          axis.text.x = element_text(
            angle = 90,
            vjust = 0.5,
            hjust = 1
          ),
          axis.text = element_text(size = 9),
          axis.title = element_text(size = 10, face = "bold")
        ) +
        scale_fill_grey() +
        labs (title = paste0("Soll ", i)) -> pb
      print(pb)
      #ggsave(paste0("Z:/FOR/FOR-Projects/IPP_Soelle/Daten/Data_Raw/Max_2021/Grafen/Bodenfeuchte_fdr_kumnied_", i, ".png"))
    }
    

    【讨论】:

    • 嘿,这也有效。谢谢!我怎样才能让它说“Niederschlag”而不是“蓝色”。有没有办法。
    • 您指的是蓝色还是传说中的名称“蓝色”?如果你指的是后者,那么你可以试试scale_color_manual(name = 'Cumulative', values = c('Niederschlag' = 'blue'))
    • 是的,这就是我的意思。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-31
    • 1970-01-01
    相关资源
    最近更新 更多