Map highcharter
highcharter: add a map series
library("dplyr")
library(highcharter)
data("USArrests", package = "datasets")
data("usgeojson")
USArrests <- mutate(USArrests, state = rownames(USArrests))
highchart() %>%
hc_title(text = "Violent Crime Rates by US State") %>%
hc_subtitle(text = "Source: USArrests data") %>%
hc_add_series_map(usgeojson, USArrests,
name = "Murder arrests (per 100,000)",
value = "Murder", joinBy = c("woename", "state"),
dataLabels = list(
enabled = TRUE,
format = "{point.properties.postalcode}"
)
) %>%
hc_colorAxis(stops = color_stops()) %>%
hc_legend(valueDecimals = 0, valueSuffix = "%") %>%
hc_mapNavigation(enabled = TRUE)
ggmap
library(ggmap)
MapLocation <-c(mean(M3$Longitude),mean(M3$Latitude))
MapType <- "roadmap"
MapSource <- "google"
Box <- c(min(M3$Longitude),min(M3$Latitude),max(M3$Longitude),max(M3$Latitude))
mapImageData3 <- get_map(location = Box
,color = "bw",
source = MapSource,
maptype = MapType,
zoom = 8)
M3$IDc <- as.character(M3$ID)
## Join assignment to the table
M4 <- sqldf::sqldf("
select a.*
,b.assignment
from M3 a
left join MP3 b on a.ID = b.ID
")
assign.color <- function(x){
if(x == 1){return("cornflowerblue")}else{return("bisque3")}
}
M4$Color <- sapply(M4$Assignment,assign.color)
M4 <- M4[order(M4$pair_id),]
M4$Group <- rep(c(0,1),12)
Line.x <- M4[M4$Group == 0,c("ID","pair_id","Longitude")]
Line.y <- M4$Latitude[M4$Group == 0]
Line.xend<- M4[M4$Group == 1,c("ID","Longitude")]
Line.yend <- M4$Latitude[M4$Group == 1]
M4.seg <- as.data.frame(cbind(Line.x,Line.y,Line.xend,Line.yend))
colnames(M4.seg) <- c("ID","pair_id","x","y","ID2","xend","yend")
png(filename = "Map_final.png", width = 9, height = 6.7, units = "in", res=240)
ggmap(mapImageData3,
extent = "device",
ylab = "Latitude",
xlab = "Longitude")+
geom_point(aes(x =Longitude, y = Latitude),data=M4,
alpha = .8, color=M4$Color, size = 5) +
geom_curve(aes(x=x,y=y
,xend=xend,yend=yend,colour="curve")
,data=M4.seg
,curvature= 0.4
,lineend="square"
,alpha = .8, color="darkblue", size = 0.5) +
annotate("text"
,label=M4$IDc
, x =M4$Longitude
, y = M4$Latitude
)+
coord_cartesian()
dev.off()
R sessionInfo
R version 4.2.0 (2022-04-22) Platform: x86_64-pc-linux-gnu (64-bit) Running under: Ubuntu 20.04.3 LTS
Matrix products: default BLAS: /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0 LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
locale: [1] LC_CTYPE=C.UTF-8 LC_NUMERIC=C LC_TIME=C.UTF-8
[4] LC_COLLATE=C.UTF-8 LC_MONETARY=C.UTF-8 LC_MESSAGES=C.UTF-8
[7] LC_PAPER=C.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=C.UTF-8 LC_IDENTIFICATION=C
attached base packages: [1] stats graphics grDevices utils datasets methods base
other attached packages: [1] highcharter_0.9.4 Wu_0.0.0.9000 flexdashboard_0.5.2 [4] lme4_1.1-29 Matrix_1.4-0 mgcv_1.8-38
[7] nlme_3.1-152 png_0.1-7 scales_1.2.0
[10] nnet_7.3-16 labelled_2.9.1 kableExtra_1.3.4
[13] plotly_4.10.0 gridExtra_2.3 ggplot2_3.3.6
[16] DT_0.23 tableone_0.13.2 magrittr_2.0.3
[19] lubridate_1.8.0 dplyr_1.0.9 plyr_1.8.7
[22] data.table_1.14.2 rmdformats_1.0.4 knitr_1.39
loaded via a namespace (and not attached): [1] xts_0.12.1 webshot_0.5.3 httr_1.4.3 tools_4.2.0
[5] backports_1.4.1 bslib_0.3.1 utf8_1.2.2 R6_2.5.1
[9] DBI_1.1.2 lazyeval_0.2.2 colorspace_2.0-3 withr_2.5.0
[13] tidyselect_1.1.2 curl_4.3.2 compiler_4.2.0 cli_3.3.0
[17] rvest_1.0.2 xml2_1.3.3 bookdown_0.27 sass_0.4.1
[21] systemfonts_1.0.4 stringr_1.4.0 digest_0.6.29 minqa_1.2.4
[25] rmarkdown_2.14 svglite_2.1.0 pkgconfig_2.0.3 htmltools_0.5.3
[29] fastmap_1.1.0 htmlwidgets_1.5.4 rlang_1.0.4 TTR_0.24.3
[33] rstudioapi_0.13 quantmod_0.4.20 jquerylib_0.1.4 generics_0.1.3
[37] zoo_1.8-10 jsonlite_1.8.0 rlist_0.4.6.2 Rcpp_1.0.9
[41] munsell_0.5.0 fansi_1.0.3 lifecycle_1.0.1 stringi_1.7.8
[45] yaml_2.3.5 MASS_7.3-54 grid_4.2.0 forcats_0.5.1
[49] lattice_0.20-45 haven_2.5.0 splines_4.2.0 hms_1.1.1
[53] klippy_0.0.0.9500 pillar_1.8.0 igraph_1.3.4 boot_1.3-28
[57] glue_1.6.2 evaluate_0.15 mitools_2.4 vctrs_0.4.1
[61] nloptr_2.0.3 gtable_0.3.0 purrr_0.3.4 tidyr_1.2.0
[65] assertthat_0.2.1 xfun_0.31 broom_0.8.0 survey_4.1-1
[69] survival_3.2-13 viridisLite_0.4.0 tibble_3.1.8 ellipsis_0.3.2