Cartography in R - how to plot numeric and binary values on a map?

825 views Asked by At

I'm currently trying to produce two maps, one with multiple categoric values and one with continous numeric values, as in:

link

I have a dataset which provides the NPA and the two informations for each NPA: the item (category) and the Frequency (on a scale from 1 to 10):

NPA     item           Frequency
1000    huitante        0
1002    huitante        10
1006    quatre-vingt    3
2000    huitante        9

I have as well a specific shapefile for the country I work on (Switzerland). On a previous post, I found some interesting code, that I copy/paste here:

# open the shapefile
require(rgdal)
require(rgeos)
require(ggplot2)
ch <- readOGR(work.dir, layer = "PLZO_PLZ")

# convert to data frame for plotting with ggplot - takes a while
ch.df <- fortify(ch)

# generate fake data and add to data frame
ch.df$count <- round(runif(nrow(ch.df), 0, 100), 0)

# plot with ggplot
ggplot(ch.df, aes(x = long, y = lat, group = group, fill = count)) +
geom_polygon(colour = "black", size = 0.3, aes(group = group)) +
theme()

the author gives in comment some information to plot a specific dataset (and not fake data):

# plot just a subset of NPAs using ggplot
my.sub <- ch.df[ch.df$id %in% c(4,6), ]
ggplot(my.sub, aes(x = long, y = lat, group = group, fill = count)) +
geom_polygon(colour = "black", size = 0.3, aes(group = group)) +
theme()

And says in the comment of the post to :

replace ggplot(my.sub, aes(x = long, y = lat, group = group, fill = count)) with ggplot(my.sub, aes(x = long, y = lat, group = group, fill = frequency))

So I guess I need to extract frequency as a variable

frequency <- table(data$frequency)

And change in the code as indicated in the quote.

Unfortunately, my problem is that it does not work, I get the following comment :

Don't know how to automatically pick scale for object of type table. Defaulting to continuous Error: Aesthetics must either be length one, or the same length as the dataProblems:frequency

My questions are :

  • how can I change the code to include my own data, and plot the numeric value (frequency)
  • how can I change the code to include my own data, and plot categoric value (item)

I don t need to represent frequency and item on the same map, just know how to create to seprated maps.

My dataset is in this file, with as well the shapefile I need to use.

https://www.dropbox.com/sh/5x6r6s2obfztblm/AAAesIOrxn76HU57AIF0y1Oua?dl=0

Any help will be really appreciated!

0

There are 0 answers