Plotting Canmaps line data in R - r

I am new to using R and am working with a shape file from the DMTI Canmap dataset: http://www.dmtispatial.com/canmap/#top
I have been successful importing the shape file as a 'SpatialLineDataFrame' using readOGR. I have been unsuccessful in mapping the data. Here is what I have tried so far. Note: I have named the data frame 'trans'.
library(maps)
library(rgdal)
1 Plotting the longitude and latitude coordinates
plot(trans$LONGITUDE,trans$LATITUDE)
This command plots the coordinates as points not lines. I'd like to map the lines.
2 Using ggmap to plot
library(ggmap)
can <- get_map(location="canada",zoom=3)
ggmap(can) + geom_point(data=trans, aes(x="LONGITUDE",y="LATITUDE"))
This returns an error saying 'Error: Discrete value supplied to continuous scale'
ggmap(can) + geom_path(data=trans, aes(x="LONGITUDE",y="LATITUDE"))
This also returns an error saying 'Error: Discrete value supplied to continuous scale'
As a heads-up, when I ask for the names of the data stored in 'trans' I get these names: "UID" "NAME" "TYPE" "LOCATION" "NUM_LINES" "MUNICIPAL" "PROV" "LONGITUDE" "LATITUDE" "BEGELEV" "ENDELEV" "ACCURACY" "ACQ_TECH" "VALDATE" "PROVIDER" "SHAPE_Leng"
Are there packages or commands you would recommend trying in order to map the lines stored in this .shp file using R?

Related

Filtering in R using climate data

I have two parts (a) : i solve it using map , (b): depend on (a) using filter.
(a) plot all the cities in the data set using map
(b) based on (a)
plot citys dot is coloured by the variable(wsdi) predicted for year(2100) in scenario( RCP2.6)
(a)
install.packages("ggplot2")
install.packages("tidyverse")
library(ggplot2)
library(tidyverse)
map_world<- borders("world",fill="white",colour="gray80")
map<- ggplot()+map_world+ylim(-60,80) #empty map
mp
mp1<- mp+geom_point(aes(x=ex$lon,y=ex$lat),color="darkblue",fill=ex$city)
mp1
(there is error massege in name of one city )
I try to use this code before, before solve
install.packages("DataEditor")
library(DataEditR)
ex_subset<- data_edit(x=ex)
(b)
mp_RCP2.6<- mp1+geom_point(wsdi%>% filter year==2100& scenario=="RCP2.6")
mp_RCP2.6
but i dont have result ?

Global cartogram in R

