I am working on covid dataset and have mapped the data on Indian Map using highcharts but that Map is not latest & doesn't show all its States - for example Telengana which was formed ~7 years back is missing in the map.
Highchart Map link: https://code.highcharts.com/mapdata/countries/in/in-all
hcmap(
"countries/in/in-all",
data = (df_ind %>% filter(Date == max(Date, na.rm = TRUE),
State.UnionTerritory != "India"
)
),
value = Daily_confirmed,
joinBy = c("hc-a2"),
datalabels = list(enabled = TRUE, format = "{point.name}"),
borderColor = "#FAFAFA",
borderWidth = 0.1,
tooltip = list(
# valueDecimals = 2,
# valuePrefix = "$"
)
) %>%
hc_colorAxis(minColor = "midnightblue", maxColor = "orange")
I did find another Map of India on highcharts containing State - Telengana but whenever I use that all the mapped data on it gets messed up:
https://code.highcharts.com/mapdata/
https://code.highcharts.com/mapdata/countries/in/custom/in-all-disputed.svg
"countries/in/custom/in-all-disputed"
If I use this link & check hc-a2 field then turns out that State Chandigarh & Chattisgarh have same CHvalue in hc-a2.
hc-a2 n
<chr> <int>
AP 2
CH 2
DA 2
MA 2
PU 2
Basically there are issues with the mapping data which I can change it now manually or may be re map it by using names but this will not be good for future use as I may need to change it everytime.
So is there a more consistent form of Map for India in highcharts that I can refer to ??
Related
I am working on making a plotly map of the US with hover tooltips which I have gotten to work somewhat. However, I have multiple observations per state for each variable I would like to display in the tooltip and currently only the first observation for each state is displayed. Each observation is the performance of a candidate in the 1976 presidential election in a state, and I would like the hover tooltip to display each candidates performance in the state instead of just the first candidate listed in that state.
Here is the code I am using at the moment.
candidate denotes the name of the candidate , state_share and round_share denote the percent of the state popular vote and state electoral votes the candidate receives respectively.
library(plotly)
colorscale <- c("blue" , "purple" , "red")
l <- list(color = toRGB("white"), width = 2)
# specify some map projection/options
g <- list(
scope = 'usa',
projection = list(type = 'albers usa'),
showlakes = FALSE
)
threshold10$hover <- with(threshold10, paste(state, "<br>" , canvec ,':',
state_share ,",","Electoral Votes", round_share ))
fig <- plot_geo(subset(threshold10, year == 1976), locationmode = 'USA-states')
fig <- fig %>%
add_trace(
z = ~evotes, text = ~ hover , locations = ~state_po,
color = ~state_share , colors = colorscale) %>%
layout(title = '1976 Electoral Vote Allocation <br> 10% State Threshold',
geo = g)
fig
I'm also attaching an image of the dataset and the map produced by my code. I appreciate any help anyone has to offer. I am newish to working with plotly and mapping so if this is a simple question sorry about that. Thank you for your help.
dataframe:
map output:
I have a dataset that includes both a date and a species for each bird observed in a county. I've mapped them using leaflet, but want to use two AddLayersControl to control for both the date and the species. Right now I can only control for the year or the species. I would like the second group of checkboxes so I can control the species as well. I want the marker to go away if either its year group is unchecked or its species group is unchecked.
What I think I need to do is to assign each marker to two different groups that I could control independently. I don't think I am able to assign certain markers as base layers because I don't want a certain subset of them always available. I have also tried just adding another AddLayersControl - sadly the second one will always win and it doesn't seem like you can have two on the same map.
library(leaflet)
library(magrittr)
library(dplyr)
library(htmltools)
# Data
birds <- data.frame(observed_on = c("4/4/2009",
"4/1/2009",
"3/6/2016",
"2/9/2016"),
url = c("http://www.inaturalist.org/observations/2236",
"http://www.inaturalist.org/observations/2237",
"http://www.inaturalist.org/observations/2778201",
"https://www.inaturalist.org/observations/9796150"),
latitude = c(43.08267975,
43.0844841,
43.055512,
43.0180932),
longitude = c(-89.43265533,
-89.43793488,
-89.314878,
-89.52836138),
scientific_name = c("Agelaius phoeniceus",
"Bubo virginianus",
"Quiscalus quiscula",
"Strix varia"),
common_name = c("Red-winged Blackbird",
"Great Horned Owl",
"Common Grackle",
"Barred Owl"),
taxon_order_name = c("Passeriformes",
"Strigiformes",
"Passeriformes",
"Strigiformes"),
taxon_species_name = c("Agelaius phoeniceus",
"Bubo virginianus",
"Quiscalus quiscula",
"Strix varia" ),
year = c("2009", "2009", "2016", "2016"))
# Leaflet Chart Formatting --------------------------------------------------------
palette <- colorFactor(palette = rainbow(length(unique(birds$taxon_order_name))),
domain = birds$taxon_order_name)
# Leaflet Chart -------------------------------------------------------------------
mymap <- leaflet(birds) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
setView(lng = -89.398721,
lat = 43.071580,
zoom = 13)
for (t in unique(birds$year)){
sub <- birds[birds$year == t,]
labels <- mapply(function(x, y, z, a) {
HTML(sprintf("%s<br><em>%s</em><br>%s<br><a href=%s>link</a>",
htmlEscape(x),
htmlEscape(y),
htmlEscape(z),
htmlEscape(a)))},
sub$common_name,
sub$taxon_species_name,
sub$observed_on,
sub$url,
SIMPLIFY = FALSE)
mymap <- mymap %>%
addCircleMarkers(data = sub,
lng = ~longitude,
lat = ~latitude,
fillOpacity = 0.6,
radius = 8,
fillColor = ~palette(taxon_order_name),
color = "black",
weight = 1,
opacity = 0.5,
popup = labels,
group = as.character(t))
}
mymap %>%
addLegend(pal = palette,
values = ~taxon_order_name,
title = "Taxon Order") %>%
addLayersControl(overlayGroups = as.character(unique(birds$year)),
options = layersControlOptions(collapsed = FALSE))
# addLayersControl(overlayGroups = unique(birds$taxon_order_name), options = layersControlOptions(collapsed = FALSE))
map showing points with both year and species info but layers control for the only year
does this work?
addLayersControl(overlayGroups = as.character(c(unique(birds$year),unique(birds$taxon_order_name)), options = layersControlOptions(collapsed = FALSE))
Hello all experts out there,
I am very new to R and leaflet
I would like to create a shiny dashboard and within it, I want to create a map with leaflet. Here is how I want it to be, first, the user will pick a state, then, you may or may not pick a county. So if they pick a state I would like to use setview() to just look at the chosen state, and if they pick a state and a county, then setview() to look at the chosen county.
So first I want to make sure my leaflet code works first, before I embed it into shiny server. So I ran the following code assuming that the user picks California state and county Los Angeles.
I got the following error and not sure how to fix it.
Any suggestion? Thank you so much in advance!
Error in cut.default(x, binsToUse, labels = FALSE, include.lowest = TRUE, :
'breaks' are not unique
> head(lonlat)
STATE COUNTY county.State lon lat
1 AK KENAI PENINSULA KENAI PENINSULA,AK -151.3044 60.58293
2 AK ANCHORAGE ANCHORAGE,AK -149.8557 61.22002
3 AK DENALI DENALI,AK -145.8662 63.04024
4 AK BETHEL BETHEL,AK -161.7558 60.79222
5 AK MATANUSKA-SUSITNA MATANUSKA-SUSITNA,AK -150.5125 61.54361
6 AK KODIAK ISLAND KODIAK ISLAND,AK -152.3539 57.80459
>
states <- readOGR("cb_2015_us_state_20m.shp",
layer = "cb_2015_us_state_20m", GDAL1_integer64_policy = TRUE)
states <- subset(states, states$STUSPS %in% c("CA"))
class(states)
leaflet(data = map_dat[map_dat$STATE %in% "CA" &
map_dat$COUNTY %in% "LOS ANGELES",]) %>%
addProviderTiles(
providers$Stamen.TonerLite,
options = providerTileOptions(noWrap = TRUE)
) %>%
addPolygons(data=states,color = "#444444", weight = 1, smoothFactor = 0.5,
opacity = 1.0, fillOpacity = 0.5,
fillColor = ~colorQuantile("YlOrRd", ALAND)(ALAND),
highlightOptions = highlightOptions(color = "white", weight = 2,
bringToFront = TRUE))
selectedCounty <- lonlat[lonlat$COUNTY == "LOS ANGELES" &&
lonlat$STATE == "CA", ]
#leafletProxy(mapId = "allmap") %>%
setView(lng = selectedCounty$lon, lat = selectedCounty$lat, zoom = 5)
I would like to merge regions in a map and not display intra borders. I also would like to always display the names of the newly grouped regions.
Is hc_add_series_map the right instruction for doing so ?
Thanks in advance for your help
(I do not use Java for programming, only R)
Here is an example :
mapdata <- get_data_from_map(download_map_data("countries/fr/fr-all-all"))
glimpse(mapdata)
set.seed(1234)
data_fake <- mapdata %>%
select(code = `hc-a2`) %>%
mutate(value = 1e5 * abs(rt(nrow(.), df = 10)))
glimpse(data_fake)
hcmap("countries/fr/fr-all-all", data = data_fake, value = "value",
joinBy = c("hc-a2", "code"), name = "Fake data",
dataLabels = list(enabled = TRUE, format = '{point.code}'),
borderColor = "#FAFAFA", borderWidth = 0.1,
tooltip = list(valueDecimals = 2))
enter image description here
I would like to add borders for 5 grouped "big" regions, either by deleting 'intra borders' in a region, or adding black lined borders for big regions.
Thanks in advance.
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!!