R: Gradient graph on shapefile

I currently have a UK shapefile, and I am referring to a population of species in different regions of the UK. So far, I just made up 3 levels of the species population and colored them red = high, orange = honey, green = low. But what I would like to do would be to have a gradient graph instead of being limited to only 3 colors. So far I have a table called โ€œGraphโ€ that has regions as column names, and then the number of views for each region is lower. My lowest score is 0, and my highest score is around 2500, and the regions in Count are the same as the regions in my shapefile. I have a function that determines what is high, medium, low based on the levels you enter yourself.

High<-colnames(Count)[which(Count>'input value here')] 

and then they are superimposed on the shapefile as follows:

  plot(ukmap[(ukmap$Region %in% High),],col='red',add=T) 

Unfortunately, I cannot install any packages, I thought about using colorRamp, but I'm not sure what to do?

EDIT: my data looks something like this.

  Wales Midlands North Scotland South East South West 1 551 32 124 1 49 28 3 23 99 291 152 164 107 4 1 7 17 11 21 14 7 192 32 12 0 1 9 9 98 97 5 1 21 0 

and the first column is just a number representing the view, and currently I have a function that calculates the score for the British shapefile, but is based on the borders of high, medium and low. The above data is not tied to my shapefile. Then I scroll through each row (view) of my dataset and draw a new map for each row (view).

+4
source share
3 answers

OK, here is an alternative solution that does not use ggplot (I will leave the ggplot solution for reference). This code is simple, but it should be enough to give you some ideas on how you can adapt it to your own data.

 # UK shapefile found via http://www.gadm.org/download uk.url <- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip" # replace following with your working directory - no trailing slash work.dir <- "C:/Temp/r.temp/gb_map" # the full file path for storing file file.loc <- paste0(work.dir, "/uk.zip") download.file (uk.url, destfile = file.loc, mode = "wb") unzip(file.loc, exdir = work.dir) # open the shapefile require(rgdal) uk <- readOGR(work.dir, layer = "GBR_adm2") # make some fake data to plot uk@data $count <- round(runif(nrow( uk@data ), 0, 2500), 0) uk@data $count <- as.numeric( uk@data $count) # and plot it plot(uk, col = gray( uk@data $count/2500)) 

The result of the code is the following graph.

screenshot

EDIT after requesting to include a legend, I changed the code a bit, but to be honest, I do not understand the basic function of R legend well enough to get something from the quality of the product, and I do not want to investigate it in the future. (By the way, tell the hat that this question is for ideas.) A look at the plot under the code suggests that we need to change the color order of the legend, etc., But I will leave this on the original poster as an exercise or for publication as another question.

 # UK shapefile found via http://www.gadm.org/download uk.url <- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip" # replace following with your working directory - no trailing slash work.dir <- "C:/Temp/r.temp/gb_map" # the full file path for storing file file.loc <- paste0(work.dir, "/uk.zip") download.file (uk.url, destfile = file.loc, mode = "wb") unzip(file.loc, exdir = work.dir) # open the shapefile require(rgdal) uk <- readOGR(work.dir, layer = "GBR_adm2") # make some fake data to plot uk@data $count <- as.numeric(round(runif(nrow( uk@data ), 0, 2500), 0)) uk@data $bin <- cut( uk@data $count, seq(0, 2500, by = 250), include.lowest = TRUE, dig.lab = 4) # labels for the legend lev = levels( uk@data $bin) lev2 <- gsub("\\,", " to ", lev) lev3 <- gsub("\\]$", "", lev2) lev4 <- gsub("\\(|\\)", " ", lev3) lev5 <- gsub("^\\[", " ", lev4) my.levels <- lev5 # Create a function to generate a continuous color palette rbPal <- colorRampPalette(c('red','blue')) uk@data $Col <- rbPal(10)[as.numeric(cut( uk@data $count, seq(0, 2500, by = 250)))] # Plot plot(uk, col = uk@data $Col) legend("topleft", fill = uk@data $Col, legend = my.levels, col = uk@data $Col) 

screenshot

+3
source

Ok, I will bite. I will not use the R base because plot too hard to understand, so we will use ggplot2 .

 # UK shapefile found via http://www.gadm.org/download uk.url <- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip" # replace following with your working directory - no trailing slash work.dir <- "C:/Temp/r.temp/gb_map" # the full file path for storing file file.loc <- paste0(work.dir, "/uk.zip") download.file (uk.url, destfile = file.loc, mode = "wb") unzip(file.loc, exdir = work.dir) # open the shapefile require(rgdal) require(ggplot2) uk <- readOGR(work.dir, layer = "GBR_adm2") # use the NAME_2 field (representing counties) to create data frame uk.map <- fortify(uk, region = "NAME_2") # create fake count data... uk.map$count <- round(runif(nrow(uk.map), 0, 2500), 0) # quick visual check ggplot(uk.map, aes(x = long, y = lat, group = group, fill = count)) + geom_polygon(colour = "black", size = 0.5, aes(group = group)) + theme() 

This generates output, which may be similar to what you need.

screenshot

Please note that in this case we will not explicitly specify the gradient - we just leave it to ggplot . If you want to specify this data, it is possible, but more active. If you go down this route, you must create another column in uk.map to select each account in one of (for example) 10 boxes using the cut function. The uk.map data frame is as follows:

 > str(uk.map) 'data.frame': 427339 obs. of 8 variables: $ long : num -2.05 -2.05 -2.05 -2.05 -2.05 ... $ lat : num 57.2 57.2 57.2 57.2 57.2 ... $ order: int 1 2 3 4 5 6 7 8 9 10 ... $ hole : logi FALSE FALSE FALSE FALSE FALSE FALSE ... $ piece: Factor w/ 234 levels "1","2","3","4",..: 1 1 1 1 1 1 1 1 1 1 ... $ group: Factor w/ 1136 levels "Aberdeen.1","Aberdeenshire.1",..: 1 1 1 1 1 1 1 1 1 1 ... $ id : chr "Aberdeen" "Aberdeen" "Aberdeen" "Aberdeen" ... $ count: num 1549 1375 433 427 1282 ... > 
+6
source

Have you tried colorRampPalette?

Here's how you could try creating a gradient palette

  gradient_color <- colorRampPalette(c("blue", "red")) gradient_color(10) 

[1] "# 0000FF" "# 1C00E2" "# 3800C6" "# 5500AA" "# 71008D" "# 8D0071" "# AA0055" [8] "# C60038" "# E2001C" "# FF0000"

Graph example

  plot(rep(1,10),col=gradient_color(10)) 
0
source

Source: https://habr.com/ru/post/1494952/


All Articles