|
| 1 | +if(getRversion() >= "2.15.1") utils::globalVariables(c("long", "lat", "group", "prop")) |
| 2 | + |
| 3 | +#' @title Plot map using IP2Location data. |
| 4 | +#' |
| 5 | +#' @description Plot the country on the map based on IP addresses and its IP2Location country data. |
| 6 | +#' @param ips A vector of IP addresses to be plot on |
| 7 | +#' @return NULL |
| 8 | +#' @import maps |
| 9 | +#' @import ggplot2 |
| 10 | +#' @import scales |
| 11 | +#' @import utils |
| 12 | +#' @export |
| 13 | +#' @examples \dontrun{ |
| 14 | +#' plot_map(c("1.0.241.135", "1.2.3.4")) |
| 15 | +#' } |
| 16 | +#' |
| 17 | + |
| 18 | +plot_map <- function(ips){ |
| 19 | + countries = c() |
| 20 | + mapData = map_data("world") |
| 21 | + for ( i in ips ) { |
| 22 | + result = ip2proxy::get_all(i) |
| 23 | + countries = append(countries, toString(result["country_long"])) |
| 24 | + } |
| 25 | + |
| 26 | + country_table <- table(countries) |
| 27 | + ipData <- data.frame(country_table) |
| 28 | + |
| 29 | + variable1 = deparse(substitute(countries)) |
| 30 | + variable1 = strsplit(variable1, "\\$")[[1]][2] |
| 31 | + |
| 32 | + if(ncol(ipData) == 2) { |
| 33 | + names(ipData) <- c(variable1, "n") |
| 34 | + } else if(ncol(ipData) > 2){ |
| 35 | + names(ipData)[length(ipData)] <- "n" |
| 36 | + } |
| 37 | + |
| 38 | + prop <- as.vector(country_table)/sum(country_table) |
| 39 | + ipData <- data.frame(ipData, prop) |
| 40 | + |
| 41 | + table.prop <- as.vector(country_table)/sum(country_table) |
| 42 | + table.perc <- format(round(table.prop*100, 1), nsmall = 1) |
| 43 | + table.perc <- gsub("$", "%", table.perc) |
| 44 | + ipData <- data.frame(ipData, table.perc) |
| 45 | + |
| 46 | + names(ipData)[1] = "group" |
| 47 | + |
| 48 | + path = find.package("ip2proxy") |
| 49 | + |
| 50 | + #data <- read.csv("inst\\countrynames_mapping.txt", header=TRUE) |
| 51 | + data <- read.csv(paste(path, '/countrynames_mapping.txt', sep = ""), header=TRUE) |
| 52 | + |
| 53 | + data$matched_country_name[data$matched_country_name == ''] <- NA |
| 54 | + |
| 55 | + for(i in 1:nrow(data)){ |
| 56 | + if(!is.na(data[i, "matched_country_name"])){ |
| 57 | + old.name <- paste(data[i, "country_name"]) |
| 58 | + new.name <- paste(data[i, "matched_country_name"]) |
| 59 | + ipData[, "group"] <- gsub(old.name, new.name, ipData[, "group"]) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + worldMapIPs <- merge(mapData, ipData, by.x = "region", by.y = "group", all.x = TRUE) |
| 64 | + worldMapIPs <- worldMapIPs[order(worldMapIPs[, "order"]), ] |
| 65 | + worldMapIPs[is.na(worldMapIPs)] <- 0 |
| 66 | + world <- map_data("world") |
| 67 | + p <- ggplot() + |
| 68 | + geom_polygon(data=world, aes(x=long, y=lat, group = group), |
| 69 | + colour = "#595959", fill = "white") + theme(panel.background = element_rect(fill = "#b2cce5"), plot.background = element_rect(fill = "#b2cce5")) |
| 70 | + |
| 71 | + mapcolors = c("black", "#d6d6d6", "white") |
| 72 | + mapvalues = c(1, .025, 0) |
| 73 | + |
| 74 | + |
| 75 | + p + geom_polygon(data=worldMapIPs, aes(x=long, y=lat, group = group, fill = prop)) + geom_path(data=worldMapIPs, aes(x=long, y=lat, group=group), color="#595959", size=0.05) + scale_fill_gradientn(colours= mapcolors, values= mapvalues, labels = percent_format(), limits = c(0,1)) + theme(plot.title = element_text(size = 20, colour = "black", family = "sans", margin = unit(c(0, 0, 5, 0), "mm"))) + scale_y_continuous(name=NULL, breaks=NULL, expand = c(0,0)) + scale_x_continuous(name=NULL, breaks=NULL, expand = c(0,0)) + theme(legend.justification = c(0, 0), legend.position = c(0, 0.32), legend.background = element_blank(), legend.title = element_blank(), legend.text = element_text(size = rel(1.1), colour = "black", family = "sans")) + guides(fill = guide_colorbar(barwidth = rel(0.5), barheight = rel(5.0), ticks = F)) |
| 76 | +} |
0 commit comments