将abline添加到填充的轮廓图

时间:2018-12-23 22:13:09

标签: r graphics

我绘制了轮廓图并添加了一条对角线:

library(MASS)
library(fields)
x <- c(21.06, 28.89, 23.00, 23.61, 23.61, 22.83, 30.44)
y <- c(26.56, 24.00, 13.06, 18.61, 18.61, 14.17, 25.33)
z <- kde2d(x, y, n=32, lims = c(0,32,0,32))
contour(z, col = "red", main = "Density estimation: contour plot",
       las=0,

    plot.title=
               {
      title(xlab=expression(alpha),cex.lab=2)
      mtext(expression(beta),2,cex=2,line=3,las=1)
               } 
)
abline(0, 1, col = "red", lwd = 2) 

first graph

我想我更喜欢填充轮廓,但是现在这条线已经关闭:

filled.contour(z,  plot.title={
   title(main = "Density estimation: contour plot")
   title(xlab=expression(alpha),cex.lab=2)
   mtext(expression(beta),2,cex=2,line=3,las=1)
 })
 abline(0, 1, col = "red", lwd = 2) 

second graph

1 个答案:

答案 0 :(得分:5)

您可以在其中扔abline

library(MASS)

x <- c(21.06, 28.89, 23.00, 23.61, 23.61, 22.83, 30.44)
y <- c(26.56, 24.00, 13.06, 18.61, 18.61, 14.17, 25.33)
z <- kde2d(x, y, n = 32, lims = c(0, 32, 0, 32))

filled.contour(z,  plot.title={
  title(main = "Density estimation: contour plot")
  title(xlab=expression(alpha),cex.lab=2)
  mtext(expression(beta),2,cex=2,line=3,las=1)
  abline(0, 1, col = "red", lwd = 2)
})

enter image description here

或者(如评论中所建议),您最好执行以下操作:

filled.contour(
  z,
  plot.title = {
    title(main = "Density estimation: contour plot")
    title(xlab = expression(alpha), cex.lab = 2)
    mtext(expression(beta), 2, cex = 2, line = 3, las = 1)
  },
  plot.axes = {
    abline(0, 1, col = "red", lwd = 2)
  }
)

仅仅因为在abline中放入plot.title有点奇怪。