Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
647 views
in Technique[技术] by (71.8m points)

r - plotting barchart in popup using leaflet library

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 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)))

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.

Hope someone can help

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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")

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)
  )

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.

UPDATE:

library(mapview) now has designated functionality for this:

  • popupGraph: to embed lattice, ggplot2 or interactive hatmlwidgets based plots.
  • popupImage: to embed local or remote (web) images

This is currently only available in the development version of mapview which can be installed with:

devtools::install_github("environmentalinformatics-marburg/mapview", ref = "develop"

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...