在 R 中删除图形的一部分

标签 r plot contour

这是从R中获得的图片(代码如下)。

我想导出到 pdf格式 .

但是,我想先删除右侧的图例栏。

据我所知,没有控制此条形图例的可选参数。

你会怎么做?

enter image description here

library(gplots)

f <- function(x, y, theta)
{
  num <- (x^(-theta) + y^(-theta) - 1)^(-1 / theta)
  denom <- x * y
  return(num / denom)
}

x <- y <- seq(0.01, 0.18, 0.01)
z <- outer(x, y, FUN=f, theta=2/3)

levels=seq(0, 36, 3)
draw.contour <- function()
{
  contour(x=x, y=y, z=z, add=TRUE, 
          levels=levels,
          drawlabels=TRUE,
          labcex=0,
          xlim=rev(range(x)),
          ylim=rev(range(y)))
}

par(mgp=c(2, 0.5, 0))
filled.contour(x=x, y=y, z=z,
               levels=levels, 
               col=colorpanel(length(levels) + 1, "white", "grey10"),
               xlim=rev(range(x)),
               ylim=rev(range(y)),
               plot.axes={axis(1, c(0.18, 0.01), label=TRUE, tcl=-0.5)
                          axis(2, c(0.18, 0.01), label=TRUE, tcl=-0.5)
                          draw.contour()},
               xlab="x",
               frame=FALSE)
mtext(text="y", side=2, line=1.8, las=1) 
par(mgp=c(3, 1, 0))

最佳答案

filled.contour函数实际上是两个图的组合;一种是填充轮廓,一种是图例。您可以做的是修改原始函数并创建自己的自定义函数。以下是修改后的filled.contour我称之为 my.filled.contour。我所做的只是注释掉图例部分。我没有改变边距,但如果你愿意,你可以这样做。

my.filled.contour <-
function (x = seq(0, 1, length.out = nrow(z)), y = seq(0, 1,
    length.out = ncol(z)), z, xlim = range(x, finite = TRUE),
    ylim = range(y, finite = TRUE), zlim = range(z, finite = TRUE),
    levels = pretty(zlim, nlevels), nlevels = 20, color.palette = cm.colors,
    col = color.palette(length(levels) - 1), plot.title, plot.axes,
    key.title, key.axes, asp = NA, xaxs = "i", yaxs = "i", las = 1,
    axes = TRUE, frame.plot = axes, ...)
{
    if (missing(z)) {
        if (!missing(x)) {
            if (is.list(x)) {
                z <- x$z
                y <- x$y
                x <- x$x
            }
            else {
                z <- x
                x <- seq.int(0, 1, length.out = nrow(z))
            }
        }
        else stop("no 'z' matrix specified")
    }
    else if (is.list(x)) {
        y <- x$y
        x <- x$x
    }
    if (any(diff(x) <= 0) || any(diff(y) <= 0))
        stop("increasing 'x' and 'y' values expected")
    mar.orig <- (par.orig <- par(c("mar", "las", "mfrow")))$mar
    on.exit(par(par.orig))
    w <- (3 + mar.orig[2L]) * par("csi") * 2.54
    layout(matrix(c(2, 1), ncol = 2L), widths = c(1, lcm(w)))
    par(las = las)
    mar <- mar.orig
    mar[4L] <- mar[2L]
    mar[2L] <- 1
    par(mar = mar)
    plot.new()
    plot.window(xlim = c(0, 1), ylim = range(levels), xaxs = "i",
        yaxs = "i")
#    rect(0, levels[-length(levels)], 1, levels[-1L], col = col)
#    if (missing(key.axes)) {
#        if (axes)
#            axis(4)
#    }
#    else key.axes
#    box()
    if (!missing(key.title))
        key.title
    mar <- mar.orig
    mar[4L] <- 1
    par(mar = mar)
    plot.new()
    plot.window(xlim, ylim, "", xaxs = xaxs, yaxs = yaxs, asp = asp)
    if (!is.matrix(z) || nrow(z) <= 1L || ncol(z) <= 1L)
        stop("no proper 'z' matrix specified")
    if (!is.double(z))
        storage.mode(z) <- "double"
    .Internal(filledcontour(as.double(x), as.double(y), z, as.double(levels),
        col = col))
    if (missing(plot.axes)) {
        if (axes) {
            title(main = "", xlab = "", ylab = "")
            Axis(x, side = 1)
            Axis(y, side = 2)
        }
    }
    else plot.axes
    if (frame.plot)
        box()
    if (missing(plot.title))
        title(...)
    else plot.title
    invisible()
}

这是结果:
my.filled.contour(x=x, y=y, z=z,
               levels=levels,
               col=colorpanel(length(levels) + 1, "white", "grey10"),
               xlim=rev(range(x)),
               ylim=rev(range(y)),
               plot.axes={axis(1, c(0.18, 0.01), label=TRUE, tcl=-0.5)
                          axis(2, c(0.18, 0.01), label=TRUE, tcl=-0.5)
                          draw.contour()},
               xlab="x",
               frame=FALSE)

enter image description here

关于在 R 中删除图形的一部分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16774928/

相关文章:

r - spark_write_csv dplyr 函数的 options 参数是什么?

r - 如何用 mice R 仅估算一列或几列

python - 当我使用第三个变量定义颜色制作 python 散点颜色图时没有颜色

colors - gnuplot 等高线颜色 : set style line and set linetype not working

python - 等高线图python中的长错误

c++ - 在 OpenCV 中使用 ROI?

r - 以固定间隔将一列分成多行

R dplyr::使用字符串变量重命名并选择

Pandas 条形图更改日期格式

r - 如何在 R 中更改 Y 轴的比例