使用单条从标准条形图(或 ggplot2 geom_col)创建图标

Create icons from standard barplot (or ggplot2 geom_col) with single bar

我想创建带有单条的 png 图标(来自标准条形图或 ggplot2 geom_col)。图标将显示在传单地图上。有 data.frame: lat,lon,val。参数 "val" 用于设置栏的高度(一个图标上只有一个栏)。图标必须具有相同的大小,条形必须具有相同的宽度,每个条形都带有上面的标签 (val)。栏的高度受最大值(图标高度)限制。

Example image - map with icons to be reconstructed

示例代码如下。我使用了这里的提示: R Barplot with one bar - how to plot correctly

我的代码的结果 - 都具有相同的高度

lats = c(69.5, 70.0, 69.0) 
lons = c(33.0,33.5,34.3) 
vals = c(7,19,5) 
df = data.frame(lats, lons, vals)

for (i in 1:3) {
      png(file=paste0(i,".png"), width=100, height=200, res=72)
      bp <- barplot(df$vals[i], height =df$vals[i],
                    width=0.2, xlim=c(0,1.2), col="brown4", axes=FALSE);
      text(bp, 10*df$vals[i]+10, labels=df$vals[i]);
      dev.off()
}

我使用了@Axeman 的建议并使用 png/barplot 参数进行了一些实验。 问题解决了。结果如下。

library(shiny)
library(leaflet)

ui <- fluidPage(leafletOutput("map"))

myicon=function(condition){
  makeIcon(
    iconUrl = paste0(condition,".png"),
    iconWidth = 30, iconHeight = 80
  )}

server <- function(input, output, session) {

  lats = c(69.5, 70.0, 69.0) 
  lons = c(33.0,33.5,34.3) 
  vals = c(7,12,5) 
  df = data.frame(lats, lons, vals)
  for (i in 1:nrow(df)) {
    png(file=paste0(i,".png"), bg="transparent",width=3, height=10, units="in", res=72)
    bp <- barplot(df$vals[i], height =10*df$vals[i],
                  width=1, ylim=c(0,max(10*df$vals)+30),col="brown4", axes=FALSE);
    text(bp,10*df$vals[i]+20,labels=df$vals[i],cex=10,font=2);
    dev.off()
  }

  output$map <- renderLeaflet({
    top=70.4;
    bottom=66.05;
    right=42.05;
    left=27.5;
    leaflet(data = df,
            options = leafletOptions(minZoom = 3,maxZoom = 10))%>%
      fitBounds(right,bottom,left,top)%>%
      addTiles()%>%
      addProviderTiles("Esri.OceanBasemap") %>%
      addMarkers(
        icon = myicon(index(df)),
        lng = lons, lat = lats,
        options = markerOptions(draggable = TRUE))
  })
}

shinyApp(ui, server)