R Programming: Using ISO Country Codes in LeafLet - r

I am working on creating a client dashboard. I have ISO country codes for the clients also I have plotted the same in the map using rworldmap package, but the UI is not very good.
So, I want to use the leaflet package. How can I use these ISO Country Codes ALPHA 2 in creating the map.
Thanks!

Leaflet does not accept ISO Alpa2 code rather accepts ISO Alpha3 codes. After going through almost everywhere I tried this and it solved my problem.
output$myMapOne = renderPlotly({
height = 1000
units="px"
clientName = input$clientSelector
conWiseSub = subset(conData, conData$GCA_CSTMR_DS == clientName)
defOne = aggregate(CNT ~ CODE, conWiseSub, sum)
d = defOne$CODE
e = defOne$CNT
# light grey boundaries
l <- list(color = toRGB("grey"), width = 0.5)
# specify map projection/options
g <- list(
showframe = TRUE,
showcoastlines = FALSE,showland = TRUE,showcountries = TRUE,
countrycolor = toRGB("white"),
landcolor = toRGB("grey85"),
projection = list(type = 'Mercator', scale =1)
)
plot_ly(defOne, z = e, text = d,locations = d, type = 'choropleth',
color = e, colors = 'PuBu', marker = list(line = l), colorbar = list(title = "SOI Distribution")
) %>%
layout( geo = g,title= paste("Region Wise SOI Distribution of", clientName , sep = " "))
})
Click Here to View the Map Created By the Code
Hope this helps!!

Related

Adding title to simple_visNet

I am using simple_visNet to represent data but I am not able to add a title to my graph. Any idea?
simu_data_mapper <- mapper.sta(dat = data,
filter_values = filt,
num_intervals = 10,
percent_overlap = 70)
simple_visNet(simu_data_mapper, color_filter = FALSE)

Is there a way to add a click event to open a url in a highcharter plot without shiny?

I am trying to make a map plot using highcharter in which the points can be clicked to access their associated url. I've created the following simplified version of what I am doing (I'm doing a mapbubble plot instead) to illustrate my code. What am I doing wrong? Note: I have also tried this.point.options.url in the javascript section of the code.
library(dplyr)
library(highcharter)
data("USArrests", package = "datasets")
USArrests = mutate(USArrests, "woe-name" = rownames(USArrests))
USArrests[["url"]] = c("https://www.google.com/", "https://www.wikipedia.org/")
hcmap(map = "countries/us/us-all", data = USArrests,
joinBy = "woe-name", value = "UrbanPop", name = "Urban Population",
allowPointSelect = TRUE) %>%
hc_plotOptions(
point = list(
events = list(
click = JS("function() {
window.open(point.url);
}"
)
)
)
)
There are 2 issues with your code:
The map tile is a map and not a point, thus, your option file should link the event handler to map and not point.
To access the urlyou have to use a parameter in your JS function via which you can access the url eventually.
hcmap(map = "countries/us/us-all", data = USArrests,
joinBy = "woe-name", value = "UrbanPop", name = "Urban Population",
allowPointSelect = TRUE) %>%
hc_plotOptions(
map = list(
events = list(
click = JS("function(self) {
window.open(self.point.url);
}"
)
)
)
)

How do I filter countries/us/us-all-all to only show ca and nv?

