I have been trying to convert the latitude and longitude into multipolygon object so that that I can use tmap library to plot it, but I am not able to that. Converting useing st_as_sf is not wroking, can some help me? I am attaching sample data set.
coor<-structure(list(Type = c("Registry", "Registry", "Registry", "Registry", "Platform", "Registry"),`Location of coordinating center` = c("USA","USA", "USA", "USA", "United Kingdom", "United Kingdom"),`3ISO code` = c("USA", "USA", "USA", "USA", "GBR", "GBR"), `WHO region code` = c("AMR","AMR", "AMR", "AMR", "EUR", "EUR"), city = c("Philadelphia","Chicago", "Washington", "Alexandria", "London", "Manchester"), lat = c(32.7761, 41.8373, 38.9047, 38.8185, 51.50853, 53.4794), lng = c(-89.1221, -87.6862, -77.0163, -77.0861, -0.12574, -2.2453)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
Two comments to this:
you are unlikely to convert your data to multiopolygon object, since you have only a single coordinate pair for each city / that spells points to me, not polygons
you were on a right track with sf::st_as_sf()
What you need to do in your st_as_sf call to make it work properly is
specify columns in your data frame that have coordinate information and
give a meaning to the figures (are they degrees? meters? or has an American been feeling patriotic and encoded the data in feet?) you use a coordinate reference system (crs) for that.
For long lat data EPSG:4326 is usually a good default.
library(sf)
library(tmap)
sf_coords <- coor %>%
st_as_sf(coords = c("lng", "lat"), crs = 4326)
tmap_mode("view")
tm_shape(sf_coords) +
tm_bubbles(size = 5, col = "red", id = "city") +
tm_basemap(leaflet::providers$Stamen.Watercolor)
Related
I am following the code from this website and trying to plot some data on an interactive map using ploty. Difference is I only have data for 6 euro pean countries. Currently the map is plotting for the entire world. How do I adjust the code to zoom in on either all of Europe or just the 6 European countries in the data. See sample code below. The country codes are from this link.
# Libraries
library(tidyverse)
library(plotly)
# Data
countries <- c("Germany", "Belgium", "Framce", "Italy", "Spain", "Poland")
codes <- c("DEU", "BEL", "FRA", "ITA", "ESP", "POL")
values <- c(100, 200, 300, 400, 500, 600)
df <- tibble(countries, codes, values)
# Maps
plot_geo(locations = df$codes) %>%
add_trace(
z = ~values,
locations = ~codes,
color = ~values
)
I have written the following code:
library(ozmaps)
oz(states = TRUE, coast = TRUE,
ylim = NULL, add = FALSE, ar = 1, eps = 0.25)
That generates:
I wonder how to add the names of the states on the map? If the whole process can be done by "map" package, it is fine as well.
Thanks.
I'm not familiar with package ozmaps. I could not find data for the state names and latitude and longitude values for state centroids within the package.
A quick internet search produced some representative state data from https://www.distancelatlong.com/distancecalculator/country/australia/
You can adjust this data or find a better source, so this should be a start.
library(tibble)
library(ggplot2)
library(ozmaps)
ggplot(ozmap_states)+
geom_sf()+
geom_text(data = oz_states, aes(long, lat, label = state))+
theme_void()
data
oz_states <- tribble(
~state, ~lat, ~long,
"Australian Capital Territory", -35.3546004, 149.2113468,
"New South Wales", -33.42004148, 151.3000048,
"Northern Territory", -13.81617348, 131.816698,
"Queensland", -26.67998777, 153.0500272,
"South Australia", -34.28293455, 140.6000378,
"Tasmania", -40.83292234, 145.1166613,
"Victoria", -37.73119953, 142.0234135,
"Western Australia", -33.58287392, 120.0333345
)
Created on 2021-03-29 by the reprex package (v1.0.0)
I am creating a heatmap using the map of Europe in rworldmap package in R (since I don't know how to do this with ggmap or ggplot2).
I need to plot the country names of the countries that are present in my dataframe only, not all european countries. How can I do this?
My code:
library(RColorBrewer)
#getting colours
colourPalette <- brewer.pal(5,'RdPu')
library(rworldmap)
europe <- data.frame(
"country" = c("Greece",
"France",
"Spain",
"Italy",
"UK",
"Finland","Norway","Sweden",
"Germany",
"Romania"),
"x" = c(2.5, 3, 2.2, 1.8,2.32, 1.99, 2.01, 2.34, 1.88, 2.45))
matched <- joinCountryData2Map(europe, joinCode="NAME", nameJoinColumn="country")
mapParams <- mapCountryData(matched,
nameColumnToPlot="x",
mapTitle="my Titley",
addLegend=FALSE,
mapRegion="Europe"
,colourPalette=colourPalette,
oceanCol="#404040", missingCountryCol="#A0A0A0")
#adding legend
do.call(addMapLegend
,c(mapParams
,legendLabels="all"
,legendWidth=0.5
,legendIntervals="data"
,legendMar = 2))
labelCountries()
Using labelCountries() prints all country names and it's not readable.
Thanks
With a little bit help of this previously answer:
# get the coordinates for each country
country_coord<-data.frame(coordinates(matched),stringsAsFactors=F)
# label the countries
country = c("Greece",
"France",
"Spain",
"Italy",
"UK",
"Finland","Norway","Sweden",
"Germany",
"Romania")
#filter your wanted countrys
country_coord = country_coord[country,]
#insert your labels in plot
text(x=country_coord$X1,y=country_coord$X2,labels=row.names(country_coord))
you can add the country labels with text but you must extract the coordinates before from your matched coordinates.
Output:
You might play a bit with col = "color" in text, since some country can barely been read. Or maybe change the color scale in your map
I am generating a world map and hoping to highlight some specific countries. This is my attempt:
require(mapdata)
cc <- map('world', names = TRUE, plot = FALSE)
take <- unlist(sapply(c("uk", "usa", "switzerland","new zealand",
"israel","denmark","sweden","italy",'canada'),
grep, tolower(cc), value = TRUE))
map()
map('world', regions=take, fill=TRUE, col='red', add = TRUE)
nums <- c(12,11,1,2,1,1,1,1,1)
I have two questions.
1) Is it possible to include points on this map to correspond to the variable 'nums'. Each of the numbers in 'nums' refers to the number of measurements taken from a specific country, 12 is for the UK and so on... Is it possible to include a point on the map with the radius of that point corresponding to the number in 'nums'. I know this is possible with ggplot2 but am not sure how to do it with mapdata?
2) How can I make this map look nicer i.e. there seem to be a lot of empty countries here, is there an elegant way of only including the countries that are colored?
Hm something like this?
require(mapdata)
cc <- map('world', names = TRUE, plot = FALSE)
take <- unlist(sapply(countries <- c("uk", "usa", "switzerland","new zealand",
"israel","denmark","sweden","italy",'canada'),
grep, tolower(cc), value = TRUE))
nums <- c(12,11,1,2,1,1,1,1,1)
# gc <- ggmap::geocode(countries) # a googlemaps query gives this:
gc <- structure(list(lon =
c(-3.435973, -95.712891, 8.227512, 174.885971,
34.851612, 9.501785, 18.643501, 12.56738, -106.346771), lat = c(55.378051,
37.09024, 46.818188, -40.900557, 31.046051, 56.26392, 60.128161,
41.87194, 56.130366)),
.Names = c("lon", "lat"),
class = "data.frame",
row.names = c(NA, -9L))
map(xlim=c(-150, 40), ylim=c(25, 75)) # plot only a part of the world map
map('world', regions=take, fill=TRUE, col='red', add = TRUE, )
with(gc, points(lon, lat, cex=nums, pch=19, col=rgb(0, 1, 0, .8))) # add circles
I would like to use R to generate a very basic world map with a specific set of countries filled with a red colour to indicate that they are malaria endemic countries.
I have a list of these countries in a data frame but am struggling to overlay them on a world map.
I have tried using the wrld_simpl object and also the joinCountryData2Map method in the rworldmap package.
I would comment on this answer to prevent addition of a possibly redundant question but I do not have enough reputation at the moment, apologies for this.
https://stackoverflow.com/a/9102797/1470099
I am having difficulty understanding the arguments given to the plot() command - I wondered if there was just an easy way to tell R to plot all of the country NAMEs in my list on the wrld_simpl map instead of using grepl() etc. etc.
plot(wrld_simpl,
col = c(gray(.80), "red")[grepl("^U", wrld_simpl#data$NAME) + 1])
Using the rworldmap package, you could use the following:
library(rworldmap)
theCountries <- c("DEU", "COD", "BFA")
# These are the ISO3 names of the countries you'd like to plot in red
malDF <- data.frame(country = c("DEU", "COD", "BFA"),
malaria = c(1, 1, 1))
# malDF is a data.frame with the ISO3 country names plus a variable to
# merge to the map data
malMap <- joinCountryData2Map(malDF, joinCode = "ISO3",
nameJoinColumn = "country")
# This will join your malDF data.frame to the country map data
mapCountryData(malMap, nameColumnToPlot="malaria", catMethod = "categorical",
missingCountryCol = gray(.8))
# And this will plot it, with the trick that the color palette's first
# color is red
EDIT: Add other colors and include picture
## Create multiple color codes, with Burkina Faso in its own group
malDF <- data.frame(country = c("DEU", "COD", "BFA"),
malaria = c(1, 1, 2))
## Re-merge
malMap <- joinCountryData2Map(malDF, joinCode = "ISO3",
nameJoinColumn = "country")
## Specify the colourPalette argument
mapCountryData(malMap, nameColumnToPlot="malaria", catMethod = "categorical",
missingCountryCol = gray(.8), colourPalette = c("red", "blue"))
Try using googleVis package and use gvisGeoMap Functions
e.g.
G1 <- gvisGeoMap(Exports,locationvar='Country',numvar='Profit',options=list(dataMode='regions'))
plot(G1)
library(maptools)
data(wrld_simpl)
myCountries = wrld_simpl#data$NAME %in% c("Australia", "United Kingdom", "Germany", "United States", "Sweden", "Netherlands", "New Zealand")
plot(wrld_simpl, col = c(gray(.80), "red")[myCountries+1])