r - 将 abline 添加到填充等高线图

标签 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

最佳答案

你可以把 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)
  }
)

只是因为在 plot.title 中放置一个 abline 感觉有点奇怪。

关于r - 将 abline 添加到填充等高线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53907534/

相关文章:

r - dplyr top_n 中的权重变量

r - 循环遍历数据帧列表以在 R 中创建图形

graphics - 重新编写并使用重新处理

java - Java 上的 plot/drow 分形图形库

opengl - 分割如何提高性能?

r - R中的all. different函数,在循环中应用它吗?

r - 如何在R中的弹性包中使用index_create()为数据建立索引

r - R Markdown ioslides 中的目录

c++ - 什么是更好的Matrix4x4类设计c++新手

c++ - 雾好像不是很明显,怎么调参数?