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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:46:18+00:00 2026-05-24T08:46:18+00:00

How can I improve the legend of spatial raster map plot using ggplot when

  • 0

How can I improve the legend of spatial raster map plot using ggplot when compared to a spplot() legend?

I would like plot spatial maps using ggplot() instead of ssplot() however there are a few things that I would like to improve when compared to the spplot:

  1. create a ggplot legend that goes from small (bottom) to large values (top)
  2. Have the breaks in the ggplot legend similar to the ssplot() legend so that I know what the boundaries are of each color.

## load packages
require(raster)
require(ggplot2)
require(rgdal)
require(RColorBrewer)
set.seed(1)

r <- raster(xmn=-110, xmx=-90, ymn=40, ymx=60, ncols=40, nrows=40,
          crs="+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100
+ellps=WGS84")
r <- setValues(r,matrix(rnorm(1600, mean=0.4,sd=0.2))) 

## 1. spatial map with spplot
cuts <-seq(minValue(r),maxValue(r),length.out=8)
cuts = round(cuts,digits=2)
col.regions = brewer.pal(length(cuts)+3-1, "RdYlGn")
print( 
spplot(as(r, 'SpatialGridDataFrame'),at=cuts,
col.regions=col.regions,
colorkey=list(labels=list(at=cuts),at=cuts), pretty=TRUE,
scales=list(draw=T)
) 
)

## 2. spatial map with ggplot
p = rasterToPoints(r); df = data.frame(p)
colnames(df) = c("x", "y", "NDVI")

p  <- ggplot(data=df) + geom_tile(aes(x, y, fill=NDVI)) +
coord_equal() + labs(x=NULL, y=NULL) + 
scale_fill_gradient2(low="red", mid="yellow",high="green",
limits=c(minValue(r),maxValue(r)), midpoint = 0.4) + theme_bw() +
scale_x_continuous(expand=c(0,0)) + scale_y_continuous(expand=c(0,0))
print(p)

ssplot() result
ssplot

ggplot() result
ggplot

  • 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-05-24T08:46:19+00:00Added an answer on May 24, 2026 at 8:46 am

    thanks @joran for the pointer to that.

    here is an example code and output using the dev version:

    br <- seq(min(df$NDVI), max(df$NDVI), len=8)
    
    ggplot(data=df) + 
      geom_tile(aes(x, y, fill=NDVI)) + 
      scale_fill_gradient(low="red", high="green", 
        breaks=br, labels=sprintf("%.02f", br), 
        guide=guide_colorbar(title=NULL, nbin=100, barheight=unit(0.75, "npc"), label.hjust=1)) + 
      scale_x_continuous(expand=c(0,0)) + 
      scale_y_continuous(expand=c(0,0))
    

    enter image description here

    you can probably try this by:

    # from Hadley's instruction
    install.packages("devtools")
    library(devtools)
    dev_mode() # to avoid interfering with your existing install
    install_github("ggplot2", username="kohske", branch = "feature/new-guides-with-gtable")
    library(ggplot2)
    

    UPDATED:

    here is the instruction for the installation from scratch:

    install.packages(
      c('devtools', 'digest', 'memoise', 'plyr', 'reshape2', 'RColorBrewer', 'stringr', 'dichromat', 'munsell', 'plyr', 'colorspace'), 
      dep=TRUE)
    
    library(devtools)
    dev_mode()
    
    install_github("scales")
    install_github("ggplot2", username="kohske", branch = "feature/new-guides-with-gtable")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know how I can improve my development environment. At the
I'm wondering about some ideas which can improve process of designing solutions using Access
I am interested in seeing if I can improve the way we use NUnit
I have an idea of how I can improve the performance with dynamic code
I'm looking for any way that I can improve the selector performance of a
How can I improve the look and feel of my Linux desktop to suit
How can I improve this username/password checking? [AcceptVerbs(HttpVerbs.Post)] public ActionResult Login(FormCollection collection) { var
How can I improve this code? What has made this long winded is the
I wonder if anyone can help improve my understanding of JOINs in SQL. [If
What are some things I can do to improve query performance of an oracle

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.