[英]Adding a color legend to an image
我有一個矩陣,我做了一個使用image(matrix)
。 有沒有在我的圖像中添加顏色的圖例,就像我在為圖表添加圖例時一樣?
或者可以像這樣提供圖例:
legend(grconvertX(0.5, "device"), grconvertY(1, "device"),
c("0",".5","1"), fill = colMap[c(1, 10, 20)], xpd = NA)
其中grconvertX()和grconvertY()和xpd確保圖例位於繪圖區域之外。 一個似是而非的例子是:
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:我知道這是一個舊的請求,它已經解決了。 然而,我正在尋找類似的答案,我找不到它。 由於我懶得解決這個問題,我想也許其他人也可以從中受益。
R中的image
是一個相當基本的繪圖功能。 如果您想要一個能夠自動為圖例分配空間的函數,您可能需要查看filled.contour
。 或試試這個:
library(lattice)
levelplot(matrix)
從包fields
,您可以嘗試image.plot
。 此功能基於常規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.