简体   繁体   English

为图像添加颜色图例

[英]Adding a color legend to an image

I have a matrix that I made an image of using image(matrix) . 我有一个矩阵,我做了一个使用image(matrix) Is there away to add a legend of the colors to my image like I do when adding a legend to plot? 有没有在我的图像中添加颜色的图例,就像我在为图表添加图例时一样?

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. 其中grconvertX()和grconvertY()和xpd确保图例位于绘图区域之外。 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)

ps: I know it is an old request and it is solved. ps:我知道这是一个旧的请求,它已经解决了。 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. 由于我懒得解决这个问题,我想也许其他人也可以从中受益。

image in R is a fairly basic plotting function. R中的image是一个相当基本的绘图功能。 You might want to look at filled.contour if you want a function that will automatically allocate space for a legend. 如果您想要一个能够自动为图例分配空间的函数,您可能需要查看filled.contour Or try this: 或试试这个:

 library(lattice)
 levelplot(matrix)

From the package fields , you could try image.plot . 从包fields ,您可以尝试image.plot This function is based on the regular image , but it provides a figure legend. 此功能基于常规image ,但它提供了图形图例。

library(fields)
x = 1:10
y = 1:15
z = outer( x,y,"+") 
image.plot(x, y, z)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM