2013-05-27 24 views
3

這裏是從R獲得的圖片(代碼在下面給出)。刪除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)) 
+0

你可以試試'.filled.contour'吧? – joran

+0

@joran:你是什麼意思? – Marco

+0

從文檔:「.filled.contour是一個'裸骨'接口,用於將等高線圖添加到已經設置的繪圖區域,它用於編程使用,程序員負責檢查條件論證「。我沒有嘗試過,但它聽起來像是將等高線圖,但沒有傳說,添加到已設置的繪圖區域。 – joran

回答

3

我會讓使用.filled.contour所推薦的joran新的情節。

例如:

plot(NA,xlim=rev(range(x)), 
       ylim=rev(range(y)),xlab="x",ylab="y", 
       frame=FALSE,axes=F,xaxs="i",yaxs="i") 

.filled.contour(x=x, y=y, z=z, 
       levels=levels, 
       col=colorpanel(length(levels) + 1, "white", "grey10")) 
draw.contour() 
axis(1, c(0.18, 0.01), label=TRUE, tcl=-0.5) 
axis(2, c(0.18, 0.01), label=TRUE, tcl=-0.5, las=1) 

使得

enter image description here

+0

太棒了!謝謝! – Marco

5

filled.contour函數實際上是兩個曲線的組合;一個是充滿輪廓的,一個是傳奇。你可以做的是修改原始功能並創建自己的自定義功能。以下是我稱爲my.filled.contour的修改後的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

+0

@P Lapointe:謝謝!給我一些時間來完成它,然後我可能會接受它。 – Marco

+0

@P Lapointe:+1,非常感謝! – Marco