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 8791169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:48:22+00:00 2026-06-13T22:48:22+00:00

I am using the example here for discussion: ggplot map with l library(rgdal) library(ggplot2)

  • 0

I am using the example here for discussion:
ggplot map with l

library(rgdal)
library(ggplot2)
library(maptools)

# Data from http://thematicmapping.org/downloads/world_borders.php.
# Direct link: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip
# Unpack and put the files in a dir 'data'

gpclibPermit()
world.map <- readOGR(dsn="data", layer="TM_WORLD_BORDERS_SIMPL-0.3")
world.ggmap <- fortify(world.map, region = "NAME")

n <- length(unique(world.ggmap$id))
df <- data.frame(id = unique(world.ggmap$id),
                 growth = 4*runif(n),
                 category = factor(sample(1:5, n, replace=T)))

## noise
df[c(sample(1:100,40)),c("growth", "category")] <- NA


ggplot(df, aes(map_id = id)) +
     geom_map(aes(fill = growth, color = category), map =world.ggmap) +
     expand_limits(x = world.ggmap$long, y = world.ggmap$lat) +
     scale_fill_gradient(low = "red", high = "blue", guide = "colorbar")

Gives the following results:
enter image description here

I would like to map one variable to the left “half” of a country and a different variable to the right “half” of the country. I put “half” in quotes because it’s not clearly defined (or at least I’m not clearly defining it). The answer by Ian Fellows might help (which gives an easy way to get the centroid). I’m hoping for something so that I can do aes(left_half_color = growth, right_half_color = category) in the example. I’m also interested in top half and bottom half if that is different.

If possible, I would also like to map the individual centroids of the halves to something.

  • 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-13T22:48:23+00:00Added an answer on June 13, 2026 at 10:48 pm

    This is a solution without ggplot that relies on the plot function instead. It also requires the rgeos package in addition to the code in the OP:

    EDIT Now with 10% less visual pain

    EDIT 2 Now with centroids for east and west halves

    library(rgeos)
    library(RColorBrewer)
    
    # Get centroids of countries
    theCents <- coordinates(world.map)
    
    # extract the polygons objects
    pl <- slot(world.map, "polygons")
    
    # Create square polygons that cover the east (left) half of each country's bbox
    lpolys <- lapply(seq_along(pl), function(x) {
      lbox <- bbox(pl[[x]])
      lbox[1, 2] <- theCents[x, 1]
      Polygon(expand.grid(lbox[1,], lbox[2,])[c(1,3,4,2,1),])
    })
    
    # Slightly different data handling
    wmRN <- row.names(world.map)
    
    n <- nrow(world.map@data)
    world.map@data[, c("growth", "category")] <- list(growth = 4*runif(n),
                     category = factor(sample(1:5, n, replace=TRUE)))
    
    # Determine the intersection of each country with the respective "left polygon"
    lPolys <- lapply(seq_along(lpolys), function(x) {
      curLPol <- SpatialPolygons(list(Polygons(lpolys[x], wmRN[x])),
        proj4string=CRS(proj4string(world.map)))
      curPl <- SpatialPolygons(pl[x], proj4string=CRS(proj4string(world.map)))
      theInt <- gIntersection(curLPol, curPl, id = wmRN[x])
      theInt
    })
    
    # Create a SpatialPolygonDataFrame of the intersections
    lSPDF <- SpatialPolygonsDataFrame(SpatialPolygons(unlist(lapply(lPolys,
      slot, "polygons")), proj4string = CRS(proj4string(world.map))),
      world.map@data)
    
    ##########
    ## EDIT ##
    ##########
    # Create a slightly less harsh color set
    s_growth <- scale(world.map@data$growth,
      center = min(world.map@data$growth), scale = max(world.map@data$growth))
    growthRGB <- colorRamp(c("red", "blue"))(s_growth)
    growthCols <- apply(growthRGB, 1, function(x) rgb(x[1], x[2], x[3],
      maxColorValue = 255))
    catCols <- brewer.pal(nlevels(lSPDF@data$category), "Pastel2")
    
    # and plot
    plot(world.map, col = growthCols, bg = "grey90")
    
    plot(lSPDF, col = catCols[lSPDF@data$category], add = TRUE)
    

    enter image description here

    Perhaps someone can come up with a good solution using ggplot2. However, based on this answer to a question about multiple fill scales for a single graph (“You can’t”), a ggplot2 solution seems unlikely without faceting (which might be a good approach, as suggested in the comments above).


    EDIT re: mapping centroids of the halves to something: The centroids for the east (“left”) halves can be obtained by

    coordinates(lSPDF)
    

    Those for the west (“right”) halves can be obtained by creating an rSPDF object in a similar way:

    # Create square polygons that cover west (right) half of each country's bbox
    rpolys <- lapply(seq_along(pl), function(x) {
      rbox <- bbox(pl[[x]])
      rbox[1, 1] <- theCents[x, 1]
      Polygon(expand.grid(rbox[1,], rbox[2,])[c(1,3,4,2,1),])
    })
    
    # Determine the intersection of each country with the respective "right polygon"
    rPolys <- lapply(seq_along(rpolys), function(x) {
      curRPol <- SpatialPolygons(list(Polygons(rpolys[x], wmRN[x])),
        proj4string=CRS(proj4string(world.map)))
      curPl <- SpatialPolygons(pl[x], proj4string=CRS(proj4string(world.map)))
      theInt <- gIntersection(curRPol, curPl, id = wmRN[x])
      theInt
    })
    
    # Create a SpatialPolygonDataFrame of the western (right) intersections
    rSPDF <- SpatialPolygonsDataFrame(SpatialPolygons(unlist(lapply(rPolys,
      slot, "polygons")), proj4string = CRS(proj4string(world.map))),
      world.map@data)
    

    Then information could be plotted on the map according to the centroids of lSPDF or rSPDF:

    points(coordinates(rSPDF), col = factor(rSPDF@data$REGION))
    # or
    text(coordinates(lSPDF), labels = lSPDF@data$FIPS, cex = .7)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the example found here: http://en.wikibooks.org/wiki/OpenGL_Programming/Modern_OpenGL_Tutorial_Text_Rendering_01 The issue is that when I specify
I follow example from here using section listview http://lalit3686.blogspot.com/2012/05/sectionadapter.html But how can I implement
I'm using the SSLStream example from msdn here . The client code seems to
Example: here is the string: blablabla123:550:404:487blablabla500:488:474:401blablablabla here is what I'm using: string reg =
I found an example here to create a select list with optgroups using KnockoutJS.
I am learning and trying simple example using node.js and mongoskin. here is my
I'm so close! I'm using the XNA Game State Management example found here and
Here is an (artificial) example of using a function that returns an anonymous struct
I am developing a application using PHP. Some example code is here. $url =
Just using this as an example... Here are the columns in my UserProfile table:

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.