I can get all US counties and I can get one state, but I can't get all the counties from two states into the same map. Here is my R code:
hcmap("countries/us/us-all-all", data = dataframe, value = "value1",
joinBy = c("name"), name = "Name",
dataLabels = list(enabled = TRUE, format = "{point.name}"),
tooltip = list(valueDecimals = 0, valueSuffix = "%"),
pointFormat = "County: {point.name}<br/>{point.value1}")
I tried downloading each state's data:
camapdata <- get_data_from_map(download_map_data("countries/us/us-ca-all"))
nvmapdata <- get_data_from_map(download_map_data("countries/us/us-nv-all"))
Then combining into one data set, but then hcmap errors because it's not a URL.
I can also download and filter the whole US map:
mapdata <- get_data_from_map(download_map_data("countries/us/us-all-all"))
canvmap <- filter(mapdata, `hc-key` == "us-ca*")
but get the same problem.
Is there a way to filter the US county map to specific states within the hcmap function?
This is the best I have so far--but I can only get CA and NV in one map...
camapdata <- get_data_from_map(download_map_data("countries/us/us-ca-all"))
nvmapdata <- get_data_from_map(download_map_data("countries/us/us-nv-all"))
states2<-join(camapdata,nvmapdata,by=c("fips"), type="full",match="all")
setnames(states2, old=c("hc-a2"), new=c("STABBR"))
setnames(states2, old=c("hc-middle-y"), new=c("HCMIDDLE"))
XXX<-sqldf("select * from states2 where STABBR in ('CA','OR')")
library(highcharter)
hcmap("countries/us/us-all-all", data = states2, value = "HCMIDDLE",
joinBy = c("name"), name = "Name",
dataLabels = list(enabled = TRUE, format = "{point.name}"),
tooltip = list(valueDecimals = 0, valueSuffix = "%"),
pointFormat = "County: {point.name}<br/>{point.value1}")

Why aren't event.lines showing up when using chart.TimeSeries?

I am trying to plot various events over a times series data set using chart.TimeSeries in the PerformanceAnalytics library. I have tried to follow the example from but I think I am missing something.
ExampleChart MyChart
Neither my vertical event lines nor are my period areas are showing up on the chart. My code is almost identical to the example code. Any ideas?
risk.dates = c(
"2015-04-19"
)
risk.labels = c(
"Jon Oliver's Patent Episode "
)
cycle.dates <- c ("2015-1-01/2015-01-31",
"2015-03-01/2015-03-31",
"2015-05-01/2015-05-31",
"2015-07-01/2015-07-31"
)
chart.TimeSeries(df, colorset = "darkblue",
date.format.in = "%Y-%m-%d",
legend.loc = NULL,
period.areas = cycles.dates,
period.color = "lightblue",
event.lines = risk.dates,
event.labels = risk.labels,
event.color = "red", lwd = 2,
main = "Reddit Sentiment Index",
auto.grid = FALSE)
df <- data.frame(DateTime=
c("2/17/2015","2/18/2015","2/19/2015",
"2/20/2015","2/21/2015","2/22/2015",
"2/23/2015","2/24/2015","2/25/2015",
"2/26/2015","2/27/2015","2/28/2015",
"2/28/2015","2/27/2015"),
AvgSentimentScore=
c("NA","NA","NA","NA","NA","NA","NA","NA","NA",
"8.405975696","8.313470536","8.414560335",
"8.720865075","9.284902246"))

Personalise the magnifying glass

I use the googleVis package to plot a map of France which I embedded in a shiny app. But the thinner division is by regions and i have to represents departments also. I actually use .shp files to plot and render each regions divided by departments, but I want to make my outputs more interactive and aesthetically pleasing.
Is there a way to force the magnifying glass when i hover over a region and to personalise the content of it (display a region map inside for example) ? Or to construct an equivalent.
Probaly not with R directly...
library(googleVis)
df <- data.frame(departement = c("Alpes-Maritimes", "Bouches-du-Rhone", "Rhone", "Savoie", "Haute-Savoie"), region = c(rep("Provence-Alpes-Cote d'Azur", 2), rep("Rhone-Alpes", 3)),
ISO_3166.2 = c(rep("FR-U", 2), rep("FR-V", 3)), x = rnorm(5))
dfAggreg <- aggregate(df$x, by = list(region = df$region, iso = df$ISO_3166.2), sum)
plot(gvisGeoChart(dfAggreg,
locationvar = "iso", colorvar = "x", hovervar = "region",
options = list(region = "FR", displayMode = "regions",
resolution = "provinces",
width = 500, height = 400,
colorAxis = "{colors:['#FFFFFF', '#0000FF']}"
)))

Resources