How can I embed an image on echart plot? - r

I was trying to embed an image on an echart plot. The image is coming perfectly if I provide a link of the image, but while I am trying to call the same image from my directory, the image is not coming in the plot.
I was tring the example provided in this website and the code I am using is as below.
library( echarts4r)
cars %>%
e_charts(speed) %>%
e_scatter(dist) %>%
e_image_g(
right = 20,
top = 20,
z = -999,
style = list(
image = "~/Rlogo.png",
width = 150,
height = 150,
opacity = .6
)
)
So how can I capture this Rlogo image from my local directory to the plot?

Related

Inserting external image into PowerPoint with officer without changing image dimensions

Is it possible to insert external images into a Powerpoint with officer without changing the image dimensions?
This is the code I have now where x is an rpptx object:
library(officer)
image_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Instagram_logo_2016.svg/768px-Instagram_logo_2016.svg.png"
t_file <- tempfile(pattern = "logo_", fileext = ".png")
download.file(image_url, destfile = t_file)
x <- x %>%
ph_with(value = external_img(t_file), location = ph_location(left = 9.4, top = 4.6))
This will change the dimensions of the image to 4x3 inches (the default for ph_location()).
I know you can use external R packages to get image metadata then pass this to the function so the actual image dimensions are used, but I don't want to add any more external R package dependencies.
You can always specify the image dimensions within your external_img function. I'm assuming you know what dimensions you want, so say your dimensions are width = 9, height = 5 it would look something like this:
library(officer)
image_url <- "https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Instagram_logo_2016.svg/768px-Instagram_logo_2016.svg.png"
t_file <- tempfile(pattern = "logo_", fileext = ".png")
download.file(image_url, destfile = t_file)
x <- x %>%
#specify height and width
ph_with(value = external_img(t_file, width = 9, height = 5), location = ph_location(left = 9.4, top = 4.6))
Hope that is helpful!

Shiny + Leaflet - Is it possible to add images to popups based on groups?

Currently, I have a shiny application that creates a leaflet map of species points. When you hover over a point, the species name is displayed both as a label and a popup.
leafletProxy("map", data = filteredData()) %>%
clearMarkers() %>%
addCircleMarkers(popup = ~as.character(species),
label = ~as.character(species),
radius = 6,
stroke = TRUE,
weight = 1,
fillOpacity = 5,
fillColor = ~pal(species),
color = "black")
I have read up on how to add an image to a popup, but this would add the same image to all popups. What I would like to happen is once a point is clicked, a popup appears with the name of the species and a picture (either local file or web-linked, whichever is easiest)- So that each group (species) would have its own associated picture.
Is this possible?
Yes, if you want to each group to have its own image, you need to create a new column which contains URL to your image. And the important part is to use HTML img tag in popup.
See demo below.
data <- data.frame(
lng = c(-1,0,1,2),
lat = c(-1,0,1,2),
label = c("p1","p2","p3","p4"),
# some random images I picked up from google images
# it can be both local or remote
image_link = c(
"https://jessehouwing.net/content/images/size/w2000/2018/07/stackoverflow-1.png",
"https://store-images.s-microsoft.com/image/apps.18496.14408192455588579.aafb3426-654c-4eb2-b7f4-43639bdd3d75.2c522ca4-9686-4ee2-a4ac-cdbfaf92c618?mode=scale&q=90&h=1080&w=1920",
"https://mk0jobadderjftub56m0.kinstacdn.com/wp-content/uploads/stackoverflow.com-300.jpg",
# row number 4 use the same link as row number 1
"https://mk0jobadderjftub56m0.kinstacdn.com/wp-content/uploads/stackoverflow.com-300.jpg"
)
)
library(leaflet)
leaflet(data = data) %>%
addTiles() %>%
addCircleMarkers(
lng = ~lng,
lat = ~lat,
popup = ~paste0(
"<img src='",image_link,"' width='50px' height='50px'>"
)
)
You could also replicate code from here:
https://github.com/abenedetti/bioNPS

How to use the Leaflet.MarkerCluster.PlacementStrategies subplugin in R

