Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145147
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:26:29+00:00 2026-06-17T10:26:29+00:00

I am trying to use the function fortify in R to plot the map.

  • 0

I am trying to use the function fortify in R to plot the map. But I am always getting an error while processing the data. I read the shape file using the following code:

shape <- readShapeSpatial("Lond_City/lond_city.shp")

The shapefile can be found on the following dropbox location:
https://www.dropbox.com/sh/d4w6saailydtu1r/5oIa56xV6S

Then I tried to use the following command :

shape1 <- fortify(shape,region="data")

In the above code, what should I put in Place of “data”. This is the place which seems confusing. I can open the data or shapefile in R. However, when I run the fortify line I get the following error:

Error in ‘[.data.frame'(attr, , region) : undefined columns selected

Can anybody tell me how to properly fortify a shape file with example ? The reason I want to use fortify is that I want to combine the shape file with the data points.

Thank you so much.

Jdbaba

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:26:31+00:00Added an answer on June 17, 2026 at 10:26 am

    Your problem is that you need to give fortify a column that actually exists. If you type str(lon.shape) from my example code below, you will see that there is no region column:

    > str(lon.shape)
    Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
      ..@ data       :'data.frame': 1 obs. of  7 variables:
      .. ..$ ons_label: chr "00AA"
      .. ..$ name     : chr "City of London"
      .. ..$ label    : chr "02AA"
      .. ..$ X_max    : num 533843
      .. ..$ y_max    : num 182198
      .. ..$ X_min    : num 0
      .. ..$ y_min    : num 0
      ..@ polygons   :List of 1
      .. ..$ :Formal class 'Polygons' [package "sp"] with 5 slots
      .. .. .. ..@ Polygons :List of 1
      .. .. .. .. ..$ :Formal class 'Polygon' [package "sp"] with 5 slots
      .. .. .. .. .. .. ..@ labpt  : num [1:2] 532464 181220
      .. .. .. .. .. .. ..@ area   : num 3151465
      .. .. .. .. .. .. ..@ hole   : logi FALSE
      .. .. .. .. .. .. ..@ ringDir: int 1
      .. .. .. .. .. .. ..@ coords : num [1:771, 1:2] 531027 531029 531036 531074 531107 ...
      .. .. .. ..@ plotOrder: int 1
      .. .. .. ..@ labpt    : num [1:2] 532464 181220
      .. .. .. ..@ ID       : chr "0"
      .. .. .. ..@ area     : num 3151465
      ..@ plotOrder  : int 1
      ..@ bbox       : num [1:2, 1:2] 530967 180404 533843 182198
      .. ..- attr(*, "dimnames")=List of 2
      .. .. ..$ : chr [1:2] "x" "y"
      .. .. ..$ : chr [1:2] "min" "max"
      ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slots
      .. .. ..@ projargs: chr "+proj=tmerc +lat_0=49 +lon_0=-2 +k=0.9996012717 +x_0=400000 +y_0=-100000 +ellps=airy +units=m +no_defs" 
    

    Instead, try using name, like this:

    lon.df <- fortify(lon.shape, region = "name")
    

    Which produces this data frame:

    > head(lon.df)
          long      lat order  hole piece            group             id
    1 531026.9 181611.1     1 FALSE     1 City of London.1 City of London
    2 531028.5 181611.2     2 FALSE     1 City of London.1 City of London
    3 531036.1 181611.5     3 FALSE     1 City of London.1 City of London
    4 531074.0 181610.3     4 FALSE     1 City of London.1 City of London
    5 531107.0 181609.3     5 FALSE     1 City of London.1 City of London
    6 531117.1 181608.9     6 FALSE     1 City of London.1 City of London
    

    Here’s one way to go about it, start to finish:

    library(rgdal)
    library(ggplot2)
    library(rgeos)
    
    shape.dir <- "c:/test/london" # use your directory name here
    
    lon.shape <- readOGR(shape.dir, layer = "lond_city")
    lon.df <- fortify(lon.shape, region = "name")
    
    ggplot(lon.df, aes(x = long, y = lat, group = group)) +
        geom_polygon(colour = "black", fill = "grey80", size = 1) +
        theme()
    

    …resulting in the following:

    screenshot

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use function with foreach but getting error. Here is my code:
I'm trying to use the function regmatches, in R, but i'm getting an error
I am trying to use a function pointer and I am getting this error:
Im trying to use this function but getting some errors: public string GetShortcutTargetFile(string shortcutFilename)
I am trying to use onGUIInit function on my installer script but while compiling
I was trying to use the function glMultiDrawElements while studying OpenGL (using red book)
I'm trying to use the function [context].startSubActivity([intent i]) but that won't compile. Apparently startSubActivity()
I'm trying to use a function with PostgreSQL to save some data. Here is
I am trying to use this function to pull data from Foursquare through yahoo
I want to delete file from my program. Trying to use this function but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.