控制等高线图中的 z 标签

发布于 2024-11-04 09:29:49 字数 539 浏览 2 评论 0原文

我试图控制在用点阵库中的 contourplot() 绘制的等高线图中应写入多少个 z 标签。
我有 30 条轮廓线,但我只想标记前 5 条。我尝试了很多类似的事情

contourplot(z ~ z+y, data=d3, cuts=30, font=3, xlab="x axis", ylab="y axis", scales=list(at=seq(2,10,by=2)))
contourplot(z ~ z+y, data=d3, cuts=30, font=3, xlab="x axis", ylab="y axis", at=seq(2,10,by=2))

,但没有任何效果。

另外,是否可以在同一个图表上绘制两个contourplot()?我尝试过

contourplot(z ~ z+y, data=d3, cuts=30)
par(new=T)
contourplot(z ~ z+y, data=d3, cuts=20)

,但没有成功。
谢谢!

I am trying to control how many z labels should be written in my contour plot plotted with contourplot() from the lattice library.
I have 30 contour lines but I only want the first 5 to be labelled. I tried a bunch of things like

contourplot(z ~ z+y, data=d3, cuts=30, font=3, xlab="x axis", ylab="y axis", scales=list(at=seq(2,10,by=2)))
contourplot(z ~ z+y, data=d3, cuts=30, font=3, xlab="x axis", ylab="y axis", at=seq(2,10,by=2))

but nothing works.

Also, is it possible to plot two contourplot() on the same graph? I tried

contourplot(z ~ z+y, data=d3, cuts=30)
par(new=T)
contourplot(z ~ z+y, data=d3, cuts=20)

but it's not working.
Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盗梦空间 2024-11-11 09:29:49

这是我的看法:

library(lattice)
x <- rep(seq(-1.5,1.5,length=50),50)
y <- rep(seq(-1.5,1.5,length=50),rep(50,50))
z <- exp(-(x^2+y^2+x*y))

# here is default plot
lp1 <- contourplot(z~x*y)    

# here is an enhanced one
my.panel <- function(at, labels, ...) { 
    # draw odd and even contour lines with or without labels
    panel.contourplot(..., at=at[seq(1, length(at), 2)], col="blue", lty=2)
    panel.contourplot(..., at=at[seq(2, length(at), 2)], col="red",
                      labels=as.character(at[seq(2, length(at), 2)]))
} 

lp2 <- contourplot(z~x*y, panel=my.panel, at=seq(0.2, 0.8, by=0.2))
lp3 <- update(lp2, at=seq(0.2,0.8,by=0.1))
lp4 <- update(lp3, lwd=2, label.style="align")

library(gridExtra)
grid.arrange(lp1, lp2, lp3, lp4)

在此处输入图像描述

您可以调整自定义面板功能以最适合您的需求(例如用于调平 z 轴的其他比例、颜色等)。

Here is my take:

library(lattice)
x <- rep(seq(-1.5,1.5,length=50),50)
y <- rep(seq(-1.5,1.5,length=50),rep(50,50))
z <- exp(-(x^2+y^2+x*y))

# here is default plot
lp1 <- contourplot(z~x*y)    

# here is an enhanced one
my.panel <- function(at, labels, ...) { 
    # draw odd and even contour lines with or without labels
    panel.contourplot(..., at=at[seq(1, length(at), 2)], col="blue", lty=2)
    panel.contourplot(..., at=at[seq(2, length(at), 2)], col="red",
                      labels=as.character(at[seq(2, length(at), 2)]))
} 

lp2 <- contourplot(z~x*y, panel=my.panel, at=seq(0.2, 0.8, by=0.2))
lp3 <- update(lp2, at=seq(0.2,0.8,by=0.1))
lp4 <- update(lp3, lwd=2, label.style="align")

library(gridExtra)
grid.arrange(lp1, lp2, lp3, lp4)

enter image description here

You can adapt the custom panel function to best suit your needs (e.g. other scale for leveling the z-axis, color, etc.).

小姐丶请自重 2024-11-11 09:29:49

您可以将labels指定为字符向量参数,并使用rep("", 5)设置最后一个值,因此也许对于您在之前关于轮廓的问题中提供的示例

 x = seq(0, 10, by = 0.5)
 y = seq(0, 10, by = 0.5)
 z <- outer(x, y)
 d3 <- expand.grid(x=x,y=y); d3$z <- as.vector(z)
 contourplot(z~x+y, data=d3)
 # labeled '5'-'90'
 contourplot(z~x+y, data=d3, 
    at=seq(5,90, by=5),
    labels=c(seq(5,25, by=5),rep("", 16) ),
    main="Labels only at the first 5 contour lines")
 # contourplot seems to ignore 'extra' labels
 # c() will coerce the 'numeric' elements to 'character' if any others are 'character'
 ?contourplot   # and follow the link in the info about labels to ?panel.levelplot

第二个情节:

You can specify the labels as a character vector argument and set the last values with rep("", 5), so perhaps for the example you offered on an earlier question about contour

 x = seq(0, 10, by = 0.5)
 y = seq(0, 10, by = 0.5)
 z <- outer(x, y)
 d3 <- expand.grid(x=x,y=y); d3$z <- as.vector(z)
 contourplot(z~x+y, data=d3)
 # labeled '5'-'90'
 contourplot(z~x+y, data=d3, 
    at=seq(5,90, by=5),
    labels=c(seq(5,25, by=5),rep("", 16) ),
    main="Labels only at the first 5 contour lines")
 # contourplot seems to ignore 'extra' labels
 # c() will coerce the 'numeric' elements to 'character' if any others are 'character'
 ?contourplot   # and follow the link in the info about labels to ?panel.levelplot

The second plot:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文