I am trying to create a global cartogram using the cartogram package in R. I am trying to use the data from wrld_simpl. What I expect is a cartogram in which the Population ("Pop2005" variable) is plotted. The code I have developed is this:
data(wrld_simpl)
world<-wrld_simpl
world_sf = st_as_sf(world)
world_sf_proj = st_transform(world_sf, crs = 3785)
world_cartogram <- cartogram_cont(world_sf_proj, "POP2005")
plot(world_cartogram)
Nonetheless, this has resulted in the following figure:
Do you know what is wrong with the code? Maybe the CRS? I have tried to use others CRS but I got the following error:
"Error: Using an unprojected map. This function does not give correct centroids and distances for longitude/latitude data:
Use "st_transform()" to transform coordinates to another projection."
Taken from this documentation, it is stated that
The default plot of an sf object is a multi-plot of all attributes, up
to a reasonable maximum
If you want to use the base R plot function, then use st_geometry(your_map) to plot (the geometry) an sf object.
Another possibility (which I don't recommend) is to set plot options to 1 plot maximum (options(sf_max.plot=1)), but this plots the first variable, and it might not be the best idea.
library(sf)
library(spData)
library(cartogram)
library(tidyverse)
world_sf = st_as_sf(world)
world_sf_proj = st_transform(world_sf, crs = 3785)
world_cartogram <- cartogram_cont(world_sf_proj, "pop")
plot(st_geometry(world_cartogram))
Now, sf is particularly well suited with ggplot2 and the tidyverse. In that setting, just use ggplot in combination with geom_sf.
ggplot(world_cartogram) +
geom_sf()

"Wasn't able to determine range of domain" for ColorNumeric

I'm trying to create a choropleth map in R. I've merged my shapefiles and data files. I'm trying to create a palette for the different colours I want my data to display in on my choropleth. When I use the colorNumeric function it returns an error message.
library(leaflet)
library(rgdal)
library(tigris)
library(dplyr)
LAsMap <- readOGR(dsn = "C:/Users/LocalAuthorityShapefiles",
layer = "Local_Authority_Districts_December_2017_Generalised_Clipped_Boundaries_in_United_Kingdom_WGS84")
data <- read.csv(file = "C:/projects/N123.csv")
data_merged_map <- geo_join(LAsMap, data, "lad17cd", "lad17nm")
mypalette <- colorNumeric( palette="viridis", domain=data_merged_map$N456)
I expect the output to just run the code and create a palette in my workspace but instead the "Wasn't able to determine range of domain" error message appears.
For me, this happened because I had -Inf values. Originally they were 0s, but I log10() transformed the vector, and that's what caused them to be -Inf for me.

r geom_map fails with GeoJSON map simplified with gSimplify

I'm constructing world maps with countries color-filled with the (continuous) value depending on a column in a data frame called temp.sp. I want to put several of these maps in a graph. I construct each map using ggplot with geom_map and then construct and display the graphs using multiplot() which uses grid code.
I'm using a GeoJSON map (world <- readOGR(dsn = "ne_50m_admin_0_countries.geojson", layer = "OGRGeoJSON")). The resulting SpatialPolygonsDataFrame is 4.1 Mb and the dataframe that results from worldMap <- broom::tidy(world, region = "iso_a3") has 93391 rows. So when I run multiplot with 4 plot files, it takes a long time.
I thought that I could speed up the printing by simplifying the world map with gSimplify using code like world.simp <- gSimplify(world, tol = .1, topologyPreserve = TRUE). The resulting data frame, worldMap.simp only has 27033 rows but when I use this map I get the error message Error in unit(x, default.units) : 'x' and 'units' must have length > 0.
The error message is generated when I run this code with worldMap.simp. When I use worldMap I have no problems.
gg <- ggplot(temp.sp, aes(map_id = id))
gg <- gg + geom_map(aes(fill = temp.sp$value), map = worldMap.simp, color = "white").
I tried converting temp.sp$value to factor but it made no difference.
To summarize, using a gSimplified map causes the displaying of a graph produced with ggplot and geom_map to fail.
Rather than try to figure out what was going wrong with gSimplify, I found and downloaded a lower resolution map from http://geojson.xyz. The one I'm currently using is
https://d2ad6b4ur7yvpq.cloudfront.net/naturalearth-3.3.0/ne_110m_admin_0_countries.geojson
Note that it has a similar filename, but with 110m instead of 50m.

Error while producing choropleth map with maptoools

Hello, I am new to GIS with R and have been trying to create a choropleth map. I successfully created a choropleth map with ggplot2 and the fortify function, but it is not that easy to add more layers on the top of a map with ggplot2. Instead I am using maptools to plot a choropleth map and later add more layers as needed for my analysis.
The choropleth map I am trying to plot is the level of unemployment for Allegheny county by census tract. the files are available here:
shapefile https://www.dropbox.com/s/uci5g2ekeq9niww/census%20tract%20allegheyny%202010.shp
csv file https://www.dropbox.com/s/6nq8nnxftot8iya/allegheyny%20socioeconomic%20info.csv
And here is my code
library(rgdal)
library(RArcInfo)
library(RColorBrewer)
library(maptools)
library(maps)
library(classInt)
we load the csv file, clean it and create a subset with Id2 and unemployment
data<- read.csv('allegheyny socioeconomic info.csv',dec='.',
header=T)
data$Id2<-as.numeric(as.character(data$Id2))
data$Percent.Unemployed<-as.numeric(as.character(data$Percent.Unemployed))
names(data)[names(data)=="Percent.Unemployed"]<-'unemployed'
data1<-subset(data, select= c('Id2', 'unemployed'))
load shapefile of Allegheny County census tracts for 2010
tracts<-readShapePoly("census tract allegheyny 2010.shp")
names(tracts)[names(tracts)=="GEOID10"]<-'Id2'
merge the data by Id2
tr1<-merge(data1,tracts)
sort(tr1$Id2)
colours<-brewer.pal(5, 'Greens')
breaks<- classIntervals(tr1$unemployed, n=5, style='sd')
plot(tr1, col=colours[findInterval(tr1$unemployed, breaks, all.inside=T), axes=F])
And this is the message I get:
Error in x[-1L] >= x[-n] : comparison of these types is not implemented
plot(tracts,col=colours[findInterval(tr1$unemployed, breaks$brks, all.inside=T)])
Produces this:
To answer your question specifically:
The reason for the error is that findInteerval(...) takes as its second argument a vector of numeric. But
breaks<- classIntervals(tr1$unemployed, n=5, style='sd')
produces a list with two elements: 'var' and 'brks'. You need to use breaks$brks in findInterval(...).
Your statement
plot(tr1, col=colours[findInterval(tr1$unemployed, breaks, all.inside=T), axes=F])
attempts to plot tr1, which is not a map, it's a data frame with 402 rows. You need to plot(tracts,...)
Finally, why do you believe it is difficult to add layers in ggplot??

Resources