I'm building an interactive map in Leaflet using R, and I would like to use the Leaflet.MarkerCluster.PlacementStrategies sub-plugin to control the placement of markers within a cluster (when "spiderfied" on mouse click over the cluster), and always lay them out in a circle around the cluster icon, rather than along a spiral as it happens when there are more than 8 markers in a cluster with the standard Leaflet.MarkerCluster plugin that comes embedded in the leaflet htmlWidget. The code I'm using to build the map is something like this:
library(leaflet)
data_example <- data.frame(name = rep("site A", times = 14),
lon = rep(14.25000, times = 14),
lat = rep(40.83333, times = 14),
issue = paste("Issue", LETTERS[1:14]),
severity = sample(c("Mild", "Moderate", "Severe"), size = 14, replace = TRUE))
issues_pal <- colorFactor(brewer.pal(3, "RdYlBu"), levels = c("Mild", "Moderate", "Severe"), reverse = TRUE)
leaflet(data_example, options = leafletOptions(maxZoom = 7)) %>%
setView(lng = 8, lat = 50, zoom = 4) %>%
addProviderTiles(providers$Esri.WorldStreetMap) %>%
addCircleMarkers(
lng = ~ lon,
lat = ~ lat,
radius = 10,
color = "grey",
weight = 2,
fillColor = ~ issues_pal(severity),
#stroke = FALSE,
fillOpacity = 0.8,
label = ~ paste(issue, severity),
clusterOptions = markerClusterOptions(
spiderfyDistanceMultiplier = 1.1,
spiderLegPolylineOptions = list(weight = 0)),
group = df,
clusterId = df
)
In the map, when one clicks on the cluster icon, the markers are shown placed along a spiral path originating from the cluster. This is because the Leaflet.MarkerCluster plugin that control the clustering of markers in leaflet, place them along a circle only when there are up to 8 markers. But the Leaflet.MarkerCluster.PlacementStrategies should allow to specify a different placement strategy, and to have markers laid out in a circle even if they are more than 8, as shown in this page and here.
Since the sub-plugin is not yet included in leaflet package, I'd like to use the method highlighted here to use in R any leaflet JS plugins, but I'm struggling to find a way to make it work for my case. According to the method, I should first of all include the following in my code:
placement.strategies <- htmlDependency(
"Leaflet.MarkerCluster.PlacementStrategies",
"0.0.1",
src = c(href = "https://github.com/adammertel/Leaflet.MarkerCluster.PlacementStrategies/blob/master/dist/"),
script = "leaflet-markercluster.placementstrategies.src.js"
)
registerPlugin <- function(map, plugin) {
map$dependencies <- c(map$dependencies, list(plugin))
map
}
And then I should call registerPlugin(placement.strategies) in the leaflet pipe. However, I can't figure out how to adapt the following custom JS code to my needs:
leaflet() %>% setView(lng = 8, lat = 50, zoom = 4) %>%
# Register plugin on this map instance
registerPlugin(placement.strategies) %>%
# Add your custom JS logic here. The `this` keyword
# refers to the Leaflet (JS) map object.
onRender("function(el, x) {
L.esri.basemapLayer('Topographic').addTo(this);
}")
I've tried to simply specify in the call to addCircleMarkers() the elementsPlacementStrategy = 'one-circle' as a clusterOption within the markerClusterOptions(), but it does not work. I suspect the JS code I need to call from should somehow modify the option property elementsPlacementStrategy for the cluster, but how to do that I don't know. Can anyone help with this? Thank you in advance!
post scriptum
I have found a way around to get the circle placement without using the subplugin, that involves modifying in the leaflet.markercluster.js file the _circleSpiralSwitchover property to a number greater than 14 (that is the number of markers I have in the cluster). While this workaround achieves the result I wanted, it is sub-optimal, because unless I decide to modify the default .js file that is located in:
/Library/Frameworks/R.framework/Versions/3.2/Resources/library/leaflet/htmlwidgets/plugins/Leaflet.markercluster
I would need to remake the edit on the .js file every time I export the map with saveWidget.

Save .png by webshot in R

I'm using webshot to save a png file from HTML :
p <- plot_ly(plotly::wind, r = ~r, t = ~t) %>% add_area(color = ~nms)%>%
layout(radialaxis = list(ticksuffix = "%"), orientation = 270)
saveWidget(as.widget(p), "temp.html")
webshot("temp.html",file = "temp.png",cliprect = "viewport")
This is giving me a plot like this. It returns a small image in the upper left corner. What should I do to have a full-size image in a center?

Watermark adding in R

I am using magick library in R. I want to add watermark on some pictures.
I used image_annotatefunction as below.
img <- image_read("C:\\Users\\Maydin\\Desktop\\manzara.png")
image_annotate(img, "my watermark", gravity = "northwest", location = "+200+275",
degrees = -30, size =50, font = NULL, color = "transparent",
strokecolor = "gray90", boxcolor = NULL)
At the end, the output looks like this;
However, what I want to have is something like this ,
Is that doable in magick in R?
For instance, this
download.file("https://i.stack.imgur.com/7X5To.png", tf<-tempfile(fileext = ".png"), mode="wb")
library(magick)
img <- image_read(tf)
library(extrafont)
truetype_path <- paste0("#", subset(fonttable(), FullName=="Matura MT Script Capitals", fontfile)[1,])
image_annotate(img, "my watermark", gravity = "northwest", location = "+70+220",
degrees = -30, size = 80, font = truetype_path, color = "#FFFFFF66",
strokecolor = NULL, boxcolor = NULL)
gives this image:
I.e., choose a nice font like maybe Matura MT Script Capitals, tell image_annotate where to find it on your harddrive, adjust the opacity in the color argument - et voila. The font does not drop a shadow or show a relief, but maybe you can emulate this by plotting the text two times, the dark shadow one with a little offset to the other light one.
#lukA nicely demonstrates a solution using extrafonts package with magick package, but it looks like you can refer to the font by name within image_annotate() without the clever lookup of the full path. Use extrafonts::fonttable() to find the name.
library(extrafonts)
library(magick)
#download original example file
download.file("https://i.stack.imgur.com/7X5To.png", tf<-tempfile(fileext = ".png"), mode="wb")
img <- image_read(tf)
#Use stroke to create an outline of the text with 50% alpha
magick::image_annotate(img, "Preview", location = "+100+175", degrees = -30, size=75, weight=700, font = "MonotypeCorsiva" , color = "transparent",
strokecolor = "#00000050", boxcolor = NULL)

Resources