【问题标题】:glue function in R isn't printing the right value when used in ggplot2 title在 ggplot2 标题中使用时,R 中的胶水函数没有打印正确的值
【发布时间】:2020-12-21 14:38:29
【问题描述】:

我正在使用排名系统在 ggplot2 中编写图,我想在标题中绘制实际值(因为它的排名出现在图中),稍后我将使用 dlply 打印多个图(一个用于数据库的每一行)。这是一个绘图示例,过滤到第十行(nregs == "1.10 Xingu e Interbacias Xingu - Tapajós"):

precipitation %>% 
        mutate(rank_precip = rank(-precip)) %>%
        filter(nregs == '1.10 Xingu e Interbacias Xingu - Tapajós') %>% 
        ggplot(aes(x = rank_precip, y = nregs)) +
        labs(title = glue("Accumulated Precipitation (1980-2010): {precip} mm")) +
        geom_segment(aes(x = 1, y = nregs, xend = 58, yend = nregs), size = 1.5, color = 'antiquewhite4') +
        geom_segment(data = expand.grid(x = rev(seq(1,58, by = 3)), y = 1),
                   aes(x = x, y = y, xend = x, yend = y + 0.05),
                   color = 'antiquewhite4',
                   size = 1.5) +
        geom_segment(data = expand.grid(x = rev(seq(1,58, by = 3)), y = 1),
                   aes(x = x, y = y, xend = x, yend = y - 0.05),
                   color = 'antiquewhite4',
                   size = 1.5) +
        scale_x_reverse() +
        geom_rect(aes(xmin = rank_precip - 0.5, xmax = rank_precip + 0.5, ymax = 1.1, ymin = 0.9), fill = 'blue3') +
        geom_rect(aes(xmin = 57.5, xmax = 58.5, ymax = 1.1, ymin = 0.9)) +
        geom_rect(aes(xmin = 0.5, xmax = 1.5, ymax = 1.1, ymin = 0.9)) +
        geom_text(aes(label = "58º", x = 57.75, y = 1.15), size = 6, fontface = 'bold') +
        geom_text(aes(label = "1º", x = 0.75, y = 1.15), size = 6, fontface = 'bold') +
        geom_rect(aes(xmin = rank_precip - 0.5, xmax = rank_precip + 0.5, ymax = 1.1, ymin = 0.9), fill = 'blue3') +
        geom_text(aes(x = rank_precip - 0.25, y = 1.15, label = glue("{rank_precip}º")), size = 6, fontface = 'bold', color = 'blue3') +
        theme_minimal() +
        theme(axis.text.y = element_blank(),
              axis.title.y = element_blank(),
              axis.text.x = element_blank(),
              axis.title.x = element_blank(),
              panel.grid = element_blank()) 

打印的情节在这里:

但是,从数据集可以看出,标题中的降水量值打印为​​“67”,但实际上是1937.08。我尝试将数据集转换为tibble , 将 precip 值转换为字符,但没有任何效果,你能帮我吗?

