I want to create a geographical map of countries in Europe where I can highlight which of the countries are using a particular system.
I have tried using the library(maps) but here I cant find an option to plot the whole of Europa (and also several relevant countries are not included in the package). Does anybody have suggestion for me on how to do is?
What about this?
library(rworldmap)
world <- getMap()
europe <- world[world$ADMIN %in% c("Austria","Belgium","Bulgaria","Croatia","Cyprus","Czech Republic","Denmark","Estonia","Finland","France","Germany","Greece","Hungary","Ireland","Italy","Latvia","Lithuania","Luxembourg","Malta","Netherlands","Poland","Portugal","Romania","Slovakia","Slovenia","Spain","Sweden","United Kingdom"), ]
cols <- setNames(rep("yellow", length(europe$ADMIN)), europe$ADMIN)
cols["France"] <- "blue"; cols["Poland"] <- "red"
plot(europe, col = cols)
Related
I try to sew/merge/bind two LINESTRINGs together (spanish coast + french coast) to have the full map of the whole coast. I found all the shpaefiles here:
https://www.marineregions.org/gazetteer.php?p=details&id=3417
https://www.marineregions.org/gazetteer.php?p=details&id=19888
Import and plot the data
frenchCoast_CoteBanyuls <- st_read("coasts_subnational/coasts_subnational.shp") %>%
st_geometry() #
plot(frenchCoast_CoteBanyuls)
spainCoast_CoteBanyuls <- st_read("coasts_subnational SPAIN/coasts_subnational.shp")%>%
st_geometry() #
spainCoast_CoteBanyuls <- st_cast(x = spainCoast_CoteBanyuls, to = "LINESTRING")
plot(spainCoast_CoteBanyuls, add = T)
This map and features seem ok.
According to all the posts I found, I tried to merge the two shapefiles together:
CoteBanyuls_c <- c(frenchCoast_CoteBanyuls, spainCoast_CoteBanyuls)
# CoteBanyuls_rbind <- rbind(frenchCoast_CoteBanyuls, spainCoast_CoteBanyuls)
# CoteBanyuls <- st_union(CoteBanyuls)
# CoteBanyuls <- union(CoteBanyuls)
# CoteBanyuls <- bind(CoteBanyuls)
plot(CoteBanyuls)
the c() function gives a shapefile that does not have the good dimensions (see below) and none of these solutions does plot the good shape of the two coast merged (seems to plot only the spanish coast).
What did I do wrong ? I do not understand why these solutions are not working...
Do you have some ideas ?
Thanks in advance !
Charlotte
I think the issue comes from extracting only the geometry attributes before you combine them. Try this:
frenchCoast_CoteBanyuls <- st_read("coasts_subnational/coasts_subnational.shp")
spainCoast_CoteBanyuls <- st_read("coasts_subnational SPAIN/coasts_subnational.shp")
combined_coast <- rbind(spainCoast_CoteBanyuls, frenchCoast_CoteBanyuls)
mapview::mapview(combined_coast)
I have a map of Russia with regional subdivision
library(raster)
data <- getData('GADM', country='RUS', level=1)
http://www.gks.ru/bgd/regl/B16_14p/IssWWW.exe/Stg/d01/08-01.doc
The link is to a Word.doc with data (table) on crime rates for Russian regions. I can extract this data and use it in R. I want to take 2015 year and colorize regions on the map depending on the crime rate (also add a legend). How can I do this? The problem is that names of regions are sometimes different in the shape file (NL_NAME_1) and in the data from www.gks.ru.
I also have this code for graph that I need, except that here we have meaningless colors:
library(sp)
library(RColorBrewer)
data$region <- as.factor(iconv(as.character(data$NAME_1)))
spplot(data, "region", xlim=c(15,190), ylim=c(40,83),
col.regions=colorRampPalette(brewer.pal(12, "Set3"))(85), col = "white")
If I understand your question properly, you just need to add your data to the spatial object for making colors meaningful.
Note, please, that the data is a reserved word in R. So, it's better to modify a little your variable name:
geo_data <- getData('GADM', country = 'RUS', level = 1)
Let's emulate some data to demonstrate a visualization strategy:
set.seed(23)
geo_data#data["data_to_plot"] <- sample(1:100, length(geo_data#data$NAME_1))
Using a default GADM projection would cut the most eastern part of the country. A simple transformation helps to fit the whole area to a plot:
# fit Russian area inside the plot
geo_data_trsf <- spTransform(geo_data, CRS("+proj=longlat +lon_wrap=180"))
Draw the map selecting data_to_plot instead of region:
max_data_val <- max(geo_data_trsf#data$data_to_plot)
spplot(geo_data_trsf, zcol = "data_to_plot",
col.regions = colorRampPalette(brewer.pal(12, "Set3"))(max_data_val),
col = "white")
The plot limits are adjusted automatically for the transformed spatial data geo_data_trsf, making possible to omit xlim and ylim.
As for the problem with the names, I can't provide any ready-to-use solution. Obviously, the regions' names of NL_NAME_1 need some additional treatment to use them as labels. I think, it would be better to use NAME_1 as an identifier in your code to ensure that it'll be no troubles with encoding. The NL_NAME_1 column is perfectly suitable to set the correspondence between your Word-data and the data inside the spatial object geo_data.
I need to plot a map of Denmark divided into regions (there are 5: Region Nordjylland, Midtjylland, Sydjylland, Sjælland and Hovedstaden) and then color the regions such that the different regions stand out clearly. I have to use leaflet, since it has other features, that I will use later. I found a map on naturalearthdata.com, that I think I can use, but I can't figure out how to color (or even indicate) the regions. The code I tried is below
library(rgdal)
library(leaflet)
download.file(file.path('http://www.naturalearthdata.com/http/',
'www.naturalearthdata.com/download/50m/cultural',
'ne_50m_admin_1_states_provinces_lakes.zip'),
f <- tempfile())
unzip(f, exdir=tempdir())
world <- readOGR(tempdir(), 'ne_50m_admin_1_states_provinces_lakes', encoding='UTF-8')
DK <- subset(world, name=="Denmark")
leaflet() %>% addTiles() %>% addTopoJSON(DK, weight = 1, color = "#444444", fill = TRUE)
How does one use the naturalearthdata.com data to plot regions/states/provinces of different countries? I have seen a very nice example at
http://www.56n.dk/kort/dk2050kort_age.html
but there is no sample code available.
I have also found a very nice example here: https://rpubs.com/walkerke/leaflet_choropleth - but I need a map of Denmark.
UPDATE: I have found a shapefile at http://www.kortforsyningen.dk which does the trick. So now my question is how do I combine my own data with a shapefile and plot it in leaflet? If I just put
DK <- readOGR(".../shape", layer="REGION")
leaflet(data=DK)
I get a blank screen...
Using some code I've gotten from StackOverflow, I would like to shade a map but also have the county name as well.
In a perfect world, I'd like to be able to show ONLY the top and bottom 5 county names and as an additional twist I'd like the high names in red and low names in black.
But I'm having enough trouble getting any names on or finding any examples of other options to add.
I'm new to R and not sure if this is the right way to do it.
Also, is there a visualization you would recommend to show 3 variables (income, birth rates, population) by county like this?
Thank you
library(noncensus)
library(zipcode)
library(choroplethr)
library(ggplot2)
library(sp)
library(maps)
#this guy is pretty but needs county names
data(df_pop_county)
county_choropleth(df_pop_county,
title="US 2012 County Population Estimates",
legend="Population",
buckets=1,
zoom=c("new york"))
#this guy has county names
getLabelPoint <- # Returns a county-named list of label points
function(county) {Polygon(county[c('long', 'lat')])#labpt}
df <- map_data('county', 'new york') # NY region county data
centroids <- by(df, df$subregion, getLabelPoint) # Returns list
centroids <- do.call("rbind.data.frame", centroids) # Convert to Data Frame
names(centroids) <- c('long', 'lat') # Appropriate Header
map('county', 'new york')
text(centroids$long, centroids$lat, rownames(centroids), offset=0, cex=0.4)
Say I am plotting countries on a world map using maptools, if I were to plot a country, is there a way of plotting the countries that border this country in a different colour? I am using the shapefile wrld_simpl that comes with maptools, so say I plot China:
plot(wrld_simpl[wrld_simpl$NAME=='China',], col='red', add=T)
is there a way I can get it to plot all the bordering countries to China. I want to be able to do this for lots of different countries so I'd ideally want a general solution, not one specific to just China.
How about gTouches or gIntersects in rgeos?
library(rgeos)
library(maptools)
wc <- subset(wrld_simpl, NAME == "China")
world <- subset(wrld_simpl, !NAME == "China")
Create a vector to store the test:
tst <- logical(nrow(world))
Do the test:
for (i in 1:nrow(world)) {
tst[i] <- gTouches(wc, world[i,])
}
See the result:
levels(world$NAME)[world$NAME[tst]]
[1] "India" "Russia"
plot(wc)
plot(world[tst, ], add = TRUE, col = "grey")
(No further correspondence on world affairs will be entered into, but gIntersects seems to give a better answer).
I strongly advise you to treat these built-in data sets with caution, you certainly need to get your hands on reliable data if you are to use such a tool for any non-trivial purpose. Geometry and data are tricky enough already. :)
for (i in 1:nrow(world)) {
tst[i] <- gIntersects(wc, world[i,])
}
length(levels(world$NAME)[world$NAME[tst]])
[1] 14
plot(world[tst, ], col = "firebrick")
plot(wc, add = TRUE, col = "grey")