Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to add more complex objects like ggplots? #51

Open
Make42 opened this issue Mar 26, 2015 · 7 comments
Open

Is it possible to add more complex objects like ggplots? #51

Make42 opened this issue Mar 26, 2015 · 7 comments

Comments

@Make42
Copy link

@Make42 Make42 commented Mar 26, 2015

Is it possible to add more complex objects like ggplots?
For example I might have a data.frame with columns lat, long, x, y:

lat, long, x, y
40, 5, 1, 6
40, 5, 2, 8
40, 5, 5, 3
41, 7, 3, 9
41, 7, 7, 10
41, 7, 9, 13

and I would like to build two ggplots(...) + geom_line(...) - one for 40, 5 and one for 41, 7 - which are printed as embetted graphs on their respective coordinates, namely 40, 5 and 41, 7.

Would that be possible and if so, how?
If not, what would be an option? (E.g. a different package instead of rstudio/leaflet or a different package instead of ggplot2.)

@yihui
Copy link
Member

@yihui yihui commented Mar 26, 2015

It is certainly possible, although not straightforward at the moment. Hopefully it will become easier after we merge #48.

@Make42
Copy link
Author

@Make42 Make42 commented Mar 27, 2015

That'd be great! What is the timeline for that - if there is?

In the meantime: How would I do this until then? An example would be great.

@yihui
Copy link
Member

@yihui yihui commented Mar 27, 2015

Hopefully by the end of next week. Examples will be given when this feature is ready.

@Make42
Copy link
Author

@Make42 Make42 commented Jun 8, 2015

How is the status? Could you provide a link to the examples?

@Make42
Copy link
Author

@Make42 Make42 commented Apr 21, 2016

How is this going? How is the status? Link to examples?

@jcheng5
Copy link
Member

@jcheng5 jcheng5 commented Apr 21, 2016

It's possible now. The trickiest part is encoding the plot as a data URI; I've provided a makePlotURI function to help you with that here.

library(leaflet)
library(ggplot2)

makePlotURI <- function(expr, width, height, ...) {
  pngFile <- plotPNG(function() { expr }, width = width, height = height, ...)
  on.exit(unlink(pngFile))

  base64 <- httpuv::rawToBase64(readBin(pngFile, raw(1), file.size(pngFile)))
  paste0("data:image/png;base64,", base64)
}

plot1 <- makePlotURI({
  print(ggplot(cars, aes(speed, dist)) + geom_point())
}, 200, 200, bg = "transparent")

plot2 <- makePlotURI({
  print(ggplot(cars, aes(speed, dist)) + geom_point())
}, 200, 200, bg = "transparent")

df <- data.frame(
  lat = c(40, 41),
  lng = c(5, 7),
  plots = c(plot1, plot2),
  stringsAsFactors = FALSE
)

leaflet(df) %>% addTiles() %>%
  addMarkers(icon = ~icons(plots))
@timelyportfolio
Copy link
Contributor

@timelyportfolio timelyportfolio commented Apr 22, 2016

very nice example @jcheng5. It might be helpful to add library(shiny) for plotPNG. Thanks for posting this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.