Related
I plotted a few European countries on a map, but there are some outliers which I don't need. I tried to remove them from my spatial df using different ways suggested in similar questions but they didn't work for this case. Could you please give me your ideas on removing them? I appreciate it. The shape file is available here
EDIT: I need to remove these areas not only from the map, but also from the spatial data frame.
library(rgdal)
library(raster)
myCountries <- c("Austria", "Belgium", "Czech Republic", "Denmark", "Estonia", "Finland",
"France", "Germany", "Latvia", "Hungary", "Iceland", "Ireland", "Italy",
"Netherlands", "Norway", "Portugal", "Poland", "Spain", "Sweden", "Switzerland",
"Turkey", "United Kingdom")
countries <- readOGR('ne_110m_admin_0_countries.shp')
eurcountries <- countries[countries$NAME_EN %in% myCountries ,]
eurcountries2<-spTransform(eurcountries, CRS("+proj=longlat +datum=NAD83"))
plot(eurcountries2)
Here is how you can do that with terra (the replacement for raster):
myCountries <- c("Austria", "Belgium", "Czech Republic", "Denmark", "Estonia", "Finland",
"France", "Germany", "Latvia", "Hungary", "Iceland", "Ireland", "Italy",
"Netherlands", "Norway", "Portugal", "Poland", "Spain", "Sweden", "Switzerland",
"Turkey", "United Kingdom")
library(terra)
countries <- vect('ne_110m_admin_0_countries.shp')
eur <- countries[countries$NAME_EN %in% myCountries ,]
e <- ext(c(-28, 48, 35, 76)))
x <- crop(eur, e)
plot(x, "NAME_EN")
You can interactively find the extent you need for cropping by doing
plot(eur)
e <- draw()
# now click on the map twice
Or subset interactively, like this:
d <- disagg(eur)
plot(d)
s <- sel(d) # now draw a bounding box on the plot
a <- aggregate(s, "NAME_EN")
plot(a, "NAME_EN")
And you can coerce the SpatVector objects to sp or sf types like this:
sf <- sf::st_as_sf(x)
library(raster)
sp <- as(x, "Spatial")
Or vice versa with:
y <- vect(sf)
Instead of using the SP package, I find the SF package is better as it plays well with ggplot2. Then limiting the canvas is straightforward and adds the ability to colour the countries.
library(rgdal)
library(ggplot2)
myCountries <- c("Austria", "Belgium", "Czech Republic", "Denmark", "Estonia", "Finland",
"France", "Germany", "Latvia", "Hungary", "Iceland", "Ireland", "Italy",
"Netherlands", "Norway", "Portugal", "Poland", "Spain", "Sweden", "Switzerland",
"Turkey", "United Kingdom")
countries <- readOGR("C:/R/projects/ne_110m_admin_0_countries/ne_110m_admin_0_countries.shp")
eurcountries <- countries[countries$NAME_EN %in% myCountries, ]
eurcountries3 <- sf::st_as_sf(eurcountries)
ggplot(eurcountries3) +
geom_sf(aes(fill = ADMIN)) +
lims(x = c(50,-40), y = c(30, 74)) +
guides(fill = "none") +
theme_void()
I would like to calculate the betweenness of a network like e.g.
>dput(relations)
structure(list(From = c("France", "Italy", "Malta", "Spain",
"Germany", "Austria", "Luxembourg", "Luxembourg", "Luxembourg",
"Greece", "Slovakia", "Belgium", "Luxembourg", "Estonia", "Germany",
"Netherlands", "Netherlands", "Cyprus", "Greece", "Lithuania",
"Estonia", "Greece", "Lithuania", "Slovakia", "Netherlands",
"Luxembourg", "Estonia", "Malta", "Belgium", "Cyprus", "Austria",
"Estonia", "Austria", "Germany", "Austria", "Netherlands", "Portugal",
"Portugal", "Estonia", "Italy", "Spain", "Finland", "Belgium",
"Spain", "Estonia", "Latvia", "Luxembourg", "Luxembourg", "Netherlands",
"Italy", "Lithuania", "Cyprus", "Ireland", "Luxembourg", "Ireland",
"Spain", "Belgium", "Latvia", "Netherlands", "Italy", "Slovenia",
"Netherlands", "Greece", "Spain", "Austria", "Finland", "Malta",
"Spain", "Austria", "Lithuania", "France", "Portugal", "Cyprus",
"Finland", "Spain", "Spain", "Belgium", "Germany", "Germany",
"Greece", "Netherlands", "Luxembourg", "Estonia", "Ireland",
"Italy", "Portugal", "Greece", "Estonia", "Belgium", "Germany",
"Slovenia", "Slovakia", "Lithuania", "Slovenia", "Portugal",
"Portugal", "Slovakia", "France", "Portugal", "Netherlands"),
To = c("Slovakia", "Germany", "Portugal", "Austria", "Latvia",
"Cyprus", "Portugal", "Greece", "Italy", "Slovenia", "Ireland",
"Malta", "Ireland", "Germany", "Cyprus", "Portugal", "Slovenia",
"Italy", "Luxembourg", "France", "Slovakia", "Netherlands",
"Greece", "France", "Ireland", "Netherlands", "Cyprus", "Germany",
"Portugal", "Austria", "Luxembourg", "Austria", "Spain",
"Netherlands", "Belgium", "Cyprus", "Cyprus", "Luxembourg",
"Finland", "Belgium", "Lithuania", "Austria", "Spain", "Slovenia",
"Luxembourg", "Finland", "Slovenia", "Germany", "Austria",
"Slovenia", "Slovenia", "Portugal", "Finland", "Lithuania",
"Latvia", "France", "Netherlands", "Cyprus", "Spain", "Malta",
"France", "Finland", "Belgium", "Latvia", "Slovenia", "Slovenia",
"Cyprus", "Slovakia", "Slovakia", "Latvia", "Austria", "Ireland",
"Luxembourg", "Belgium", "Italy", "Estonia", "Greece", "Slovakia",
"Belgium", "Italy", "Latvia", "Malta", "Greece", "France",
"Greece", "Netherlands", "France", "Slovenia", "Latvia",
"Finland", "Austria", "Slovenia", "Belgium", "Cyprus", "Greece",
"Slovenia", "Cyprus", "Finland", "Malta", "Germany"), `1995` = c(274959716,
42345007970, 52361033, 784003104, 363593773, 26339142, 70240364.9716237,
66160839.8751728, 570402199.020507, 48087089, 7213761, 51841984.7433951,
17654753.3588222, 172147969, 510653861, 1490942352, 236034017,
21599848, 8237002.21796104, 78753113, 1785782, 317673116,
1997651, 191502494, 1085060805, 397894640.417207, 3278854,
311533699, 1120849630.02838, 16319437, 83172852.11371, 7429991,
1130916439, 37969299820, 893625108.88629, 85666548, 20013832,
34463454.0875298, 391203046, 6317180664.31942, 19705368,
392268361, 4455785372.87273, 222358956, 411505.596673383,
52421883, 15725678.4821813, 1822047157.86885, 2459346931,
1876562166, 808113, 1124090, 266044676, 2248412.10389544,
7378082, 18441136707, 20732500726.5828, 2916590, 4946996508,
1165517054, 737293168, 1253219429, 234340068.782039, 10770159,
966929910, 40043392, 1231347, 76204779, 564056237, 152914849,
3269662990, 108614454, 2313081.15506869, 1189550231.54615,
8097112911, 15174304, 877483944.124827, 2135381409, 28188202490.5877,
1524668022, 84865417, 1965397.25660491, 1384674, 3699841535,
4383865473, 1233064242, 664865392, 183091, 42970685.557638,
4742184966, 550250520, 102947427, 49519226.4265594, 5273729,
105863344, 5888705, 10907803, 1170153350, 20869834, 43104477514
)), row.names = c(NA, -100L), class = c("tbl_df", "tbl",
"data.frame"))
> dput(actors)
c("Slovakia", "Germany", "Portugal", "Austria", "Latvia", "Cyprus",
"Greece", "Italy", "Slovenia", "Ireland", "Malta", "Luxembourg",
"France", "Netherlands", "Spain", "Belgium", "Finland", "Lithuania",
"Estonia")
>g <- graph_from_data_frame(relations, directed=TRUE, vertices=actors)
I would like to use the command provided by the package: betweenness(g). But I don't know how the distance between two nodes in igraph is calculated. Is it the sum of the weights? Or is it the sum of the reciprocals of the weights?
In all igraph functions, the "length" of a path is considered to be the sum of the weights of edges making up that path.
This question already has an answer here:
Conditional labeling in ggplot2 using geom_text and subsetting
(1 answer)
Closed 1 year ago.
I have a dot plot with data from dataframe df_roles$result and would like to assign labels only to certain dots specified by their specific name in df_roles$name.
My expected output is to have e.g. China labelled.
I have tried both these arguments but they won't work:
ifelse(df_roles$name = "China",
text(df_roles[[3]], df_roles[[2]], labels = df_roles[[1]], pos = 2, cex=0.3), " ")
geom_text(data = filter(df_roles$result, df_roles$name=="China"),aes(label=df_roles$name))
Reproducible example:
df_roles <- structure(list(name = structure(1:6, .Label = c("Afghanistan",
"Albania", "Algeria", "Angola", "Argentina", "Armenia", "Australia",
"Austria", "Azerbaijan", "Bahrain", "Bangladesh", "Barbados",
"Belarus", "Belgium", "Belize", "Benin", "Bolivia", "Bosnia and Herzegovina",
"Botswana", "Brazil", "Bulgaria", "Burkina Faso", "Burundi",
"Cameroon", "Canada", "Chile", "China", "Colombia", "Congo",
"Costa Rica", "Croatia", "Cuba", "Cyprus", "Democratic Republic of the Congo",
"Denmark", "Dominican Republic", "Ecuador", "Egypt", "El Salvador",
"Eritrea", "Estonia", "Ethiopia", "Finland", "France", "Gabon",
"Gambia", "Georgia", "Germany", "Ghana", "Greece", "Grenada",
"Guatemala", "Guinea", "Guyana", "Haiti", "Honduras", "Hungary",
"India", "Indonesia", "Iran ", "Iraq", "Ireland", "Israel", "Italy",
"Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kuwait",
"Kyrgyzstan", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libya",
"Lithuania", "Luxembourg", "Malawi", "Malaysia", "Mali", "Malta",
"Mauritania", "Mexico", "Mongolia", "Montenegro", "Morocco",
"Mozambique", "Namibia", "Nepal", "Netherlands", "New Zealand",
"Nicaragua", "Nigeria", "Norway", "Oman", "Pakistan", "Palestine",
"Panama", "Paraguay", "Peru", "Philippines", "Poland", "Portugal",
"Qatar", "Republic of Korea", "Republic of Moldova", "Romania",
"Russia", "Rwanda", "Saint Lucia", "Saint Vincent and the Grenadines",
"Saudi Arabia", "Senegal", "Serbia", "Singapore", "Slovakia",
"Slovenia", "Somalia", "South Africa", "Spain", "Sri Lanka",
"Sudan", "Suriname", "Sweden", "Switzerland", "Syria", "Taiwan",
"Tajikistan", "Thailand", "Togo", "Trinidad and Tobago", "Tunisia",
"Turkey", "Turkmenistan", "Uganda", "UK", "Ukraine", "United Arab Emirates",
"United Republic of Tanzania", "United States of America", "Uruguay",
"Uzbekistan", "Venezuela", "Viet Nam", "Yemen", "Zambia", "Zimbabwe"
), class = "factor"), result = c(0.0900102874778869, -0.0934265332577318,
-0.177974826946747, -0.590024694573266, 1.20884852383168, -0.183452483887309
)), row.names = c(NA, 6L), class = "data.frame")
Since the data shared does not have 'China' in it I am using 'Afghanistan' as an example. Also using row number as x-axis.
library(dplyr)
library(ggplot2)
df_roles %>%
mutate(name = as.character(name),
label = replace(name, name != 'Afghanistan', ''),
row = row_number()) %>%
ggplot(aes(row, result, label = label)) +
geom_point() + geom_text(vjust = -0.5)
I am trying to plot countries in R Maps. However, when I use FILL=TRUE all my countries boundaries are drawn in black. I want them in Gray. This is my code:
library(maps) # Provides functions that let us plot the maps
library(mapdata) # Contains the hi-resolution points that mark out the Cnt
countries=c("Argentina","Armenia","Australia","Bahrain","Belgium","Botswana","Bulgaria","Canada", "Chile","Tawain", "Croatia","Cyprus", "Czech Republic", "Denmark","Egypt","UK:Great Britain","Finland", "France", "Georgia", "Germany", "China:Hong Kong", "Hungary", "Indonesia", "Iran", "Ireland", "Israel", "Italy", "Japan", "Jordan", "Kazakhstan", "Korea", "Kuwait", "Lebanon", "Lithuania", "Malaysia", "Malta", "Morocco", "Netherlands", "New Zealand", "UK:Northern Ireland", "Norway", "Oman", "Palestine", "Poland", "Portugal", "Qatar", "Russia", "Saudi Arabia", "Serbia", "Singapore", "Slovak Republic", "Slovenia", "South Africa", "Spain", "Sweden", "Thailand", "Turkey", "United Arab Emirates", "USA")
map('world', resolution=1, col="darkgray")
map('world', countries, resolution=1, fill = T, col = "royalblue", add = T)
map('world', resolution=1,col="darkgray", add=TRUE)
Any ideas will be appreciated.
Thanks
Jº
To change the colour of the boundaries, add the argument border='darkgrey'. This argument is not listed explicitely in the man page for 'map' because it is in fact an argument for the call to 'polygon'. It is mentioned in the '...' segment of the man page, though.
map('world', countries, resolution=1, fill = T,
col = "royalblue", border="darkgrey", add = T)
I would like to generate a set of maps in R with all of them having the same background (a focus on Europe) BUT each of them having one EU country highlighted in another color. And I can't seem to figure out how to write the for loop to get that...
Here is my code:
require(rgdal)
setwd(...) #where I have my GIS shapefile
world <- readOGR(dsn = ".", layer = "TM_WORLD_BORDERS-0.2")
#Subset European countries
#List of "european" countries + shapefile
europe <- c("Russia", "Isle of Man", "Channel Islands", "Faroe Islands",
"France", "Denmark", "Iceland", "Germany", "Romania", "Poland", "Portugal",
"United Kingdom", "Spain", "Sweden", "Lithuania", "Ireland", "Italy",
"Netherlands", "Norway", "Ukraine", "Latvia", "Estonia", "Finland",
"Bulgaria", "Belgium", "Montenegro", "Serbia and Montenegro", "Slovenia",
"Albania", "Greece", "Croatia", "Malta")
europe <- subset(world, NAME %in% europe)
#List of countries in the EU + shapefile
EU <- c("Isle of Man", "Channel Islands", "Faroe Islands", "France",
"Denmark", "Germany", "Romania", "Poland", "Portugal", "Spain", "Sweden",
"Lithuania", "Ireland", "Italy", "Netherlands", "Ukraine", "Latvia", "Estonia",
"Finland", "Bulgaria", "Belgium", "Montenegro", "Serbia and Montenegro",
"Slovenia", "Albania", "Greece", "Croatia", "Malta")
EU <- subset(europe, NAME %in% EU)
#Generate one map per highlighted country
eucountries <- unique(europe$NAME)
for(i:length(eucountries))
{
print(i)
png(paste(i,".png",sep=""), 200, 200)
map("world", ylim=c(35,70), xlim=c(-20,45), col="#BFBFBF", fill=TRUE)
plot(eucountries, add=TRUE, col="#769EB2", namesonly=TRUE)
dev.off()
}
I want to produce one png per country. Each png will have one specific country highlighted with a different color. The full map will be plotted each time.
Thanks to vpipkt's comment that indicated that map()$names does provide a list of names of the things (polygons I suspect) that are plotted I could come up with a much more elgant solution:
building an index of for those polygons that are named like countries
using that information to build a color vector to color the countries
Note: the borders provided by the maps packae seem a litle outdated, e.g. Yugoslavia
# library
library(maps)
# options
old <- par()$mar
par("mar"=c(0,0,0,0))
YLIM <- c(35,70)
XLIM <- c(-20,45)
# plotting
for(country in c("Germany", "Ireland", "Spain", "Greece", "Denmark", "Yugoslavia") )
{
polygon_names <- map("world", ylim=YLIM, xlim=XLIM)$names
index <- grep(country, polygon_names)
colvec <- rep("white", length(polygon_names))
colvec[index] <- "red"
png(paste0(country,".png"))
map("world", ylim=YLIM, xlim=XLIM, col=colvec, fill=TRUE)
dev.off()
}
# resetting options
par("mar"=old)
Inside your loop, try
plot(eucountries[i], add=TRUE, col="#769EB2", namesonly=TRUE)
in place of your current plot call. Note the subset of eucountries.