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

The Archive Base Latest Questions

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

I have created a 3d map using rgl.surface(), mainly following Shane’s answer in this

  • 0

I have created a 3d map using rgl.surface(), mainly following Shane’s answer in this post. Using my own data, I get this map

enter image description here

On top of this surface map, I would like to add a map of vegetation density such that I obtain something like this (obtained with the software Surfer):

enter image description here

Is it possible to do this with rgl, or for that matter any other package in r or is the only solution to have two maps like in Shane’s answer?

Thank you.

Edit:

Following @gsk3’s request, here is the code for this map:

library(rgl)

# Read the z (i.e. elevation) dimension from file
z1 = matrix(scan("myfile.txt"),nrow=256, ncol=256, byrow=TRUE)
#create / open x y (i.e. easting and northing coordinates) dimensions 
y=8*(1:ncol(z)) # Each point is 8 m^2
x=8*(1:nrow(z))

# See https://stackoverflow.com/questions/1896419/plotting-a-3d-surface-plot-with-contour-map-overlay-using-r for details of code below
zlim <- range(z)
zlen <- zlim[2] - zlim[1] + 1
colorlut <- terrain.colors(zlen,alpha=0) # height color lookup table
col <- colorlut[ z-zlim[1]+1 ] # assign colors to heights for each point
open3d()
rgl.surface(x,y,z)

I can’t post the elevation code because there are 65536 (i.e. x*y=256*256) points but it is a matrix which looks like this

            [,1]     [,2]     [,3]     [,4]     [,5] 
[1,]    1513.708 1513.971 1514.067 1513.971 1513.875 
[2,]    1513.622 1513.524 1513.578 1513.577 1513.481

and so on.
Same for the vegetation density map, which is exactly the same format and for which I have a single value for each x*y point. I hope this makes things a bit clearer…?

Edit 2, final version

This is the map I have produced with R. I haven’t got the legend on it yet but this is something I’ll do at a later stage.

enter image description here

The final code for this is

library(rgl)
z1 = matrix(scan("myfile.txt"),nrow=256, ncol=256, byrow=TRUE)
# Multiply z by 2 to accentuate the relief otherwise it looks a little bit flat.
z= z1*2

#create / open x y dimensions
y=8*(1:ncol(z))
x=8*(1:nrow(z))

trn = matrix(scan("myfile.txt"),nrow=256, ncol=256, byrow=TRUE)
fv = trn*100
trnlim = range(fv)

fv.colors = colorRampPalette(c("white","tan4","darkseagreen1","chartreuse4")) ## define the color ramp
colorlut =fv.colors(100)c(1,seq(35,35,length.out=9),seq(35,75,length.out=30),seq(75,100,length.out=61))] 

# Assign colors to fv for each point
col = colorlut[fv-trnlim[1]+1 ] 
open3d()
rgl.surface(x,y,z,color=col) 

Thank you very much to @gsk3 and @nullglob in this post for their help. Hope this post will help many others!

  • 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:30:02+00:00Added an answer on May 24, 2026 at 8:30 am

    Modified above code to give an answer. Note that terrain should be a matrix in the same format as the elevation matrix. And I added a ,color argument to your function call so it actually uses the color matrix you created.

    library(rgl)
    
    # Read the z (i.e. elevation) dimension from file
    z1 = matrix(scan("myfile.txt"),nrow=256, ncol=256, byrow=TRUE)
    #create / open x y (i.e. easting and northing coordinates) dimensions 
    y=8*(1:ncol(z)) # Each point is 8 m^2
    x=8*(1:nrow(z))
    
    # Read the terrain types from a file
    trn = matrix(scan("terrain.txt"),nrow=256, ncol=256, byrow=TRUE)
    
    # See http://stackoverflow.com/questions/1896419/plotting-a-3d-surface-plot-with-contour-map-overlay-using-r for details of code below
    trnlim <- range(trn)
    trnlen <- trnlim[2] - trnlim[1] + 1
    colorlut <- terrain.colors(trnlen,alpha=0) # height color lookup table
    col <- colorlut[ trn-trnlim[1]+1 ] # assign colors to heights for each point
    open3d()
    rgl.surface(x,y,z,color=col)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a site-map using the following HTML: <ul class=main-menu> <li> <div> <a
I am using the jquery-ui-map plugin (http://code.google.com/p/jquery-ui-map/). I have successfully created a map using
So I have created a nested template (a map of vectors) using stanford's CS106
I have created a div with a image I'm using with Image Map. <div
I have created a Map as follows: private Map<String, List<Client>> clientCatalogue; this.clientCatalogue = new
I have created Map application for iPhone using xcode 3.2 and sdk 4.3.2. I
I am new to iphone development.I have created map applications and displayed the cuurent
I have an ArcGIS map created with Flex. The labels created dynamically are the
I have created a custom dialog for Visual Studio Setup Project using the steps
I have created a C# class file by using a XSD-file as an input.

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.