Search code examples
rggplot2direct-labels

Error: No Default label placement from this type of ggplot


Why am I getting this error? Here is the R script and the data set I am trying to plot salinity contours for (link to data here): It looks like this (first 10 rows):

    Longitude   Latitude    Salinity
1   -76.7027969 35.8915787  0.094745182
2   -76.67744141    35.8915787  0.10131969
3   -76.65208591    35.8915787  0.109281363
4   -76.62673042    35.8915787  0.118873653
5   -76.60137493    35.8915787  0.130326379
6   -76.57601943    35.8915787  0.143826847
7   -76.55066394    35.8915787  0.159496078
8   -76.52530845    35.8915787  0.177391073
9   -76.49995296    35.8915787  0.197562864
10  -76.47459746    35.8915787  0.220200511

ASsalinity <- read.csv("~/ASsalinity.csv")
library("ggplot2")
library("directlabels")
plot1<-ggplot(ASsalinity)+geom_raster(aes(Longitude,Latitude,fill = Salinity),data=ASsalinity)+geom_contour(aes(Longitude,Latitude,z=Salinity),data=ASsalinity)
plot1

I can get the contours plotted on the ggmap, A plot of the salinity contours in Albemarle Sound, NC. Longitude (x) and Latitude (y), and an interpolation program was used to generate the salinity at all other points on a grid.

but the direct.label command is throwing this error:

direct.label (plot2)

If the defaults are used:

Error in (function (geom, p, L, colvar, ...)  : 
  No default label placement for this type of ggplot

If I specify a position placement:

direct.label (plot2, "bottom.pieces")

I get a different error:

'ggproto' is not an exported object from 'namespace:ggplot2'

What am I doing wrong?
Thanks in advance.


Solution

  • So I discovered the answer to my own question, with the help of aosmith. The key was upgrading to ggplot2 2.1.0 and then using stat_contour(). Below, I updated my code, while adding a base map with ggmap 2.6.1:

    library("ggmap")
    library("ggplot2")
    library("directlabels")
     ASmap3<-get_map(location= c(lon= -76.16,lat=36.08),zoom=9)
    plot1<-ggmap(ASmap3)+stat_contour(aes(Longitude,Latitude,z=Salinity,colour=..level..),data=ASsalinity)
     direct.label(plot1,"bottom.pieces")
    

    Here is the map: A map of Albemarle Sound, NC, USA with labeled salinity contours