Heat map of Germany using spplot
Asked Answered
D

1

6

I was working on heat map of Germany using spplot, I had the shape file from GADM German shape file Level 1

http://biogeo.ucdavis.edu/data/gadm2.8/rds/DEU_adm1.rds

I am able to make the heatmap but i suppose the maps are getting wrongly plotted, as for instance in my data "Bremen" has value as 0 but "Sachsen-Anhalt " is being plotted as white with 0 values, is it something with the mapping in the .rds file ?

Here is my code

  library(sp)
library(latticeExtra)

### load the German federal state polygons
my.data <- readRDS("DEU_adm1.rds")
sample <- read.csv(file.choose())
 final <- merge(x =my.data@data, y = sample, by = "ID_1", all.y = TRUE)
my.data@data <- data.frame(my.data@data, sample[match(my.data@data[,"ID_1"], sample[,"ID_1"]),])
### German language hick-ups need to be resolved
enamessp <- gsub("?", "ue", my.data@data$NAME_1)
my.data@data$NAME_1 <- enamessp

### insert the newly created clicksvariable into the spatial data frame
my.data$clicks <- sample$clicks


clrs <- c('#F4F1A2',
          '#F4F1A2',
          '#E6EAA2',
          '#E6EAA2',
          '#CFE3A2',
          '#CFE3A2',
          '#9AD0A3',
          '#9AD0A3',
          '#7FC9A4',
          '#7FC9A4',
          '#32B9A3',
          '#32B9A3',
          '#00A7A2',
          '#00667E',
          '#00667E',
          '#1D4F73'
)
spplot(my.data, zcol = "clicks", main = "Region Distribution", 
       col.regions = clrs,at=sort(sample$clicks))

Here is the dput for sample :

structure(list(ID_1 = c(7L, 4L, 5L, 14L, 12L, 15L, 11L, 13L, 
2L, 3L, 16L, 6L, 10L, 9L, 8L, 1L), clicks = c(19L, 4L, 0L, 12L, 
4L, 3L, 8L, 5L, 41L, 12L, 4L, 11L, 59L, 19L, 4L, 25L)), .Names = c("ID_1", 
"clicks"), class = "data.frame", row.names = c(NA, -16L))

Output looks like this : enter image description here

Digitalin answered 9/1, 2016 at 9:27 Comment(5)
Sorry, but I really was wondering if a "German heat map" was something different than a normal heat map :).Filament
No it's not just that I have issues with the regions being wrongly plotted, is it the shape file? Can you help here?Digitalin
I looked at it, but where do I get "DEU_adm1.rds"?Filament
I shared the link "German shape file " there you can download it, from the drop down select Germany thereafter from the second drop down select option for shape file for R, then on the resulting page you can download the file named as Level 1. Hope this helpsDigitalin
Ok, fixed your last bug.Filament
F
5

spplot works weirdly, and I went down a couple of blind alleys first. But basicly you were close, that at=sort(sample$clicks) was screwing it up, you just have to get rid of that.

library(sp)

### load the German  geo map polygons
my.data <- readRDS("DEU_adm1.rds")  

### sample "clicks" data with German state coded as ID_1
sample <- data.frame( 
  ID_1 =    c( 7, 4, 5, 14, 12, 15, 11, 13,  2, 3, 16,  6, 10, 9,  8, 1L), 
  clicks =  c(19, 4, 0, 12,  4,  3,  8,  5, 41, 12, 4, 11, 59, 19, 4, 25L)) 

### Merge sample data with geo map data
final <- merge(x =my.data@data, y = sample, by = "ID_1", all.y = TRUE)
my.data@data <- data.frame(my.data@data, 
                           sample[match(my.data@data[,"ID_1"], 
                           sample[,"ID_1"]),])

### German language hick-ups need to be resolved
enamessp <- gsub("?", "ue", my.data@data$NAME_1)
my.data@data$NAME_1 <- enamessp

# print out states and clicks (sorted high to low) for verification
final[ order(-final$clicks),c("ID_1","NAME_1","HASC_1","clicks") ]

### insert the newly created clicksvariable into the spatial data frame
my.data$clicks <- final$clicks

clrs <- c('#F4F1A2','#F4F1A2','#E6EAA2','#E6EAA2',
          '#CFE3A2','#CFE3A2','#9AD0A3','#9AD0A3',
          '#7FC9A4','#7FC9A4','#32B9A3','#32B9A3',
          '#00A7A2','#00667E','#00667E','#1D4F73')

spplot(my.data, zcol = "clicks", main = "Clicks Region Distribution", col.regions = clrs)

Yielding:

enter image description here

And here is the data to check it against:

> print(sample[ order(-sample$clicks), ])
   ID_1 clicks                       land  hasc
13   10     59              Saxony-Anhalt DE.ST
9     2     41               Lower Saxony DE.NI
16    1     25                  Thuringia DE.TH
1     7     19                            DE.BW
14    9     19                     Saxony DE.SN
4    14     12                            DE.BR
10    3     12     North Rhine-Westphalia DE.NW
12    6     11                            DE.SL
7    11      8                      Hesse DE.HE
8    13      5 Mecklenburg-West Pomerania DE.MV
2     4      4                    Bavaria DE.BY
5    12      4                     <Null> DE.HB
11   16      4       Rhineland-Palatinate DE.RP
15    8      4                            DE.SH
6    15      3                            DE.HH
3     5      0                            DE.BE 
Filament answered 9/1, 2016 at 11:27 Comment(7)
Yes this fixes the issue, but i wonder why at=sort(sample$clicks) is screwing things up, ideally it should have binned things based on click volume(as per package description), and then would change color accordingly, correct me if i am wrong hereDigitalin
No, reading the docs at ?spplot: the parameter at is said to "specify at which values colours change". So it sets the transition points. You probably don't need to do that. zcol does what you think that does and you had already specified that.Filament
Sorry, but do you think the map is correct ? The highest clicks is for "Nordrhein-Westfalen"(59 clicks) and not "Sachsen-Anhalt"(5 clicks)(this is mapped as the darkest blue on the color scale), similarly "Bayern" should be green according to the color scale(41 click) but it is yellowish.Digitalin
Could you provide a mapping of IDs to state names. This is a pain to figure out otherwise.Filament
Yes You can look at this data frame for that final <- merge(x =my.data@data, y = sample, by = "ID_1", all.y = TRUE), here you will have region and ID mapping(NAME_1 has the region)Digitalin
Yeah, you had another bug in there. After you merge with final you can't use sample data anymore as a simple vector as the order is now wrong.Filament
Hey, Big Thanks for pointing me the error, I can now see where i messed up, appreciate your help.Digitalin

© 2022 - 2024 — McMap. All rights reserved.