r - 使用单条从标准条形图(或 ggplot2 geom_col)创建图标
问题描述
我想用单条创建 png 图标(来自标准 barplot 或 ggplot2 geom_col)。图标将显示在传单地图上。有data.frame:lat,lon,val。参数“val”用于设置条的高度(一个图标只有一个条)。图标必须具有相同的大小,条形必须具有相同的宽度,每个条形都带有上面的标签(val)。条的高度受最大值(图标高度)的限制。
示例代码如下。我使用了这里的提示: R Barplot with one bar - how to plot wrong
我的代码的结果 - 都具有相同的高度
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)
推荐阅读
- angular - 为什么浏览器对 OPTION 请求得到 200 响应和 503 响应?
- python - Keras,使用 y_pred y_true 的不同属性最大化自定义损失
- html - 我正在尝试更改我网站上的字体,但无论我尝试什么都行不通
- c++11 - 如何将可变参数模板参数分配给 std::array
- postgresql - PostgreSQL 没有复制一些更改(扩展)
- pytorch - Pytorch 中是否有一种方法可以以可以反向传播的方式计算唯一值的数量?
- amazon-web-services - 自动为所有 AWS 账户创建相同的策略
- android - 无法从 Android PUT 到 DJANGO REST
- node.js - Access-Control-Allow-Headers 在预检响应中不允许请求标头字段内容类型
- python - 我试图通过列表理解找出下面显示的结果: