Skip to content

Commit

Permalink
version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Luigi-Annic authored and cran-robot committed Feb 14, 2024
0 parents commit 106f5cb
Show file tree
Hide file tree
Showing 36 changed files with 1,229 additions and 0 deletions.
28 changes: 28 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Package: WorldMapR
Type: Package
Title: Worldwide or Coordinates-Based Heat Maps
Version: 0.1.0
Authors@R: c(
person("Luigi", "Annicchiarico", email = "luigi.annic@gmail.com", role = c("cre", "aut"))
)
Description: Easily plot heat maps of the world, based on continuous or categorical data. Country labels can also be added to the map.
License: GPL-3
URL: https://github.com/Luigi-Annic/WorldMapR/
BugReports: https://github.com/Luigi-Annic/WorldMapR/issues
Encoding: UTF-8
Depends: R (>= 4.3.0)
Imports: ggplot2 (>= 3.4.4), dplyr (>= 1.1.4), rnaturalearth (>=
1.0.1), sf (>= 1.0-14), countrycode (>= 1.5.0), utils (>=
4.3.0)
LazyData: true
RoxygenNote: 7.2.3
Suggests: knitr, rmarkdown, testthat (>= 3.0.0), rnaturalearthdata (>=
1.0.0)
VignetteBuilder: knitr
Config/testthat/edition: 3
NeedsCompilation: no
Packaged: 2024-02-12 20:48:43 UTC; luigi
Author: Luigi Annicchiarico [cre, aut]
Maintainer: Luigi Annicchiarico <luigi.annic@gmail.com>
Repository: CRAN
Date/Publication: 2024-02-13 17:12:45 UTC
35 changes: 35 additions & 0 deletions MD5
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
710df204b2b78b5f7fbc11b71e975447 *DESCRIPTION
43476396ae78fec9a54a3cb87501ed48 *NAMESPACE
0372cea5d53ee53045bb79edd6c3eea5 *NEWS.md
d8e2af7df075f79ebbd1b1a62aa7e880 *R/geometries_data.R
4ad7a9373c94408e2dfcf6141b371587 *R/globals.R
6d278c77cd320a36926d5072398f01b7 *R/testdata1.R
e5fc870ab22ea68b80d023f4e681cbc2 *R/testdata1b.R
8c117797e868c753c69399c83dc89845 *R/testdata1c.R
43049579511465d6b854375d2e5037c8 *R/worldplot.R
0d645140b10983bb59df0086e276982b *R/worldplotCat.R
28e5810ce7c03b1475886bc2d29cd7bb *README.md
cefe2f4f6243b80e71654542d559df95 *build/vignette.rds
08f4149f2530f0d0ef3dc006a6b0b692 *data/testdata1.RData
9f5d29690f663dc2e8b5150963ed20a0 *data/testdata1b.RData
f7f6f6477e0ceef01edab7eb53dd3913 *data/testdata1c.RData
724df204ebd894e0a4cd35c80676d31d *inst/doc/WorldMapR_Vignette.R
4eea6fbdeb68e2c00253228ca4b2608d *inst/doc/WorldMapR_Vignette.Rmd
ecf6704e651e249dbcfb1c2400812423 *inst/doc/WorldMapR_Vignette.html
88315f7c257bda68a4e365fa854248b0 *man/geometries_data.Rd
3642130008594cf62a44797761943e76 *man/testdata1.Rd
47e766c473a1a7faf0364387ccb25153 *man/testdata1b.Rd
ae58395f77c37ef095c7a981cd092767 *man/testdata1c.Rd
6631ea7be6bc2c30867b4b0c30e9a048 *man/worldplot.Rd
418f2108260faa3b716a2bbc59844d96 *man/worldplotCat.Rd
bbf8a0e4e21f0235816a3ec9ead378fd *tests/testthat.R
179ee19050fd69c99a16a5d44efa12dc *tests/testthat/test1.R
92dc8b5d92a017a13c589da237e993be *tests/testthat/test2.R
70fd20eb6813aaf207fc52bb36c8841c *tests/testthat/test3.R
fdbfeb4a85fb52f94aae0dbb5cb5e101 *tests/testthat/test4.R
5b5900cb1c8cf21adbb5048883d89eee *tests/testthat/test5.R
da9342a10a354f7e54cc7de22c7be43a *tests/testthat/test6.R
c7a21570d0776ea70ba2f0424653f1b0 *tests/testthat/testCat1.R
b2f2a5023158392cbd52277109f5a537 *tests/testthat/testCat2.R
94121e662c1821729190b8c9989b7b61 *tests/testthat/test_geometry_data.R
4eea6fbdeb68e2c00253228ca4b2608d *vignettes/WorldMapR_Vignette.Rmd
34 changes: 34 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Generated by roxygen2: do not edit by hand

export(geometries_data)
export(worldplot)
export(worldplotCat)
importFrom(countrycode,countrycode)
importFrom(dplyr,"%>%")
importFrom(dplyr,arrange)
importFrom(dplyr,filter)
importFrom(dplyr,left_join)
importFrom(dplyr,mutate)
importFrom(dplyr,relocate)
importFrom(dplyr,select)
importFrom(ggplot2,aes)
importFrom(ggplot2,coord_sf)
importFrom(ggplot2,element_blank)
importFrom(ggplot2,element_rect)
importFrom(ggplot2,element_text)
importFrom(ggplot2,geom_sf)
importFrom(ggplot2,geom_text)
importFrom(ggplot2,ggplot)
importFrom(ggplot2,ggsave)
importFrom(ggplot2,ggtitle)
importFrom(ggplot2,labs)
importFrom(ggplot2,scale_fill_viridis_c)
importFrom(ggplot2,scale_fill_viridis_d)
importFrom(ggplot2,theme)
importFrom(ggplot2,unit)
importFrom(ggplot2,xlab)
importFrom(ggplot2,ylab)
importFrom(rnaturalearth,ne_countries)
importFrom(sf,st_centroid)
importFrom(sf,st_coordinates)
importFrom(utils,globalVariables)
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# WorldMapR 0.1.0

* Initial CRAN submission.
70 changes: 70 additions & 0 deletions R/geometries_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#' @title geometries_data
#'
#' @description
#' This function generates a data frame with information about geometries and centroid coordinates of
#' countries. You can choose whether to keep all the countries or only a subset.
#'
#' @param exclude.iso.na if \code{TRUE} (default), countries that do not have a ISO 3166 code are excluded from the table.
#' @param countries.list List of the ISO 3166-1 alpha-2 codes of countries that are to be included. By default it is set to \code{NULL} and all countries are included.

#' @return an object of class \code{data.frame} and \code{sf}.
#' @export
#' @importFrom rnaturalearth ne_countries
#' @importFrom dplyr "%>%" select filter mutate arrange
#' @importFrom sf st_centroid st_coordinates
#'
#'
#' @examples
#' geometries_data(countries.list = c("IT", "FR", "US"))
#'

geometries_data <- function(exclude.iso.na = TRUE,
countries.list = NULL) {
world <- ne_countries(scale = 50, continent = NULL, returnclass = "sf")

map_df0<- world %>%
select(name, iso_a2_eh, iso_a3_eh, geometry) %>%
mutate(iso_a2 = ifelse(name %in% c("Indian Ocean Ter." , "Ashmore and Cartier Is."), -99, iso_a2_eh),
iso_a3 = ifelse(name %in% c("Indian Ocean Ter." , "Ashmore and Cartier Is."), -99, iso_a3_eh)) %>%
select(name, iso_a2, iso_a3, geometry)



sepNat <- c('AQ', 'FJ', 'FR', 'IN', 'RU', 'SD', 'SN', 'SS')

point_nations<- map_df0 %>%
filter(!(#is.na(iso_a2) |
iso_a2 %in% sepNat))

world_points0<- cbind(point_nations, st_coordinates(st_centroid(point_nations$geometry)))

leftout <- map_df0 %>%
filter(iso_a2 %in% sepNat) %>%
arrange(iso_a2) %>%
mutate( X = c(0, 178, 2, 79, 40, 30, -14, 31),
Y = c(-80, -17, 46, 21, 55, 12, 14, 7)) %>%
relocate(geometry, .after = Y)


world_points <- rbind(world_points0, leftout)

if (exclude.iso.na == TRUE) {
world_points <- world_points %>%
filter(!(is.na(iso_a2) | iso_a2 == -99))
}

if (!is.null(countries.list)) {
world_points <- world_points %>%
filter(iso_a2 %in% countries.list)

notfoundcodes <- countries.list[!(countries.list %in% world_points$iso_a2)]

if (length(notfoundcodes) > 0) {
warning("One or more iso2 codes you provided (listed above) do not match in the data base")
message(notfoundcodes)
}
}

return(world_points)

}
5 changes: 5 additions & 0 deletions R/globals.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#' @importFrom utils globalVariables
#'
utils::globalVariables(c("name", "iso_a2", "iso_a3", "geometry",
"MapFiller", "X", "Y",
"iso_a2_eh", "iso_a3_eh"))
15 changes: 15 additions & 0 deletions R/testdata1.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#' Simulated data set 1
#'
#' Data from a random simulation with continuous data.
#'
#' @docType data
#'
#' @usage data(testdata1)
#'
#' @format An object of class \code{data.frame}
#' @keywords datasets
#'
#' @examples
#' data(testdata1)
#' head(testdata1)
"testdata1"
15 changes: 15 additions & 0 deletions R/testdata1b.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#' Simulated data set 1b
#'
#' Data from a random simulation with continuous and categorical data.
#'
#' @docType data
#'
#' @usage data(testdata1b)
#'
#' @format An object of class \code{data.frame}
#' @keywords datasets
#'
#' @examples
#' data(testdata1b)
#' head(testdata1b)
"testdata1b"
16 changes: 16 additions & 0 deletions R/testdata1c.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#' Simulated data set 1c
#'
#' Data from a random simulation with continuous and categorical data. This data set contains information
#' about 237 countries (countries without unique ISO 3166 code are excluded).
#'
#' @docType data
#'
#' @usage data(testdata1c)
#'
#' @format An object of class \code{data.frame}
#' @keywords datasets
#'
#' @examples
#' data(testdata1c)
#' head(testdata1c)
"testdata1c"
115 changes: 115 additions & 0 deletions R/worldplot.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
#' @title worldplot
#'
#' @description Plot a world heat map based on a continuous variable.
#'
#' @param data Data set containing the list of nations and the variable that we want to plot.
#' @param ColName Character variable with the name of the variable of interest.
#' @param CountryName Character variable with the name of the country names column.
#' @param CountryNameType Character variable with the coding for \code{CountryName}. One of \code{isoa2} (default), \code{isoa3}, or \code{name}.
#' @param rangeVal Limit values that are to be defined for the map.
#' @param longitude Longitude limits. Default is \code{c(-180, 180)} (whole world).
#' @param latitude Latitude limits. Default is \code{c(-90, 90)} (whole world).
#' @param title Title of the plot. Default is no title.
#' @param legendTitle Title of the legend. Default is the name of the filling variable.
#' @param annote Do you want to plot country labels (ISO 3166-1 alpha-2 code) on the map? Default is set to \code{FALSE}.
#' @param div Parameter for modifying the elements dimensions in the map. Usually, it does not need to be modified. Default value is 1.
#' @param palette_option Character string indicating the palette to be used. Available options range between "A" and "H".
#' @param save Save the plot in a jpg file?
#' @param filename Only if is save set to \code{TRUE}. Name of the file.
#' @param path Only if save is set to \code{TRUE}. Path of the directory where the file is to be saved.
#' @param width Only if save is set to \code{TRUE}. Width of the file.
#' @param height Only if save is set to \code{TRUE}. Height of the file.
#' @param units Only if save is set to \code{TRUE}. Units for width and height. Can be 'cm', 'mm', 'in', or 'px'.
#' @param scale Only if save is set to \code{TRUE}. Scaling factor for adjusting image dimensions.
#'
#' @return a map
#' @export
#' @importFrom rnaturalearth ne_countries
#' @importFrom countrycode countrycode
#' @importFrom dplyr "%>%" left_join select filter mutate relocate
#' @importFrom ggplot2 ggplot geom_sf theme labs scale_fill_viridis_c coord_sf xlab ylab ggtitle
#' aes unit element_text element_blank element_rect geom_text ggsave
#' @importFrom sf st_centroid st_coordinates
#'
#' @examples
#' data(testdata1b)
#' worldplot(data = testdata1b,
#' div = 1,
#' ColName = "VNum",
#' CountryName = "Cshort",
#' CountryNameType = "isoa2",
#' rangeVal = c(0,50),
#' annote = FALSE)
#'
worldplot <- function(data,
ColName, CountryName, CountryNameType = "isoa2", rangeVal,
longitude = c(-180, 180) ,latitude = c(-90, 90),
title = "", legendTitle = as.character(ColName),
annote = FALSE, div = 1, palette_option = "D",
save = FALSE, filename = "worldplot.jpg", path = tempdir(),
width = 20, height = 10, units = "cm", scale = 1) {

world <- ne_countries(scale = 50, continent = NULL, returnclass = "sf")

map_df0<- world %>%
select(name, iso_a2_eh, iso_a3_eh, geometry) %>%
mutate(iso_a2 = ifelse(name %in% c("Indian Ocean Ter." , "Ashmore and Cartier Is."), -99, iso_a2_eh),
iso_a3 = ifelse(name %in% c("Indian Ocean Ter." , "Ashmore and Cartier Is."), -99, iso_a3_eh)) %>%
select(name, iso_a2, iso_a3, geometry)

simdata <- c()

simdata$MapFiller <- data[, which(colnames(data) == ColName)]

if (CountryNameType == "isoa2") {
simdata$iso_a2 <- data[, which(colnames(data) == CountryName)]
} else if (CountryNameType == "name") {
simdata$iso_a2 <- countrycode(sourcevar = data[, which(colnames(data) == CountryName)],
origin = "country.name", destination = "iso2c")
} else if (CountryNameType == "isoa3") {
simdata$iso_a2 <- countrycode(sourcevar = data[, which(colnames(data) == CountryName)],
origin = "iso3c", destination = "iso2c")
} else {
simdata$iso_a2 <- NULL
}

simdata <- as.data.frame(simdata)

map_df <- left_join(map_df0, simdata, by = "iso_a2")

wplot <- ggplot(data= map_df) +
geom_sf(color= 'black', aes(fill= MapFiller)) +
theme(legend.key.size = unit(1/div, 'lines'),
legend.text = element_text(size= 8/div),
legend.title = element_text(size= 8/div),
plot.title = element_text(size=8/div),
panel.grid = element_blank(),
panel.background = element_rect(fill = 'grey95'))+
labs(fill= legendTitle)+
scale_fill_viridis_c(option= palette_option, na.value = 'grey80',direction=1,begin=0.3, limits= rangeVal)+
coord_sf(xlim= longitude, ylim= latitude, expand= FALSE, label_axes = 'SW') +
xlab('') + ylab('')+
ggtitle(title)

if (annote == TRUE) {

world_points <- geometries_data(exclude.iso.na = T,
countries.list = simdata$iso_a2[!is.na(simdata$MapFiller)])

wplot <- wplot +
geom_text(data= world_points, aes(x=X, y=Y,label= iso_a2), size= 2/div, color= 'black', fontface= 'bold')
}

print(wplot)

if (save == TRUE) {
ggplot2::ggsave(filename = filename,
path = path,
width = width,
height = height,
units = units,
dpi = "retina",
scale = scale)
}

}

0 comments on commit 106f5cb

Please sign in to comment.