I am trying to convert a SpatialPolygonsDataFrame layer to a DataFrame to create plots using ggplot2 in an R Script within QGIS. I've used spatial join to append averages of BioClim mean temperature tiles within each of many polygon land class features.

When I pull up the attribute table for the layer, all values are filled for each of the features; however, R in QGIS keeps throwing the error:

Error in data.frame(x=Layer[[X]], y=Layer[[Y]]):
arguments imply differing number of rows: 9382, 0
Execution halted

For some reason it doesn't seem to be picking up the second field of the SpatialPolygonsDataFrame object.

The R script I've composed thus far is here:

##Vector Processing=group
##X=Field Layer
##Y=Field Layer


ggplot(data=data.frame(x=Layer[[X]], y=Layer[[Y]])) + geom_histogram(aes(x=x)) + facet_wrap(~y)

This question is a follow-up to one of my earlier ones, which is here: Use facet wrap in ggplot2 with QGIS?

Does anyone have an ideas of why this error is being thrown?

  • 1
    There is a problem with Layer[[Y]] (length=0), can you provide a reproducible example?
    – rcs
    Commented Nov 10, 2015 at 16:56
  • I'm not sure how to provide a reproducible example... as all of my data are in shapefile formats (a bit new to the GIS/R world). However, I created a new column that converts the Layer[[Y]] variable to an integer (though I thought it was already in that format)... and it took care of the issue. I'm not sure how to check the format (i.e. integer vs. string) of the original data column.
    – jbukoski
    Commented Nov 10, 2015 at 17:35
  • Use fortify: github.com/hadley/ggplot2/wiki/plotting-polygon-shapefiles
    – mdsumner
    Commented Nov 10, 2015 at 21:51
  • @mdsumner fortify isn't useful here - OP is just trying to histogram an attribute column by values of another attribute column (misleading called X and Y) , not do anything spatial.
    – Spacedman
    Commented Nov 11, 2015 at 13:35
  • 1
    One way to debug these things is to sprinkle generous amounts of str and print functions - str(Y) will tell you the structure of Yfor example, and then you can make sure it is what you think it is. Also to check that column Y exists, do print(names(Layer)). I suspect its a typo somewhere...
    – Spacedman
    Commented Nov 11, 2015 at 14:22

1 Answer 1


Looks like a QGIS bug.

I have a layer with an attribute column called "Gastro_Rate", but the corresponding name in the Layer object passed to my R script is "Gastro_Rat", with an "e" missing. The drop-down says "Gastro_Rate", the "X" variable is "Gastro_Rate", so it tries to get "Gastro_Rate" from a spatial data frame with "Gastro_Rat" in it. So it fails, gets 0 values, and your script tries and fails to make a data frame.

Here's my test script:

##Vector Processing=group
## showplots
##X=Field Layer

if(! X %in% names(Layer)){
 message("Error - attribute ",X," not in ",paste(names(Layer),collapse=","))

I suspect strongly there's a 10-character limit somewhere, and I have a strong feeling of deja-vu... Now there is a 10-char limit on names in shapefiles, but that doesn't apply to QGIS attribute names when they come from other data sources (mine's from an SQLite database).

Anyway, this is partly a hunch because we don't know what your data source is. But I reckon your spatial join has resulted in long attribute names.

Ah-ha! The R Algorithm in the Processing toolbox writes QGIS layers to temporary Shapefiles that are read into R - so the name truncation happens there. I suspect the easiest fix would be for QGIS to write the full attribute names to another temporary file, alongside the temporary shapefile, then read that in and assign the new names to the spatial data frame before running the script code.

  • 1
    I've just filed a QGIS bug report on this
    – Spacedman
    Commented Nov 12, 2015 at 16:30
  • yes, I was having the cut-off field name issue as well. It makes sense now, but I didn't initially recognize the cut-off field name as a bug. I just circumnavigated the issue by going into the .dbf file... Anyways, good catch! And thank you for filing the report.
    – jbukoski
    Commented Nov 13, 2015 at 4:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.