0

I am trying to create a map, showing data on a certain metric in 2007, with the colour on a gradient rather than banded.

The response showed here from SlowLearner (with the blue map) looks like it would get the results I want, so I have adapted this, but I am struggling to make this work.

The issue currently is that when I run the final line of code, I get the following error:

Error: Aesthetics must be either length 1 or the same as the data (1619341): group, x, y, fill

Here is my code, edited to anonymise the data.

EDIT: I've updated the code, replicated below, and now I get a map but it's all in one colour with no legend.

datafile <- rio::import("csv file from computer", header=TRUE)

shapefile <- readOGR(dsn="shapefile folder from computer", layer = "shapefile doc")

datafile <- datafile[order(datafile$region),]

shapefile <- shapefile[order(shapefile$region),] 

mergedfile <- append_data(shapefile, datafile, key.shp="region", key.data="region", ignore.duplicates=TRUE) 

ggplot(mergedfile, aes(x = long, y = lat), group = group, fill = mergedfile$"2007") +
  geom_polygon(colour = "black", size = 0.5, aes(group = group)) +
  theme()

I've searched and I can't work out what this means or what I need to change to make this work. Help appreciated!

Jaccar
  • 1,720
  • 17
  • 46
  • Can't reproduce your error without data, but at a glance, `fill = mergedfile$"2007"` looks weird. Does `mergedfile` have a column named 2007? – Z.Lin Aug 24 '17 at 13:07
  • Have you seen this [post](https://stackoverflow.com/questions/37778174/error-aesthetics-must-be-either-length-1-or-the-same-as-the-data-4)? Please provide reproducible example, see [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to create one. – mnm Aug 24 '17 at 13:08
  • Thanks Ashish, I will have a look and do so now. – Jaccar Aug 24 '17 at 13:23
  • @Z.Lin, yes, mergedfile has a column named 2007, which is the data that I want to appear in a gradient colouring in the regions. – Jaccar Aug 24 '17 at 13:23
  • Hi @Ashish... (a) I looked at that post and the only thing that looked relevant was possibly moving the brackets so only x and y were in the aes() in the ggplot line - I did this, and now it produces the map but all in the same colour, without a legend. (b) I've tried to create a reproducible file, and have created a csv file with dummy data to merge with my shapefile, but when I use the dput() command and copy it into a new session, it gives me a ton of errors! I'm copying directly from the console into the new session as indicated. – Jaccar Aug 24 '17 at 14:17

0 Answers0