0

So I'm plotting a shape file (from the ONS) of Great Britain split into 11 regions with the hope of creating a choropleth map based on COVID-19 cases.

I join the covid data with the shape file so that I can work within 1 data frame, joining on the region name.

I've used the longitude and latitude fields of the shape file for the x and y values within the aesthetics.

covid <- data.frame(Name = c("Scotland","Eastern","West Midlands","Yorkshire and the Humber","East Midlands","London","South West","South East","North West","North East","Wales"), 
                    Cases = c(20,50,45,30,25,75,100,5,60,35,80))

#'greatb' is the name of the shape file
join <- merge(greatb,covid,by=c("NAME","Name"),by.x=c("NAME"),by.y=c("Name"), all=TRUE)

ggplot()+
 geom_polygon(data=join, aes(x=long, y=lat, group=group, fill=Cases))

However, it seems that once I do this I can't use a variable name to fill the regions of the map. I get confronted with the error message: object 'Cases' not found

I'm unsure why I get this is message though as 'covid$data' is clearly an object and therefore so is 'join$data'. Can anyone help me with this?

  • Remove the `by = c(...)` argument. You are specifying `by.x = ` and `by.y = `. See if that helps. The `by = ` argument expects a single value that is common to both data.frames. – Ben Norris May 01 '21 at 17:56
  • 1
    If `Cases` exists in `greatb` (don't know), then post-merge you likely have two columns named `Cases.x` and `Cases.y`. (@BenNorris, fyi `by` is just a convenience: once `by.x` and `by.y` are used, `by` is ignored.) – r2evans May 01 '21 at 18:15
  • Hello Jack I think you you need to fill with a factor or character and not with numbers – LDT May 01 '21 at 18:27
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. You've defined `covid` but not `greatb` so we can't run and test the code. Check what `names(join)` returns and make sure `Cases` is there. – MrFlick May 01 '21 at 19:33
  • What are the column names in `join` before you plot? Debug this step-by-step. In `merge()` type of functions, typically your column names may have `.x` or `.y` appended to them. You may want to use `dplyr::bind_rows()` instead. – chemdork123 May 02 '21 at 12:48

0 Answers0