Skip to content

How to show the color bar legend in R?

13 messages · Ferra Xu, Zack Holden, Robert J. Hijmans +4 more

#
Based on the dataset, I computed a 3D kernel (longitude, latitude, depth) and displayed the magnitude with the sphere radius and time with color as follows. I am wondering how I can show the color bar legend.?

Also I have problem with the scale of the graph. I need to see if the balls touch or overlap each other from the graph but since the scales are not real I cannot recognize it. How can I correct it??

library(ks)?
x <- read.csv("HK1.csv")?
y <- x[,1:3]?
fhat1 <- kde(x=y)?
n <- nrow(x)?
col <- grey((n:1)/n)?
plot(fhat1, xlim=c(0,500), ylim=c(0,250), zlim=c(0, 4.4))?
spheres3d(y, radius=15*(x$magnitude), alpha=1, col= col)?

I tried this code (which didn't work):?

legend_image <- as.raster(matrix(grey((n:1)/n), ncol=1))?
plot(c(0,2),c(0,200),type = 'n', axes = F, xlab = '', ylab = '', main = ?'time scale')?
text(x=1.5, y = seq(0,200,l=5), labels = seq(0,200,l=5))?
rasterImage(legend_image, 0, 0, 1, 1)?

And this one in "plotrix" package:?

testcol<- col?
col.labels<- seq(0,200, l=5)?
color.legend(600, 0, 601,200,col.labels,testcol,gradient="y")?

But it gives me this error:?

Error in rect(xleft, ybottoms, xright, ytops, col = col, lty = 0) :?
plot.new has not been called yet?

Also I used legend() in Graphics library:?

legend("right", legend=seq(0,200,l=20), fill= colfunc(20), title="Time Scale")?

But it gives me the legend in a separate window.?Here is my data:?

?longitude ? ? ? ? latitude ? ? ? ?depth ? ? ? ? ? ? time ? ? ? ? ?magnitude?

422.697323 ?164.19886 ? 2.457419 ? ?8.083796636 ?0.83367586?
423.008236 ?163.32434 ? 0.5551326 ? 37.58477455 ?0.893893903?
204.733908 ?218.36365 ? 1.9397874 ? 37.88324312 ?0.912809449?
203.963056 ?218.4808 ? ?0.3723791 ? 43.21775903 ?0.926406005?
100.727581 ?46.60876 ? ?1.4022341 ? 49.41510519 ?0.782807523?
453.335182 ?244.25521 ? 1.6292517 ? 51.73779175 ?0.903910803?
134.909462 ?210.96333 ? 2.2389119 ? 53.13433521 ?0.896529401?
135.300562 ?212.02055 ? 0.6739541 ? 67.55073745 ?0.748783521?
258.237117 ?134.29735 ? 2.1205291 ? 76.34032587 ?0.735699304?
341.305271 ?149.26953 ? 3.718958 ? ?94.33975483 ?0.849509216?
307.138925 ?59.60571 ? ?0.6311074 ? 106.9636715 ?0.987923188?
307.76875 ? 58.91453 ? ?2.6496741 ? 113.8515307 ?0.802115718?
415.025535 ?217.17398 ? 1.7155688 ? 115.7464603 ?0.875580325?
2 days later
#
I am still struggling with this question. I appreciate any help from you!
On Friday, January 23, 2015 11:00 AM, Ferra Xu <ferra.xu at yahoo.com> wrote:
Based on the dataset, I computed a 3D kernel (longitude, latitude, depth) and displayed the magnitude with the sphere radius and time with color as follows. I am wondering how I can show the color bar legend.?

Also I have problem with the scale of the graph. I need to see if the balls touch or overlap each other from the graph but since the scales are not real I cannot recognize it. How can I correct it??

library(ks)?
x <- read.csv("HK1.csv")?
y <- x[,1:3]?
fhat1 <- kde(x=y)?
n <- nrow(x)?
col <- grey((n:1)/n)?
plot(fhat1, xlim=c(0,500), ylim=c(0,250), zlim=c(0, 4.4))?
spheres3d(y, radius=15*(x$magnitude), alpha=1, col= col)?

I tried this code (which didn't work):?

legend_image <- as.raster(matrix(grey((n:1)/n), ncol=1))?
plot(c(0,2),c(0,200),type = 'n', axes = F, xlab = '', ylab = '', main = ?'time scale')?
text(x=1.5, y = seq(0,200,l=5), labels = seq(0,200,l=5))?
rasterImage(legend_image, 0, 0, 1, 1)?

And this one in "plotrix" package:?

testcol<- col?
col.labels<- seq(0,200, l=5)?
color.legend(600, 0, 601,200,col.labels,testcol,gradient="y")?

But it gives me this error:?

Error in rect(xleft, ybottoms, xright, ytops, col = col, lty = 0) :?
plot.new has not been called yet?

Also I used legend() in Graphics library:?

legend("right", legend=seq(0,200,l=20), fill= colfunc(20), title="Time Scale")?

But it gives me the legend in a separate window.?Here is my data:?

?longitude ? ? ? ? latitude ? ? ? ?depth ? ? ? ? ? ? time ? ? ? ? ?magnitude?

422.697323 ?164.19886 ? 2.457419 ? ?8.083796636 ?0.83367586?
423.008236 ?163.32434 ? 0.5551326 ? 37.58477455 ?0.893893903?
204.733908 ?218.36365 ? 1.9397874 ? 37.88324312 ?0.912809449?
203.963056 ?218.4808 ? ?0.3723791 ? 43.21775903 ?0.926406005?
100.727581 ?46.60876 ? ?1.4022341 ? 49.41510519 ?0.782807523?
453.335182 ?244.25521 ? 1.6292517 ? 51.73779175 ?0.903910803?
134.909462 ?210.96333 ? 2.2389119 ? 53.13433521 ?0.896529401?
135.300562 ?212.02055 ? 0.6739541 ? 67.55073745 ?0.748783521?
258.237117 ?134.29735 ? 2.1205291 ? 76.34032587 ?0.735699304?
341.305271 ?149.26953 ? 3.718958 ? ?94.33975483 ?0.849509216?
307.138925 ?59.60571 ? ?0.6311074 ? 106.9636715 ?0.987923188?
307.76875 ? 58.91453 ? ?2.6496741 ? 113.8515307 ?0.802115718?
415.025535 ?217.17398 ? 1.7155688 ? 115.7464603 ?0.875580325?
#
Hi Ferra,
Without reproducible code, it's difficult to help. You might have a look at
image.plot() in the fields library, and image()

I'm not sure if this is exactly what you're looking for, but here is a
quick example of one way to control the image and legend in a plot, using
the layout command. You can create a small panel below your main plot and
then add your legend there with more control.

I hope this is helpful.

###########################################
# example code for image plot of volcano data
require(fields)
require(raster)

dat <- raster(volcano)


zc <- as.matrix(seq(minValue(dat), maxValue(dat), length.out=100))

mycolor1 <- two.colors(n=100, start="darkblue", end="red",
middle="yellow1", alpha=.9)

layout(matrix(c(1,2), 2, 1, byrow = TRUE), heights=c(4,1), widths=c(4) )
layout.show(2)
image(dat, axes=F, col=mycolor1, xlab="", ylab="")

par(mar=c(2,4,.1,2))
image(zc, col=mycolor1, axes=F, xlab="", ylab="" )

axis(1, at=seq(0,1, length.out=5),  labels=round(seq(minValue(dat),
maxValue(dat),length.out=5), digits=0), las=1,  tcl=-.15, padj=-1, cex=1.5)
# every .1 corr
# end code
#############################################



On Mon, Jan 26, 2015 at 10:36 AM, Ferra Xu via R-sig-Geo <
r-sig-geo at r-project.org> wrote:

            

  
  
#
Thank you for your email Zack. When I run the code that you provided, it gives me this error:
Error in if (axes) { : argument is not interpretable as logicalIn addition: Warning message:In if (axes) { :? the condition has length > 1 and only the first element will be used
It seems there is a problem with my R !
On Monday, January 26, 2015 11:02 AM, Zack Holden <zaholdenfs at gmail.com> wrote:
Hi Ferra,Without reproducible code, it's difficult to help. You might have a look at image.plot() in the fields library, and image()
I'm not sure if this is exactly what you're looking for, but here is a quick example of one way to control the image and legend in a plot, using the layout command. You can create a small panel below your main plot and then add your legend there with more control.?
I hope this is helpful.?
############################################ example code for image plot of volcano datarequire(fields)require(raster)
dat <- raster(volcano)

zc <- as.matrix(seq(minValue(dat), maxValue(dat), length.out=100))
mycolor1 <- two.colors(n=100, start="darkblue", end="red", middle="yellow1", alpha=.9)
layout(matrix(c(1,2), 2, 1, byrow = TRUE), heights=c(4,1), widths=c(4) )layout.show(2)image(dat, axes=F, col=mycolor1, xlab="", ylab="")
par(mar=c(2,4,.1,2))image(zc, col=mycolor1, axes=F, xlab="", ylab="" )
axis(1, at=seq(0,1, length.out=5), ?labels=round(seq(minValue(dat), maxValue(dat),length.out=5), digits=0), las=1, ?tcl=-.15, padj=-1, cex=1.5) # every .1 corr# end code#############################################
On Mon, Jan 26, 2015 at 10:36 AM, Ferra Xu via R-sig-Geo <r-sig-geo at r-project.org> wrote:
I am still struggling with this question. I appreciate any help from you!
? ? ?On Friday, January 23, 2015 11:00 AM, Ferra Xu <ferra.xu at yahoo.com> wrote:
?Based on the dataset, I computed a 3D kernel (longitude, latitude, depth) and displayed the magnitude with the sphere radius and time with color as follows. I am wondering how I can show the color bar legend.?

Also I have problem with the scale of the graph. I need to see if the balls touch or overlap each other from the graph but since the scales are not real I cannot recognize it. How can I correct it??

library(ks)?
x <- read.csv("HK1.csv")?
y <- x[,1:3]?
fhat1 <- kde(x=y)?
n <- nrow(x)?
col <- grey((n:1)/n)?
plot(fhat1, xlim=c(0,500), ylim=c(0,250), zlim=c(0, 4.4))?
spheres3d(y, radius=15*(x$magnitude), alpha=1, col= col)?

I tried this code (which didn't work):?

legend_image <- as.raster(matrix(grey((n:1)/n), ncol=1))?
plot(c(0,2),c(0,200),type = 'n', axes = F, xlab = '', ylab = '', main = ?'time scale')?
text(x=1.5, y = seq(0,200,l=5), labels = seq(0,200,l=5))?
rasterImage(legend_image, 0, 0, 1, 1)?

And this one in "plotrix" package:?

testcol<- col?
col.labels<- seq(0,200, l=5)?
color.legend(600, 0, 601,200,col.labels,testcol,gradient="y")?

But it gives me this error:?

Error in rect(xleft, ybottoms, xright, ytops, col = col, lty = 0) :?
plot.new has not been called yet?

Also I used legend() in Graphics library:?

legend("right", legend=seq(0,200,l=20), fill= colfunc(20), title="Time Scale")?

But it gives me the legend in a separate window.?Here is my data:?

?longitude ? ? ? ? latitude ? ? ? ?depth ? ? ? ? ? ? time ? ? ? ? ?magnitude?

422.697323 ?164.19886 ? 2.457419 ? ?8.083796636 ?0.83367586?
423.008236 ?163.32434 ? 0.5551326 ? 37.58477455 ?0.893893903?
204.733908 ?218.36365 ? 1.9397874 ? 37.88324312 ?0.912809449?
203.963056 ?218.4808 ? ?0.3723791 ? 43.21775903 ?0.926406005?
100.727581 ?46.60876 ? ?1.4022341 ? 49.41510519 ?0.782807523?
453.335182 ?244.25521 ? 1.6292517 ? 51.73779175 ?0.903910803?
134.909462 ?210.96333 ? 2.2389119 ? 53.13433521 ?0.896529401?
135.300562 ?212.02055 ? 0.6739541 ? 67.55073745 ?0.748783521?
258.237117 ?134.29735 ? 2.1205291 ? 76.34032587 ?0.735699304?
341.305271 ?149.26953 ? 3.718958 ? ?94.33975483 ?0.849509216?
307.138925 ?59.60571 ? ?0.6311074 ? 106.9636715 ?0.987923188?
307.76875 ? 58.91453 ? ?2.6496741 ? 113.8515307 ?0.802115718?
415.025535 ?217.17398 ? 1.7155688 ? 115.7464603 ?0.875580325?




? ? ? ? [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
#
It is for a good reason that you are requested to include your

sessionInfo()

information in messages to this list.  But even more important is to
first make sure that you are using the current version of R and the
current versions of all relevant packages.

Robert


On Mon, Jan 26, 2015 at 12:28 PM, Ferra Xu via R-sig-Geo
<r-sig-geo at r-project.org> wrote:
#
These are the information I got:
R version 3.1.2 (2014-10-31)Platform: x86_64-w64-mingw32/x64 (64-bit)
locale:[1] LC_COLLATE=English_United States.1252?[2] LC_CTYPE=English_United States.1252 ??[3] LC_MONETARY=English_United States.1252[4] LC_NUMERIC=C ? ? ? ? ? ? ? ? ? ? ? ? ?[5] LC_TIME=English_United States.1252 ? ?
attached base packages:[1] stats ? ? graphics ?grDevices utils ? ? datasets ?methods ? base ? ??
other attached packages:[1] plotrix_3.5-11 ? ? ks_1.9.3 ? ? ? ? ? rgl_0.95.1201 ? ? ?mvtnorm_1.0-2 ? ??[5] misc3d_0.8-4 ? ? ? KernSmooth_2.23-13
loaded via a namespace (and not attached):[1] tools_3.1.2
On Monday, January 26, 2015 6:33 PM, Robert J. Hijmans <r.hijmans at gmail.com> wrote:
> It seems there is a problem with my R !

It is for a good reason that you are requested to include your

sessionInfo()

information in messages to this list.? But even more important is to
first make sure that you are using the current version of R and the
current versions of all relevant packages.

Robert


On Mon, Jan 26, 2015 at 12:28 PM, Ferra Xu via R-sig-Geo
<r-sig-geo at r-project.org> wrote:

  
  
#
Dear all

We are working to convert some toolbox in matlab grass in R grass with 
the scripts in the processing toolbox of qgis.
Some examples are available here:
http://wikhydro.developpement-durable.gouv.fr/index.php/Utilisation_des_donn%C3%A9es_LIDAR_pour_la_directive_inondation
http://wikhydro.developpement-durable.gouv.fr/index.php/D%C3%A9tection_de_remblai_avec_le_LIDAR

On windows 7, we develop on two version of grass  GRASS 7.0.0RC1 and 
GRASS GIS 7.1.svn

The parameters of the two version are not the same, for example
execGRASS("g.region",parameters=list(*rast*=nomoutput))
or
execGRASS("g.region",parameters=list(*raster*=nomoutput))

but there is a lot of other small changes.

Do you know what is the future standard of GRASS to script for the 
future version.

For Grass, more elements are available on
http://grass.osgeo.org/download/software/ms-windows/

Best regards

*Fr?d?ric Pons *
*Expert hydraulique sur les inondations et al?as c?tiers
**DREC/Service Risques Inondations Littoraux et Hydraulique **- T?l.: 
(33)4 42 24 76 68 *
*Direction Territoriale M?diterran?e
*
Centre d??tudes et d?expertise sur les risques, l?environnement, la 
mobilit? et l?am?nagement
www.cerema.fr <http://www.cerema.fr>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20150127/a124e090/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: cerema.png
Type: image/png
Size: 6094 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20150127/a124e090/attachment.png>
#
On Tue, 27 Jan 2015, PONS Frederic - CEREMA/DTerMed/DREC/SRILH wrote:

            
Ask on the GRASS mail list; you can sign up at http://grass.osgeo.org/.

If there are syntactic differences I would assume that those in the 7.1svn
are the future. I've used GRASS (on linux) since 4.0 and current use 7.1svn
so I've seen many differences over the past couple of decades.

Rich
#
On Tue, 27 Jan 2015, Rich Shepard wrote:

            
There is ongoing discussion on the grass-stats list:

http://lists.osgeo.org/mailman/listinfo/grass-stats

The GRASS 7.0 RC1 is where many parameter names have been changed. The 
changes are quite large, so the current R spgrass6 package will only 
handle GRASS 6 up to GRASS 7.0 beta, while the new spgrass7 (in the 
spgrass project on R-Forge) is being matched to changes in GRASS 7.0 RC*.
See:

install.packages("spgrass7", repos="http://R-Forge.R-project.org")

This is thanks to input and work by Rainer Krug and Markus Neteler. RC2 
should be out soon too. spgrass6 will shift to maintenance mode only 
shortly after GRASS 7.0 is released, but will stay available.

The parameter name changes are on:

http://trac.osgeo.org/grass/wiki/Grass7/NewFeatures#Optionschanges

We have not checked against GRASS trunk. I think that your adaptation 
might match the regular 7.* series rather than the development trunk, but 
I agree with Rich that it would be worth asking on the GRASS users or 
developers list.

Roger

  
    
#
Thank you for your advice, I send an e-mail to a french grass list.

The best solution seems for me to work with future version

But I cannot install spgrass 7 or spgrass on windows 7 in R version 3.1.2 :

 > install.packages("spgrass7", repos="http://R-Forge.R-project.org")
Avis : impossible d'acc?der ? l'index de l'entrep?t 
http://R-Forge.R-project.org/bin/windows/contrib/3.1
Message d'avis :
package 'spgrass7' is not available (for R version 3.1.2)
 > RSiteSearch("spgrass7")
Une requ?te de recherche a ?t? soumise ? http://search.r-project.org
La page de r?sultats devrait s'ouvrir dans votre navigateur
 > install.packages("spgrass", repos="http://R-Forge.R-project.org")
Avis : impossible d'acc?der ? l'index de l'entrep?t 
http://R-Forge.R-project.org/bin/windows/contrib/3.1
Message d'avis :
package 'spgrass' is not available (for R version 3.1.2)

How to do it?
Thank you
Fr?d?ric

Le 27/01/2015 21:48, "> Roger Bivand (par Internet, d?p?t 
r-sig-geo-bounces at r-project.org)" a ?crit :

  
  
#
On Wed, 28 Jan 2015, PONS Frederic - CEREMA/DTerMed/DREC/SRILH wrote:

            
That is no use. Use grass-stats if you need help with the R-GRASS 
interface, the developers do not read other channels regularly.
Work with forthcoming (soon to be stable) GRASS 7, not with the 
development version.
So download:

http://download.r-forge.r-project.org/bin/windows/contrib/latest/spgrass7_0.1-0.zip

and install from local zip file (untried). None of the developers uses 
Windows, so this isn't a priority. Alternatively, install R-tools foe 
Windows and then install the source package.

Roger

  
    
#
Roger Bivand <Roger.Bivand at nhh.no> writes:
I am in the process of setting up a virtual machine (Windows 7, R,
GRASS) to test it out (If I manage to deal with windows ...).

Cheers,

Rainer

  
    
1 day later
#
Rainer M Krug <Rainer at krugs.de> writes:
Just a follow up: I could install spgrass7 without problems by using

--8<---------------cut here---------------start------------->8---

install.packages("spgrass7", repos="http://R-Forge.R-project.org")

--8<---------------cut here---------------end--------------->8---

Under Windows 7, R version 3.1.2 (2014-10-31)

Cheers,

Rainer