Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>OK, here is an alternative solution that doesn't use <code>ggplot</code> (I will leave the <code>ggplot</code> solution for reference). This code is simple but it should be enough to give you some ideas as to how you can adapt it to your own data.</p> <pre><code># UK shapefile found via http://www.gadm.org/download uk.url &lt;- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip" # replace following with your working directory - no trailing slash work.dir &lt;- "C:/Temp/r.temp/gb_map" # the full file path for storing file file.loc &lt;- 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 &lt;- readOGR(work.dir, layer = "GBR_adm2") # make some fake data to plot uk@data$count &lt;- round(runif(nrow(uk@data), 0, 2500), 0) uk@data$count &lt;- as.numeric(uk@data$count) # and plot it plot(uk, col = gray(uk@data$count/2500)) </code></pre> <p>The result of the code is the following plot.</p> <p><img src="https://i.stack.imgur.com/otd46.png" alt="screenshot"></p> <p>EDIT following a request to include a legend, I have tweaked the code a little but in all honesty I don't understand base R's <code>legend</code> function well enough to get something of production quality and I have no wish to research it further. (Incidentally hat tip to <a href="https://stackoverflow.com/questions/9946630/colour-points-in-a-plot-differently-depending-on-a-vector-of-values">this question</a> for ideas.) A look at the plot beneath the code suggests that we need to reorder the legend colours etc but I will leave that to the original poster as an exercise or to post as another question.</p> <pre><code># UK shapefile found via http://www.gadm.org/download uk.url &lt;- "http://www.filefactory.com/file/s3dz3jt3vr/n/GBR_adm_zip" # replace following with your working directory - no trailing slash work.dir &lt;- "C:/Temp/r.temp/gb_map" # the full file path for storing file file.loc &lt;- 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 &lt;- readOGR(work.dir, layer = "GBR_adm2") # make some fake data to plot uk@data$count &lt;- as.numeric(round(runif(nrow(uk@data), 0, 2500), 0)) uk@data$bin &lt;- 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 &lt;- gsub("\\,", " to ", lev) lev3 &lt;- gsub("\\]$", "", lev2) lev4 &lt;- gsub("\\(|\\)", " ", lev3) lev5 &lt;- gsub("^\\[", " ", lev4) my.levels &lt;- lev5 # Create a function to generate a continuous color palette rbPal &lt;- colorRampPalette(c('red','blue')) uk@data$Col &lt;- 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) </code></pre> <p><img src="https://i.stack.imgur.com/pDYOD.png" alt="screenshot"></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload