Adding a color legend to an image
Asked Answered
B

4

22

I have a matrix that I made an image of using image(matrix). Is there away to add a legend of the colors to my image like I do when adding a legend to plot?

Brittani answered 3/3, 2013 at 17:4 Comment(2)
This question is too vague to be properly answered. Please add programming languages, platforms, libraries, etc. that you are usingHarold
@Harold r is a programming language!Altamirano
W
10

image in R is a fairly basic plotting function. You might want to look at filled.contour if you want a function that will automatically allocate space for a legend. Or try this:

 library(lattice)
 levelplot(matrix)
Walkover answered 3/3, 2013 at 17:51 Comment(0)
V
19

Or the legend could be provided like this:

     legend(grconvertX(0.5, "device"), grconvertY(1, "device"), 
     c("0",".5","1"), fill = colMap[c(1, 10, 20)], xpd = NA)

where grconvertX() and grconvertY() and xpd makes sure the legend is outside the plotting region. A plausible example would be:

    nsamples <- 20
    mat <- rnorm(nsamples, .5, .15)
    dim(mat) <- c(4, 5)
    colMap <- colorRampPalette(c("red","white","blue" ))(nsamples)
    image(1:4, 1:5, mat, col = colMap, ylab="", xlab="")
    legend(grconvertX(0.5, "device"), grconvertY(1, "device"),
    c("0",".5","1"), fill = colMap[c(1, 10, 20)], xpd = NA)

p.s.: I know it is an old request and it is solved. However I was looking for a similar answer and I could not find it. Since I bother solving this issue I thought maybe someone else could also benefit from it.

Vassal answered 12/1, 2016 at 20:2 Comment(2)
Very helpful. For a plot with multiple figures, I used par(mfrow=c(1,1), new=TRUE, fig=c(0,1,0,1)) in front of the legend statement and "nfc" rather than "device".Inflated
When image() interprets the color vector though, it must be mapping from your data min-to-max, so probably need to adjust the legend values to match the data exactly? e.g. 0 and 1 don't necessarily exist in your example data, so the legend may not be mapping the colors exactly.Phatic
W
10

image in R is a fairly basic plotting function. You might want to look at filled.contour if you want a function that will automatically allocate space for a legend. Or try this:

 library(lattice)
 levelplot(matrix)
Walkover answered 3/3, 2013 at 17:51 Comment(0)
L
6

From the package fields, you could try image.plot. This function is based on the regular image, but it provides a figure legend.

library(fields)
x = 1:10
y = 1:15
z = outer( x,y,"+") 
image.plot(x, y, z)
Lagan answered 27/4, 2017 at 13:2 Comment(0)
P
0

To make a gradient color bar, you can do this:

# make example data and color key
x <- matrix(data = sample(x = 5:85, size = 100, replace = T), nrow = 10)
n.colors <- 90
color.fun <- colorRampPalette(colors = c("magenta2", "grey"), bias = 10)
col.key <- data.table("color" = color.fun(n = n.colors),
                      "value" = seq(from = min(x), to = max(x), along.with = 1:n.colors))

# make the plot with image- leaving space for legend using fig
par(fig = c(0,.9,0,1), mar = c(2,2,2,0))
image(z = t(x), axes = F, ann = F, col = col.key$color)

# add the legend
par(fig = c(.9,1,.3,.7), mar = c(1,1,1,2.5), new = T)
plot(x = rep(1,length(col.key$value)), y = col.key$value, xlim = c(0,1), col = col.key$color, type = "n", xaxs = "i", yaxs = "i", ann = F, axes = F)
segments(x0 = 0, x1 = 1, y0 = col.key$value, y1 = col.key$value, col = col.key$color, lwd = 5)
axis(side = 4,lwd = 0, las = 2, line = -.75)
mtext(text = "Legend", adj = 0, line = 1, cex = 1.2)

Here's the example plot

Note that this also addresses I think an error in HelloWorld's answer, because you want the colors to exactly map between the color bar and the image (so scale must match your actual data values).

Phatic answered 11/10, 2023 at 23:2 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.