Get startedGet started for free

Some useful methods

pop is a RasterLayer object, which contains the population around the Boston and NYC areas. Each grid cell simply contains a count of the number of people that live inside that cell.

You saw in the previous exercise that print() gives a useful summary of the object including the coordinate reference system, the size of the grid (both in number of rows and columns and geographical coordinates), and some basic info on the values stored in the grid. But it was very succinct; what if you want to see some of the values in the object?

The first way is to simply plot() the object. There is a plot() method for raster objects that creates a heatmap of the values.

If you want to extract the values from a raster object you can use the values() function, which pulls out a vector of the values. There are 316,800 values in the pop raster, so you won't want to print them all out, but you can use str() and head() to take a peek.

This exercise is part of the course

Visualizing Geospatial Data in R

View Course

Exercise instructions

  • Call plot() on pop. Can you see where NYC is?
  • Call str() on values(pop).
  • Call head() on values(pop).

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Call plot() on pop


# Call str() on values(pop)


# Call head() on values(pop)
Edit and Run Code