简体   繁体   English

如何在 image.plot 中移动垂直色带图例

[英]How to move the vertical color strip legend in image.plot

Using image.plot from fields in R, I need to move the vertical color legend to the right a bit in order to accommodate a second y axis on the right hand side of the plot.使用 R 中fields image.plot ,我需要将垂直颜色图例向右移动一点,以便在图的右侧容纳第二个 y 轴。 Here is a reproducible example of what I'm talking about:这是我正在谈论的可重复示例:

x <- seq(0, 1, 0.1)
y <- x
n <- length(x)
z <- matrix(runif(n^2), n, n)
xyzlist <- list(x, y, z)
image.plot(x,y,z)
yaxis2 <- y^2
axis(4, at = y, labels = yaxis2)

图片

So you see how the new axis labels crash into the legend.所以你会看到新的轴标签如何撞到图例中。 How to move the legend over so that it doesn't crash into the new axis labels?如何移动图例以使其不会撞到新的轴标签?

You can toy around (to some extent) with the legend's size and location by using the argument smallplot of the function.您可以通过使用函数的参数smallplot (在某种程度上)图例的大小和位置。

In your example, you could use the following code:在您的示例中,您可以使用以下代码:

library(fields)
x <- seq(0, 1, 0.1)
y <- x
n <- length(x)
z <- matrix(runif(n^2), n, n)
xyzlist <- list(x, y, z)
yaxis2 <- y^2

### code added: left, right, bottom, top
image.plot(x,y,z, smallplot = c(.89, .94, .2, .8)) 
axis(4, at = y, labels = yaxis2)

This yields the following plot:这产生了以下情节:

在此处输入图片说明

Of course, you can modify the legend further, making it smaller, shorter, etc当然,您可以进一步修改图例,使其更小、更短等

Please, let me know whether this is what you want.请让我知道这是否是您想要的。

Alternative选择

An alternative is to draw the plot 2 times.另一种方法是绘制该图 2 次。 The first call generates a plot without the legend (because it is not well specified, and this generates an error).第一次调用生成一个没有图例的图(因为它没有很好地指定,这会产生一个错误)。 Then the axis is plotted (with a line adjustment, which may be different depending on the size of the plotting window).然后绘制轴(进行线调整,根据绘图窗口的大小可能会有所不同)。 Thirdly and lastly, the legend is drawn (further to the left than in the previous plot with the error).第三次也是最后一次,绘制图例(比上一个有错误的图更靠左)。

image.plot(x,y,z, smallplot = c(.85, .86, 1, .8)) 
axis(4, at = y, labels = yaxis2, line = -6)
image.plot(x,y,z, smallplot = c(.89, .92, .2, .8), legend.only = TRUE)

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

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