2

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!

Community
  • 1
  • 1
  • As far as I can tell your `PLZO_PLZ` data don't actually contain a frequency or count column. Which field are you actually trying to visualize? – Forrest R. Stevens Jun 10 '15 at 15:03
  • I've been playing with your data. It seems to me that you need to merge the two data frames. One is a spatial data frame and the other normal data frame. One more thing. In your data, you have NPA. Is that the equivalent of PLZ in the spatial data frame? – jazzurro Jun 10 '15 at 15:27
  • I m trying to plot the values of "frequency" or "item" according to their NPA associated value (dataset called "data.txt" in my folder). NPA in the data.txt is equivalent to the PLZ in the spatial dtaframe; Any idea of how I could concatenate the PLZ values in the data.txt, still using ggplot2? – Mathieu Avanzi Jun 10 '15 at 18:02
  • I cannot take time right now. But a quick comment. Last night I remember that your data was not aggregated by NPA. It seems to me that you need to clean up your data before you think how you create maps. – jazzurro Jun 10 '15 at 22:47

0 Answers0