0
votes

I'm trying to add a second set of data, as geographic points, to a ggmap plot. I already have bathymetric/topographic data applied to the map using geom_contour and geom_raster functions. But every time I add the function geom_point, I get this message: "Discrete value supplied to continuous scale"

This is what my geographic coordinates data frame (Coords) looks like:

Long      Lat                             Type
1  155.5910 19.93401               Not geo-referenced
2  155.4998 19.83302               Not geo-referenced
3  155.2000 19.52440               Not geo-referenced
4  155.6302 20.00934               Geo-referenced
5  155.6623 19.81197               Not geo-referenced
6  155.5619 19.88102               Geo-referenced

How I obtained the bathymetric data (NOT relevant to this question)

bathydata <- getNOAA.bathy(lon1 = -180, lon2 = -154.5, lat1 = 29.3, lat2 =7, resolution = 7, antimeridian=FALSE)
bathydata <- as.matrix(bathydata)
class(bathydata) <- "matrix"
result1<- as.data.frame(bathydata)
result2<- rownames_to_column(result1, var = "lon")
result3<- gather(result2, lat, value, -1)
finalBathy<- mutate_all(result3, list(as.numeric))

This is what my bathymetric data frame (finalBathy) looks like:

lon       lat value
1     -179.9417  7.058333 -5697
2     -179.8252  7.058333 -5582
3     -179.7088  7.058333 -5322
4     -179.5924  7.058333 -5708
5     -179.4759  7.058333 -5791
6     -179.3595  7.058333 -5781

My code

baseArchipelago = get_map(location = c(-180, 7, -154.5, 29.3), zoom = 6, maptype = "terrain")

mapArchipelago <- ggmap(baseArchipelago) 

MapRaster <- mapArchipelago 
            + geom_raster(data = finalBathy, aes(x = lon, y = lat, fill = value)) 
            + geom_contour(data = finalBathy, aes(x = lon, y = lat, z = value),
                bins = 8, colour = "darkslategray", lwd=0.25) 
            + geom_point(data=Coords, aes(x = -Long, y = Lat, fill = Type, shape = Type), color = "black",
                cex = 1.2, show.legend = FALSE) 
            + scale_shape_manual(values = c(23, 23), labels = c("Point 1", "Point2"), name = NULL) 


MapFinal <-   MapRaster + coord_cartesian()

((Note: without coord_cartesian(), I get the error message: "Error: geom_raster only works with Cartesian coordinates"))

If I remove fill=Type from the aes argument in geom_point, the problem is solved and the points get mapped correctly: Without fill=Type But I need the points to be filled by their Type!

And if I add scale_fill_manual(values=c("red", "blue"), labels=c("Point 1", "Point 2"), name=NULL) to the code, the points go away and I get the same message again: "Discrete value supplied to continuous scale"

1
Looks like your fill aesthetic is already mapped to a continuous value in geom_raster. Why not map your aesthetic using color instead of fill in geom_point?Z.Lin
So once fill is mapped to one function, it can't be applied to another? I thought, since geom_raster and geom_point use different data sets, then they would be applying the fill to different points? (Changing from fill to color did not work)Joseph
You can if the mapped variables are of the same type. But from what you've posted, it seems like one is continuous and the other is categorical.Z.Lin
Is there a way to separate them so that one fill is mapped to the bathymetric data and a different fill is mapped to the geographic points?Joseph

1 Answers

0
votes

You may add additional discret scales to represent the discrete variables:

MapRaster <- mapArchipelago +
            geom_raster(data = finalBathy, aes(x = lon, y = lat, fill = value)) +
            geom_contour(data = finalBathy, aes(x = lon, y = lat, z = value), 
                bins = 8, colour = "darkslategray", lwd=0.25) +         
            geom_point(data=Coords, aes(x = -Long, y = Lat, color = Type, shape = ype), 
                cex = 1.2, show.legend = FALSE) +
            scale_discrete_manual(aesthetics = c("colour"),
                values = c("red", "yellow"), 
                labels = c("Point 1","Point2"), 
                name = NULL) +
            scale_discrete_manual(aesthetics = c("shape"),
                values = 16:17, 
                labels = c("Point 1","Point2"), 
                name = NULL) 

Which gives

enter image description here

Does it answer your question?