使用rmagick将barplots放置在地图上,将ggplots转换成循环图像

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

[将在地图上绘制多个条形图,我能够在下面使用此解决方案,使用r magick将ggplots转换为图像,然后将它们放置在地图上-也是图像。

Any way to plot multiple barplots on a map?

我想每次创建多张地图,每张地图都有10个场景,每张地图上有12个条形图。我尝试实现循环,但是magick的image_graph函数无法在其中运行。

library(ggmap)
library(maps)
library(ggplot2)
library(magick)

mp <-  NULL
mapWorld <- borders("world", colour="gray70", fill="gray70") 

fig <- image_graph(width = 850, height = 550, res = 96)
ggplot() + mapWorld
dev.off()


 df <- data.frame(region = c('France','UK'), name =
 c('A','B','C','A','B','C'), value1 = c(20,15,15,10,8,8), value2 =
 c(10,15,20,5,8,10) , value3 = c(5,15,10,3,8,5) )

 scenarios = c('value1', 'value2', 'value3')

    for (scenario in scenarios ) {

              for (reg in unique(df$region) ) {

                df_reg = df[ (df$region == reg), ]


                bp <- ggplot(df_reg, aes_string(x = "region", y = scenario, fill = "name")) +
                  geom_bar(stat = 'identity') +
                  theme_bw() + 
                  theme(legend.position = "none", axis.title.x = element_blank(), axis.title.y = element_blank())


                barfig <- image_graph(width = 100, height = 75, res = 72)
                bp
                dev.off()

                barplotname = paste0('barfig_',reg )

                assign(barplotname, barfig)

              }


    final <- image_composite(fig, barfig_France, offset = "+75+150")
    final <- image_composite(final, barfig_UK, offset = "+325+125")
    final

    filename = paste0('map_', scenario , ".png")
    image_write(final, path = filename, format = "png" )

    }

如果我按如下所示手动分解循环,它将正常工作,而我找不到为什么它不在循环中。我没有在magick文档中找到信息,也没有在循环内使用image_graph函数的示例。

              for (reg in unique(df$region) ) {

                df_reg = df[ (df$region == reg), ]


                bp <- ggplot(df_reg, aes_string(x = "region", y = "value1")) + geom_bar(stat = 'identity', aes(fill = name) ) +  #trying in one 
                  theme_bw() + 
                  theme(legend.position = "none", axis.title.x = element_blank(), axis.title.y = element_blank())

                barplotname = paste0('bp_',reg )

                assign(barplotname, bp)

              }

      barfig_France <- image_graph(width = 100, height = 75, res = 72)
      bp_France
      dev.off()

      barfig_UK <- image_graph(width = 100, height = 75, res = 72)
      bp_UK
      dev.off()


    final <- image_composite(fig, barfig_France, offset = "+75+150")
    final <- image_composite(final, barfig_UK, offset = "+325+125")
    final

如果我尝试将此块集成到“ for(scenarios ...)”循环中,问题仍然存在,我无法将绘图转换为图像

r ggplot2 mapping rmagick
1个回答
0
投票

[找到了一种基本的解决方法,包括保存条形图,然后使用image_read再次导入它们

for (scenario in scenarios ) {

          for (reg in unique(df$region) ) {

            df_reg = df[ (df$region == reg), ]

            bp <- ggplot(df_reg, aes_string(x = "region", y = scenario)) + geom_bar(stat = 'identity', aes(fill = name) ) +  #trying in one 
            theme_bw() + 
            theme(legend.position = "none", axis.title.x = element_blank(), axis.title.y = element_blank())

            barplotname = paste0('barfig_',reg, '.svg')

            ggsave(barplotname, bp, width = 1.2, height = 1, dpi = 72)


            plotname = paste0('barfig_', reg)
            img = image_read_svg(barplotname)
            assign(plotname ,img)

          }

    final <- image_composite(fig, barfig_France, offset = "+75+150")
    final <- image_composite(final, barfig_UK, offset = "+325+125")
    final

    filename = paste0('map_', scenario , ".png")
    image_write(final, path = filename, format = "png" )

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