title: R ggplot2 merge with shapefile and csv data to fill polygons
body: <p>We daily produce maps that show a calculated level for temperature in 30 distinct areas of our region, each area is filled with a different colour depending on the level. This maps look like</p>
  
  <p><img src="https://i.stack.imgur.com/HU0PJ.png" alt="enter image description here"></p>
  
  <p>Now I want to switch map generation to R. I've downloaded provincial and municipal boundaries (you can find <a href="http://centrodedescargas.cnig.es/CentroDescargas/equipamiento/lineas_limite.zip" rel="noreferrer">boundaries for whole Spain</a> or here the <a href="http://ubuntuone.com/7VMshSPgseaeJm2B7SXOz3" rel="noreferrer">subset for my region</a>) and  managed to plot them with ggplot2 following Hadley's <a href="https://github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles" rel="noreferrer">example</a>.</p>
  
  <p>I can also produce an ascii file that contains two columns: identifier (CODINE) and daily level. You can download <a href="http://ubuntuone.com/6pSCvM52sz82maoLkk0n96" rel="noreferrer">here</a>.</p>
  
  <p>This is my first script attempting to plot shapefiles with R and ggplot2 so there may be mistakes and for sure it can be improved, suggestions welcome. The following code (based on Hadley's previously mentioned) works for me:</p>
  
  <pre><code>&gt; require("rgdal")
  &gt; require("maptools")
  &gt; require("ggplot2")
  &gt; require("plyr")
  
  # Reading municipal boundaries
  
  esp = readOGR(dsn=".", layer="lineas_limite_municipales_etrs89")
  
  muni=subset(esp, esp$PROV1 == "46" | esp$PROV1 == "12" | esp$PROV1 == "3")
  muni@data$id = rownames(muni@data)
  muni.points = fortify(muni, region="id")
  muni.df = join(muni.points, muni@data, by="id")
  
  # Reading province boundaries
  
  prov = readOGR(dsn=".", layer="poligonos_provincia_etrs89")
  
  pr=subset(prov, prov$CODINE == "46" | prov$CODINE == "12" | prov$CODINE == "03" )
  pr@data$id = rownames(pr@data)
  pr.points = fortify(pr, region="id")
  pr.df = join(pr.points, pr@data, by="id")
  
  ggplot(muni.df) + aes(long,lat,group=group) + geom_path(color="blue") +
  + coord_equal()+ geom_path(data=pr.df, + 
  aes(x=long, y=lat, group=group),color="red", size=0.5) 
  </code></pre>
  
  <p>This code plots a nice map with all the boundaries <img src="https://i.stack.imgur.com/ikgpZ.png" alt="enter image description here"></p>
  
  <p>For polygon filling by level I tried to read and then merge as suggested in <a href="http://tormodboe.wordpress.com/2011/02/22/g%C3%B8y-med-kart-2/" rel="noreferrer">http://tormodboe.wordpress.com/2011/02/22/g%C3%B8y-med-kart-2/</a> </p>
  
  <blockquote>
    <p>level=read.csv("levels.dat",header=T,sep=" ")<br>
    munlevel=merge(muni.df,level,by="CODINE")</p>
  </blockquote>
  
  <p>but it gives an error </p>
  
  <blockquote>
    <p>Error en fix.by(by.x, x) : 'by' must specify a uniquely valid column</p>
  </blockquote>
  
  <p>I am not familiar with shapefiles, maybe I need to learn more on shp data attributes to find the  right choice to merge both data sets. How can I merge data so I can plot the lines (municipal boundaries) and then fill it with levels? </p>