-1

He all! I'm new in R world and I think that this will be a stupid error:
I would like to project a raster representing my "study area" adding observations take from rgbif.

First, I've take specie observations in the "study area" using a polygon:

wkt <- 'POLYGON((14.022120 41.583456,13.928857 41.610301,13.607180 41.789949,13.645924 42.021854,13.779864 42.029311,14.035472 41.823996,14.066175 41.751961,14.057895 41.614899,14.022120 41.583456))'

Picus_viridis <- occ_data(scientificName = "Picus viridis", geometry = wkt, return = "data", limit = 1999)
Picus_viridis<-Picus_viridis$data

Second, I've upload the raster, cropped it to the extension of interest and assigned a projection:

elev <- raster("data/gtopo30/gtopo30.tif")
elev_park <- crop(elev, extent(13.398953, 14.235655, 41.402820, 42.064252))

projection(elev_park) <- "+proj=longlat +datum=WGS84 +ellps=WGS84 +towgs84=0,0,0"

Than I've tried to plot the raster with the observation points using ggplot but R said me the there is an error:

ggplot() +
  geom_raster(elev_park, aes(x = x, y = y)) +
  geom_point(aes(x = Picus_viridis$decimalLongitude, y = Picus_viridis$decimalLatitude))

Errore: mapping must be created by aes()

Does anyone know were the problem is?


raster GTOPO30:

> elev_park
class      : RasterLayer 
dimensions : 84, 112, 9408  (nrow, ncol, ncell)
resolution : 0.008333334, 0.008333334  (x, y)
extent     : 13.30001, 14.23334, 41.40001, 42.10001  (xmin, xmax, ymin, ymax)
crs        : NA 
source     : memory
names      : gtopo30 
values     : 16, 2689  (min, max)
Leonardo
  • 2,439
  • 33
  • 17
  • 31
Bacone
  • 1
  • it will be easier to help if you provide a reproducible example https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – desval May 05 '20 at 10:47
  • 1
    It looks as if you might be missing the `data` argument to `ggplot`? – Peter May 05 '20 at 12:04
  • also, mapping should be the 1st argument int the geom_raster. – dww May 09 '20 at 11:35

1 Answers1

1

Without your dataset, I cannot confirm if this will all work, but the general structure of ggplot usage is:

  • In your ggplot() call, you should supply the dataset and the mapping. You don't have to do this, but it's generally a good idea unless you're using pipe commands (%>%) or something similar. Your base mapping= is almost always provided there too. Think of this function as "creating your basic plot without the data at all".

  • All geom_ functions (to my knowledge) have an argument setup that starts with mapping=, then data=, then other arguments. This means if you do not explicitly declare the structure, the first argument will be applied to mapping=. This is why your geom_raster(elev_park,...) is going to throw an error.

  • It's generally best practice to send the geoms data as a dataset and mappings as the names of your columns--not naming the vector. So, instead of x=my_dataset$column1, you would set it up as data=my_dataset and mapping=aes(x=column1...).

With that being said, my suggested revision that should give you something is below. This assumes that elev_park$x and elev_park$y exist. If not, replace x= and y= mapping with the correct column name (unquoted).

ggplot(elev_park, aes(x=x, y=y)) +
  geom_raster() +     # mapping not needed.  It's passed from ggplot()
  geom_point(         # should be explicit when setting mapping and data
    data=Picus_viridis,
    mapping=aes(x=decimalLongitude, y =decimalLatitude)
  )
chemdork123
  • 12,369
  • 2
  • 16
  • 32