问题描述
我想绘制一个叶绿素浓度图,但是这些值的分散方式使图例变得不可读(参见图片)
I'd like to plot a map of chlorophyll concentration, but the values are dispersed in such a way that the legend becomes unreadable (see picture)
因此,我试图控制颜色栏/图例中颜色的间距.我想使它均匀分布(同时在地图上保持不均匀的中断).
So I am trying to control the spacing of the colours in the colour bar/legend. I'd like to get it evenly spaced (while keeping the uneven breaks on the map itself).
下面的示例已大大简化,用于栅格图,但是image.plot也是如此.
The the example below is much simplified and is for a raster plot, but the same goes for image.plot.
library(raster)
r <- raster(ncol=5, nrow=4)
r[] <- 1:20
plot(r, breaks = c(0,1,2,3,5,10,20), col = rainbow(6))
我曾考虑过将数据转换为对数值,但这并不能给我令人满意的结果.因此,对图例的任何帮助将不胜感激.
I thought about converting the data to log values, but that doesn't give me a satisfying result. So any help with the legend is much appreciated.
推荐答案
这很丑陋,但您可以将plot
用于地图,将image.plot
用于图例.您需要将自定义休息的标签添加到等间距的休息位置.
It's ugly, but you can use plot
for the map and image.plot
for your legend. You will need to add the labels of your custom breaks to the position of the equally spaced breaks.
没有图例的地图:
library(raster)
r <- raster(ncol=5, nrow=4)
r[] <- 1:20
my_breaks = c(0,1,2,3,5,10,20)
n = 6
my_col = rainbow(n)
plot(r, breaks = my_breaks, col = my_col, legend = FALSE, zlim=c(0,20))
默认的中断间隔为0到20:
The default breaks will be equally spaced from 0 to 20:
def_breaks = seq(0,20,length.out=(n+1))
使用fields
包中的image.plot
添加图例,将自定义中断标签放置在默认中断位置:
Add legend using image.plot
from the fields
package, placing custom break labels at default break positions:
library(fields)
image.plot(r, zlim = c(0,20),
legend.only = TRUE,
col = my_col,
axis.args = list(at = def_breaks, labels = my_breaks))
这篇关于R如何控制颜色条/图例的间距的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!