Continent names and choropleth maps - r

I am working with ISO-3 names of the countries. Below you can see one example.I want to use this data for Choropleth map.
datar <- data.frame(GEO = c("GR","AZ","TR","GA","IR"),
Value = c(0.5560,0.323,0.2140,0.312,0.0225),
search_countries = factor(c("Greece","Azerbaijan","Turkey","Georgia","Iran")),
countries = c("Greece","Azerbaijan","Turkey","Georgia","Iran")
)
map <- map_data('world', region = datar$search_countries)
The code's last line allows me to draw a Choropleth map. Now I want to have an additional column with the continent names for each country next to the column subregion.
So can anybody help me how to solve this problem and to have a continent name for each country on the table?

If I understand correctly you want to add a column containing the relevant continents for each country in your dataframe?
Here we can get the country and continent names using the raster package then join them onto your dataframe :)
country.codes <- raster::ccodes()
map.continents <- left_join(map, country.codes, by = c('region' = 'NAME')) %>%
select(all_of(colnames(map)), continent)

Related

Making a graph from a dataset

I am trying to make a graph showing the average temp in Australia from 1950 to 2000. My dataset contains a "Country" table which contains Australia but also other countries as well. The dataset also includes years and average temp for every country. How would I go about excluding all the other data to make a graph just for Australia?
Example of the dataset
You just need to subset your data so that it only contains observations about Australia. I can't see the details of your dataset from your picture, but let's assume that your dataset is called d and the column of d detailing which country that observation is about is called country. Then you could do the following using base r:
d_aus <- d[d$country == "Australia", ]
Or using dplyr you could do:
library(dplyr)
d_aus <- d %>%
filter(country == "Australia")
Then d_aus would be the dataset containing only the observations about Australia (in which `d$country == "Australia"), which you could use to make your graph.
This should make the job. Alternatively, change the names of the columns to those of yours.
library("ggplot2")
library("dplyr")
data %>% filter(Country == "Australia" & Year %in% (1950:2000)) %>% ggplot(.,aes(x=Year,y=Temp)) + geom_point()

How can I select specific regions from a shapefile?

I have the present shapefile
heitaly<- readOGR("ProvCM01012017/ProvCM01012017_WGS84.shp")
FinalData<- merge(italy, HT, by.x="COD_PROV", by.y="Domain")
But I'm interesting not on all Italy, but also same provinces. How can I get them?
There are many ways to select a category into a shapefile. I don't know for what do you want. For example if it is to colour a specific region in a plot or to select a row from shapefile attribute table.
To plot:
plot(shape, col = shape$column_name == "element") # general example
plot(heitaly, col = heitaly$COD_PROV == "name of province") # your shapefile
To attribute table:
df <- shape %>% data.frame
This will give you the complete attribute table
row <- shape %>% data.frame %>% slice(1)
This will give you the first row with all columns. If you change the number 1 to another number, for example 3, will give you the information for row number 3
I hope have been useful

Pivot wider to new column based on condition R

I have a dataset associating a single application number with a series of different applicants from different countries. I have a column with each applicant's country of origin as the possible value. I want to condense everything down to 2 columns:
column 1 = count of applicants within USA
column 2 = count of applicants Outside USA
I guessed I would need to use an ifelse but I haven't managed to get anything to work so far, can someone please help?
Thanks!!
ps. If anyone knows how I could do this and produce a list of the countries outside USA like #sotos has done here Pivot wider returning 1 column? that would be even better, but that's just bonus :)
Like so?
df <- data.frame(app_num = c(1,1,1,2,2),
country = c(LETTERS[c(1:4,1)]))
library(tidyverse)
df %>%
count(A = if_else(country == "A", "USA", "Other")) %>%
pivot_wider(names_from = A, values_from = n)

Adding values from a dataframe to a different dataframe

I'm a noob in r programming.
I have 2010 census data in the link-
census data.
This is my dataframe-
dataframe.
What I'd like to do is add the population column 'P001001' from the census data for each state into the dataframe. I'm not able to figure out how to map the state abbreviations in the dataframe to the full names in the census data, and add the respective population to each row for that state in the data frame. The data is for all of the states. What should be the simplest way to do this?
Thanks in advance.
Use the inbuilt datasets for USA states: state.abb and state.name see State name to abbreviation in R
Here's a simple bit of code which will give you a tidyverse approach to the problem.
1) add the state abbreviation to the census table
2) left join the census with the df by state abbrevation
library(tibble)
library(dplyr)
census <-tibble(name = c("Colorado", "Alaska"),
poo1oo1 = c(100000, 200000))
census <-
census %>%
mutate(state_abb = state.abb[match(name, state.name)])
df <- tibble(date = c("2011-01-01", "2011-02-01"),
state = rep("CO", 2),
avg = c(123, 1234))
df <-
df %>%
left_join(census, by = c("state" = "state_abb"))

How to Assign Certain Data to Specific Areas in R Leaflet

I'm making a Leaflet map in R and I want to assign certain data to the specific areas. Right now I'm using a placeholder just to try and figure it out but I'm not having luck. I have certain data from an excel file that I want to assign to certain counties. How would I go about that?
library(maptools)
library(leaflet)
library(rjson)
library(magrittr)
library(sf)
library(xlsx)
## reads in the JSON data of all counties in USA
counties <- sf::read_sf("http://eric.clst.org/wupl/Stuff/gz_2010_us_050_00_500k.json")
## selects kansas and missouri county lines
kscounties<-counties[counties$STATE=="20",]
mocounties<-counties[counties$STATE=="29",]
## variable containing all kansas and missouricounty names
kscountynames<-kscounties$NAME
mocountynames<-mocounties$NAME
## combines both counties
bothcounties<-rbind(kscounties,mocounties)
bothcountynames<-c(kscountynames,mocountynames)
## color pallette
pal<-colorNumeric("viridis",NULL)
## placeholder
percent=c(1:100)
## creates leaflet of kansas and missouri counties
leaflet(bothcounties) %>%
addTiles() %>%
addPolygons(stroke = FALSE, smoothFactor = 0.3, fillOpacity = 1,
fillColor = ~pal(percent),
label = ~paste(bothcountynames, "\n", formatC(percent, big.mark = ",")
)) %>%
setView(-98.4,38.5,zoom=6) %>%
addLegend(position="bottomright",pal = pal, values = percent, opacity = 1.0,title="Percent") %>%
addLayersControl(position="topleft",
baseGroups = c("1","2"),
overlayGroups=c("A","B","C","D","E","F")
)
You can just create a separate dataframe containing the data in question and refer to it when plotting. You just have to be careful to ensure the order of counties in the dataframe matches the order of counties in the geo-data. The county names you're already extracting should work as a 'key' to match that order.
From your code above, change the #placeholder part to..
data_to_plot <- data.frame("NAME"=bothcountynames,"data"=###YOURDATA_IN_CORRECT_ORDER###))
..with the data you want to plot. You could also just set-up a single-column dataframe with the names and then do a merge/join as it may be an easier way to maintain the required order.
In the leaflet call, put fillColor = pal(data_to_plot$data). The ~ basically won't be necessary if the data you're referencing is stored in a separate object.

Resources