Filtering in R using climate data - r

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 ?

Related

Mapping with shapefiles in R

I have a shapefile with tons of different polygons representing different bodies of water (lakes, rivers etc.)
I would like to create a map of certain polygons. All of the current examples I could find only show how to plot shapefiles with ONLY the polygons wanted in them. So I am not sure how to only plot the specific polygons.
Link to the shapefile: https://hub.arcgis.com/datasets/esri::usa-detailed-water-bodies/explore?location=45.461044%2C-84.374110%2C10.71
I would like to plot OBJECTID295061, 295018, and 295017
Current code:
library(sf)
shp = st_read("USA_Detailed_Water_Bodies.shp")
ggplot(data = shp) +
geom_sf()+
coord_sf()+
theme_bw()
Current map:
Assuming OBJECTID is a numeric column of your data:
library(sf)
shp = st_read("USA_Detailed_Water_Bodies.shp")
Then you can make an "area of interest" subset like this:
aoi = shp[shp$OBJECTID %in% c(295061, 295018, 295017),]
Then make your plot using aoi.
If the OBJECTID numbers are the same as the row numbers you can select by row:
aoi = shp[c(295061, 295018, 295017),]
but I'm not sure because no time for a 250Mb download right now but I think this is correct.
Spatial dataframes from sf behave mostly like regular data frames, with a few weird exceptions. But for selecting rows and columns its not very different.

Represent a colored polygon in ggplot2

I am using the statspat package because I am working on spatial patterns.
I would like to do in ggplot and with colors instead of numbers (because it is not too readable),
the following graph, produced with the plot.quadratest function: Polygone
The numbers that interest me for the intensity of the colors are those at the bottom of each box.
The test object contains the following data:
Test object
I have looked at the help of the function, as well as the code of the function but I still cannot manage it.
Ideally I would like my final figure to look like this (maybe not with the same colors haha):
Final object
Thanks in advance for your help.
Please provide a reproducible example in the future.
The package reprex may be very helpful.
To use ggplot2 for this my best bet would be to convert
spatstat objects to sf and do the plotting that way,
but it may take some time. If you are willing to use base
graphics and spatstat you could do something like:
library(spatstat)
# Data (using a built-in dataset):
X <- unmark(chorley)
plot(X, main = "")
# Test:
test <- quadrat.test(X, nx = 4)
# Default plot:
plot(test, main = "")
# Extract the the `quadratcount` object (regions with observed counts):
counts <- attr(test, "quadratcount")
# Convert to `tess` (raw regions with no numbers)
regions <- as.tess(counts)
# Add residuals as marks to the tessellation:
marks(regions) <- test$residuals
# Plot regions with marks as colors:
plot(regions, do.col = TRUE, main = "")

Color tip labels in cophylo tree using R

I have created the following tree using the code below.
library(ape)
library(phytools)
#Create Random Trees just for demo purposes
TreeA<-rtree(10)
TreeB<-rtree(10)
#Convert both trees to cladograms
TreeA<-compute.brlen(TreeA)
TreeB<-compute.brlen(TreeB)
#creation of the association matrix:
association <- cbind(TreeB$tip.label, TreeB$tip.label)
searchstring1<-c("t1","t3")
searchresult1<-which(Reduce(`|`, lapply(searchstring1, grepl, x=association[,1])))
#Define which associations get a different color connector
col<-rep(make.transparent("blue",0.4),nrow(association))
col[c(searchresult1)]<-make.transparent("red",0.4)
#Build the cophylo object
obj<-cophylo(TreeA,TreeB,assoc=association, rotate=TRUE)
#Get set to export the plotted trees
setwd("C:/Users/jamie/Desktop/")
pdf(file="cophyloexample.pdf",height=14,width=14)
#Build the plot
plot(obj,link.type="curved",link.lwd=3, link.lty="solid", link.col=col, fsize=5, hang=-1)
I have tried tiplabel.color=col, label.col=col, tip.col=col, etc.
There is no documentation that I can find showing how this is done. How can this be done?
I have been successful in coloring the connectors but I can't figure out how to color the tip labels the same as the connector. For example, t10, t3 and t1 all need to be red. How can this be accomplished.

"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.

How to map a single US state (MO) county population data using maps package?

I'm having difficulty mapping gradient colors to some county-level population data I have using the base R package maps. I know that colors must be interpolated to the dataframe, but I'm not sure how that is then translated to the map. Here is the code I'm using:
library(dplyr)
require(maps)
my_fake_data <- data_frame(
county = sample(c('list','of','all','counties'),115,T),
county_population = sample(1:1000000,115,T))
grey_black <- colorRampPalette(c('grey50','black'))
map_data <- my_fake_data %>%
arrange(county_population) %>%
mutate(county_population_color = grey_black(nrow(.)))
map('county','missouri',interior = T,fill =T,
col = grey_black(map_data$county_population_color))
How do I tell R to map colors in the correct order? My sense tells me to attach my data to map's internal database, but I can't find the documentation to do it correctly - - or, more likely, I'm just wrong. Any help would be greatly appreciated.
To answer your question, you will need to access the county.fips data frame contained in the maps package. This will have the fips number and the state, county name. This list is in the correct order for mapping. The code example below extracts the Missouri counties and randomly colors a couple for verification:
mocounties<-county.fips[grepl('missouri', county.fips$polyname),]
mocounties$col<-"grey"
mocounties$col[5]<-"red"
mocounties$col[15]<-"green"
mocounties$col[115]<-"blue"
map('county','missouri', interior = T,fill =T,
col = mocounties$col)

Resources