I'm trying to use R scripts within QGIS thanks to Processing. There are some example scripts available and I achieved to plots different columns of the attribute table (with the plot built-in package).

What I cannot do is using the ggplot package. This is one of the simple code I've written:

##Vector processing=group
##Field1=Field Layer
##Field2=Field Layer
geom_point(aes([[Field1]], [[Field2]]))

But none plot is shown.

Log output says:

R execution commands
tryCatch(find.package("ggplot2"), error=function(e) install.packages("ggplot2", dependencies=TRUE))
tryCatch(find.package("rgdal"), error=function(e) install.packages("rgdal", dependencies=TRUE))
tryCatch(find.package("raster"), error=function(e) install.packages("raster", dependencies=TRUE))
Layer = readOGR("/media/data/Universita/brema/gis/file_tesi/piezo_e_logger",layer="piezo_tutto_finale")
ggplot(Layer) + geom_point(aes(Layer[[Field1]], Layer[[Field2]]))


And that's a piece of the dbf table (trying to plot Cl vs Br):

ID                     Cl   Br
2818/22/0834    125.70  0.20
2818/22/0851    219.12  0.29
2818/22/0853    298.55  0.26
2818/22/0886    132.32  0.09
B0F                520.60   1.01
B12T           3435.49  3.83
B1T                2322.61  2.54
B21T           2800.90  3.18
B22F            366.87  0.64
B26T           1891.63  2.10
B33F            216.97  0.62
B4F                 383.98  0.44
P1                   82.01  0.14
P21                 365.56  0.74
P24                 199.98  0.73
P25                 308.71  0.90
P27                 217.58  0.58
P28                 436.09  0.32
P29                 711.77  1.08

So why doesn't it work?
Look at your code, specifically the line with the attribute fields. You need to point R/QGIS directly towards the field data vectors unless you attach them beforehand.

The following code works without issues for me (provided that you have installed ggplot2):

##Vector processing=group
##Field1=Field Layer
##Field2=Field Layer
ggplot(Layer) + geom_point(aes(Layer[[Field1]], Layer[[Field2]]))
# or save them beforehand into a vector: a <- Layer[[Field1]]

One example of a basic tutorial can be found on my blog.

EDIT: If you don't have the library installed, than the r-code will of course produce no output. Try to replace library(ggplot2) with

if (!require(ggplot2)){print("ggplot2 not installed. Will install");install.packages(ggplot2, dependencies = TRUE)}
  • Hi Curlew, I've copied/pasted your code in the R script or Processing but the output is still a little question mark and none plot appears. Maybe I'm missing some libraries?
    – matteo
    Dec 13 '13 at 13:03
  • Maybe you do :) Look at the log output and replace the library(ggplot2) with the following code (edited into answer)
    – Curlew
    Dec 13 '13 at 13:59
  • That's really strange... I use R since month (with ggplot2, rgdal, raster packages installed), but look at the output log (edited in the post)
    – matteo
    Dec 13 '13 at 14:08
  • Another possible explanation could be that your field types are not numerical, but factorial (can even be caused by a NaN somewhere). Type conversion is important and Sextante won't show a seperate error here...
    – Curlew
    Dec 13 '13 at 14:55
  • mmm.. I don't think that's the problem.. No NaN around here.. BTW: the sample Histogram script works (output is created), but if I try to plot two field with the simple plot function, it doesn't work..
    – matteo
    Dec 13 '13 at 17:32

I found out what was wrong. No Layer call inside ggplot, here the right code:

##Vector processing=group
##a=Field Layer
##b=Field Layer

ggplot() + 
geom_point(aes(Layer[[a]], Layer[[b]]))


  • Good that you got it working. The script worked for me however even with the Layer call inside. I am using ggplot2 version O.9.3.1
    – Curlew
    Dec 14 '13 at 18:30
  • Maybe it's a qgis issue (I'm working with the master version, 2.1). Anyway, issue solved.. :)
    – matteo
    Dec 14 '13 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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