使用传单库在弹出窗口中绘制条形图 [英] plotting barchart in popup using leaflet library
问题描述
全部快速提问. 我在已加载到RStudio的sql服务器中有一些数据.我已经为数据制作了一个图表,现在我正在使用传单库,并使用纬度和经度在地图上绘制点.我希望能够在用户单击该点时使用弹出窗口在其中显示条形图.
Quick question all. I have some data in sql server which i have loaded into RStudio. I have made a barchart for the data and now i am using leaflet library with the use of latitude and longitude to plot a point on the map. I want to be able to use popup to show a barchart in it when the user clicks on the point.
BarChart代码(也许这是一个问题,因为我使用的是googleVis库,所以不确定是否可以在弹出窗口中使用它.但这再次是我可以根据需要制作的最合适的条形图,其他建议可能会对您有所帮助我还不是R库的专业人员)
BarChart code (maybe this is a problem because i am using googleVis library so not sure if i can use this in the popup. but again this is the most appropriate bar graph i can make and need- other suggestions could be helpful as i am not a professional in R libraries yet)
Switzerland <- sqlQuery(con, "sql query")
SwitzerlandChart <- gvisBarChart(Switzerland, options = list(height=200))
对于图形绘图,代码为:
For the graph plot the code is:
m <- leaflet() %>%
addTiles() %>% # Add default OpenStreetMap map tiles
addCircles(lng=8.498868, lat=46.9221, popup=paste(plot(SwitzerlandChart)))
当我运行此代码时,它将打开一个网页以查看我的barplot. 然后我运行以下命令:
When i run this code it opens a webpage to view my barplot. Then i run the following:
m #Prints the graph
这会打印出点位于所需位置的图形,但弹出窗口会显示一个网页,而该网页也只有我可以打开.
This prints the graph with the point in the desired location but the popup shows me a webpage instead which also only i can open.
我希望能够在弹出窗口中绘制条形图.
I want to be able to plot the bargraph inside the popup please.
希望有人可以提供帮助
推荐答案
也许有点晚,但这是一个解决方案. library(leaflet)
中的addPopups()
函数似乎能够处理.svg
文件.因此,您可以简单地使用svg()
保存您的绘图,然后使用readLines()
再次读取它.这是使用library(mapview)
的可重现示例:
Maybe a little late but here's a solution. The addPopups()
function in library(leaflet)
seems to be able to handle .svg
files. Therefore, you could simply save your plot using svg()
and then read it again using readLines()
. Here's a reproducible example using library(mapview)
:
library(lattice)
library(mapview)
library(sp)
data(meuse)
coordinates(meuse) <- ~x+y
proj4string(meuse) <- CRS("+init=epsg:28992")
clr <- rep("grey", length(meuse))
fldr <- tempfile()
dir.create(fldr)
pop <- lapply(seq(length(meuse)), function(i) {
clr[i] <- "red"
p <- xyplot(meuse$cadmium ~ meuse$copper,
col = clr, pch = 20, alpha = 0.7)
svg(filename = paste(fldr, "test.svg", sep = "/"),
width = 250 * 0.01334, height = 250 * 0.01334)
print(p)
dev.off()
tst <- paste(readLines(paste(fldr, "test.svg", sep = "/")), collapse = "")
return(tst)
})
mapview(meuse, popup = pop, cex = "cadmium")
您将看到每个弹出窗口都是一个散点图.对于leaflet
示例,请考虑以下问题:
You will see that each popup is a scatterplot. As for a leaflet
example, consider this:
content <- pop[[1]]
leaflet() %>% addTiles() %>%
addPopups(-122.327298, 47.597131, content,
options = popupOptions(closeButton = FALSE)
)
如果您需要绘图是交互式的,则可以查看library(gridSVG)
,它可以从中生成交互式svg绘图. lattice
或ggplot2
图.
In case you need the plot to be interactive, you could have a look at library(gridSVG)
which is able to produce interactive svg plots from e.g. lattice
or ggplot2
plots.
更新:
library(mapview)
现在具有为此指定的功能:
library(mapview)
now has designated functionality for this:
-
popupGraph
:嵌入晶格, ggplot2 或基于交互式 hatmlwidgets 的图. -
popupImage
:嵌入本地或远程(网络)图像
popupGraph
: to embed lattice, ggplot2 or interactive hatmlwidgets based plots.popupImage
: to embed local or remote (web) images
当前仅在 mapview 的开发版本中可用,该版本可以安装:
This is currently only available in the development version of mapview which can be installed with:
devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop"
这篇关于使用传单库在弹出窗口中绘制条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!