structure(list(nregs = c("1.1 Javari e Interbacias Javari - Juruá", 
"1.2 Transf. da Margem Esquerda do Solimões", "1.3 Juruá e Interbacias Juruá - Jutaí", 
"1.4 Purus e Interbacias Purus - Juruá", "1.5 Negro", "1.6 Madeira e Interbacias Madeira - Purus", 
"1.7 Estaduais Margem Esquerda do Amazonas", "1.8 Tapajós e Interbacias Tapajós - Madeira", 
"1.9 Estaduais PA", "1.10 Xingu e Interbacias Xingu - Tapajós", 
"1.11 Estaduais PA/AM", "1.12 Transfronteiriça Oiapoque", "1.13 Jari - Estaduais AP", 
"2.1 Araguaia", "2.2 Alto Tocantins", "2.3 Baixo Tocantins", 
"3.1 Gurupi - Estaduais MA", "3.2 Itapecuru", "4.1 Alto Parnaíba", 
"4.2 Médio Parnaíba", "4.3 Baixo Parnaíba", "5.1 Estaduais CE/PI", 
"5.2 Estaduais CE", "5.3 Estaduais CE/RN", "5.4 Piancó - Piranhas - Açu", 
"5.5 Estaduais RN", "5.6 Estaduais PB/PE", "5.7 Mundaú - Paraíba - Estaduais AL", 
"6.1 Alto São Francisco", "6.2 Médio São Francisco", "6.3 Sub Médio São Francisco", 
"6.4 Baixo São Francisco", "7.1 Vaza Barris - Real - Sergipe", 
"7.2 Itapecuru - Paraguaçu", "7.3 Contas", "7.4 Jequitinhonha - Pardo", 
"7.5 São Mateus - Mucuri - Itaúnas - Estaduais BA/MG", "8.1 Doce", 
"8.2 Estaduais ES", "8.3 Paraíba do Sul", "8.4 Estaduais RJ", 
"8.5 Estaduais SP", "8.6 Ribeira do Iguape", "9.1 Paranaíba", 
"9.2 Grande", "9.3 PCJ", "9.4 Tietê", "9.5 Paranapanema", "9.6 Paraná RH1", 
"9.7 Iguaçu", "10.1 Alto Paraguai", "10.2 Taquari - Miranda - Apa", 
"11.1 Alto Uruguai", "11.2 Médio Uruguai RS", "12.1 Estaduais PR", 
"12.2 Estaduais SC", "12.3 Guaíba e Estaduais RS", "12.4 Transfronteiriça Lagoa Mirim - Chuí"
), precip = c(2440.71, 2631.19, 2300.6, 2363.48, 2461.57, 2190.72, 
2297.28, 1960.7, 2095.36, 1937.08, 2400.93, 2387.93, 2420.15, 
1617.32, 1543.31, 2318.93, 2165.06, 1520.86, 1109.38, 872.52, 
1363.62, 1181.33, 844.2, 811.49, 826.64, 1197.12, 1094.41, 1261.29, 
1192.56, 883.14, 738.28, 823.37, 883.49, 876.4, 866.9, 950.23, 
1145.28, 1253.72, 1337.82, 1428.05, 1391.78, 1842.74, 1742.62, 
1511.54, 1517.9, 1549.69, 1459.11, 1549.54, 1503.42, 1764.78, 
1413.47, 1304.44, 1893.2, 1713.52, 2021.25, 1753.24, 1672.39, 
1412.73)), row.names = c(NA, -58L), class = c("tbl_df", "tbl", 
"data.frame"))

