[R-sig-Geo] How to show the color bar legend in R?

Zack Holden zaholdenfs at gmail.com
Mon Jan 26 19:02:00 CET 2015


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:

> 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
>

	[[alternative HTML version deleted]]



More information about the R-sig-Geo mailing list