【问题讨论】:

    标签: r ggplot2 title paste geom-text


    【解决方案1】:

    这很有趣,它使用了一个名为precip 的基础数据集,而不是您的列。我对glue 不满意所以找到sprintf 的选项并使用this SO post 的答案来格式化它,即ggplot 调用中的大括号和点

    library(tidyverse)
    library(glue)
    
    precipitation %>% 
      mutate(rank_precip = rank(-precip)) %>%
      filter(nregs == '1.10 Xingu e Interbacias Xingu - Tapajós') %>% 
      {
        ggplot(., aes(x = rank_precip, y = nregs)) +
          labs(title = sprintf("Accumulated Precipitation (1980-2010): %s mm", .$precip)) +
          geom_segment(aes(x = 1, y = nregs, xend = 58, yend = nregs), size = 1.5, color = 'antiquewhite4') +
          geom_segment(data = expand.grid(x = rev(seq(1,58, by = 3)), y = 1),
                       aes(x = x, y = y, xend = x, yend = y + 0.05),
                       color = 'antiquewhite4',
                       size = 1.5) +
          geom_segment(data = expand.grid(x = rev(seq(1,58, by = 3)), y = 1),
                       aes(x = x, y = y, xend = x, yend = y - 0.05),
                       color = 'antiquewhite4',
                       size = 1.5) +
          scale_x_reverse() +
          geom_rect(aes(xmin = rank_precip - 0.5, xmax = rank_precip + 0.5, ymax = 1.1, ymin = 0.9), fill = 'blue3') +
          geom_rect(aes(xmin = 57.5, xmax = 58.5, ymax = 1.1, ymin = 0.9)) +
          geom_rect(aes(xmin = 0.5, xmax = 1.5, ymax = 1.1, ymin = 0.9)) +
          geom_text(aes(label = "58º", x = 57.75, y = 1.15), size = 6, fontface = 'bold') +
          geom_text(aes(label = "1º", x = 0.75, y = 1.15), size = 6, fontface = 'bold') +
          geom_rect(aes(xmin = rank_precip - 0.5, xmax = rank_precip + 0.5, ymax = 1.1, ymin = 0.9), fill = 'blue3') +
          geom_text(aes(x = rank_precip - 0.25, y = 1.15, label = glue("{rank_precip}º")), size = 6, fontface = 'bold', color = 'blue3') +
          theme_minimal() +
          theme(axis.text.y = element_blank(),
                axis.title.y = element_blank(),
                axis.text.x = element_blank(),
                axis.title.x = element_blank(),
                panel.grid = element_blank()) 
      }
    

    编辑

    作为每个级别的 nregs 的函数:

    rank_precip_plot_fun <- function(input){
      ggplot(input, aes(x = rank_precip, y = nregs)) +
        labs(title = sprintf("Accumulated Precipitation (1980-2010): %s mm", input$precip)) +
        geom_segment(aes(x = 1, y = nregs, xend = 58, yend = nregs), size = 1.5, color = 'antiquewhite4') +
        geom_segment(data = expand.grid(x = rev(seq(1,58, by = 3)), y = 1),
                     aes(x = x, y = y, xend = x, yend = y + 0.05),
                     color = 'antiquewhite4',
                     size = 1.5) +
        geom_segment(data = expand.grid(x = rev(seq(1,58, by = 3)), y = 1),
                     aes(x = x, y = y, xend = x, yend = y - 0.05),
                     color = 'antiquewhite4',
                     size = 1.5) +
        scale_x_reverse() +
        geom_rect(aes(xmin = rank_precip - 0.5, xmax = rank_precip + 0.5, ymax = 1.1, ymin = 0.9), fill = 'blue3') +
        geom_rect(aes(xmin = 57.5, xmax = 58.5, ymax = 1.1, ymin = 0.9)) +
        geom_rect(aes(xmin = 0.5, xmax = 1.5, ymax = 1.1, ymin = 0.9)) +
        geom_text(aes(label = "58º", x = 57.75, y = 1.15), size = 6, fontface = 'bold') +
        geom_text(aes(label = "1º", x = 0.75, y = 1.15), size = 6, fontface = 'bold') +
        geom_rect(aes(xmin = rank_precip - 0.5, xmax = rank_precip + 0.5, ymax = 1.1, ymin = 0.9), fill = 'blue3') +
        geom_text(aes(x = rank_precip - 0.25, y = 1.15, label = glue("{rank_precip}º")), size = 6, fontface = 'bold', color = 'blue3') +
        theme_minimal() +
        theme(axis.text.y = element_blank(),
              axis.title.y = element_blank(),
              axis.text.x = element_blank(),
              axis.title.x = element_blank(),
              panel.grid = element_blank()) 
    }
    
    
    dt <- precipitation %>% 
      mutate(rank_precip = rank(-precip))
    
    library(plyr)
    plots <- dlply(dt, .(nregs), rank_precip_plot_fun)
    plots[[1]]
    plots[[2]] # different value
    

    【讨论】:

    • 它完成了部分工作!但是当我删除过滤器并将 ggplot 代码保存为函数“p”并应用此代码时:require(plyr) plots = dlply(precipitation, .(nregs), function (x) p %+% x) invisible(mapply(ggsave, file = paste0(names(plots),".jpeg"), plot = plots)) 它实际上只是继续从数据集的第一行打印 precip 值(2440.71)
    • 我已经使用功能选项对其进行了编辑。排名必须在您使用 dlply 之前进行,因此剩下的唯一命令是 ggplot2,因此不再需要 . 并且可以像往常一样传递数据
    猜你喜欢
    • 2020-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多