底部有多个轴

发布于 2024-10-30 02:07:53 字数 135 浏览 3 评论 0原文

我试图在 R 中的绘图的底部 x 轴下方放置一个轴。目前,我有一个轴,如下所示:

axis(side=1, col="blue")

现在我想在该轴下方添加另一个具有不同值的轴。我该怎么做?

I'm trying to place an axis below the bottom x-axis on my plot in R. Currently, I have an axis as follows:

axis(side=1, col="blue")

Now I want to add another axis below that one, with different values. How can I do this?

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

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

发布评论

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

评论(3

孤寂小茶 2024-11-06 02:07:53

我认为这就是您正在寻找的:

这里的关键是axis线。 at 指示要放置标签的刻度标记,label 指示如何称呼该刻度标记。

x <- sample(1:100, 10, replace = T) # just 10 random numbers
y <- sample(1:100, 10, replace = T) # 10 more random numbers
par(mar = c(10, 5, 5, 5)) 
    # increasing the 1st number to 10 makes 10 lines below axis 1
plot(x~y) # normal plot
axis(1, at = c(20, 40, 60, 80), labels = c("1", "2", "3", "4"), line = 5, col = 4) 
    # the "line" indicates which of the 10 lines made above to put the axis on

I think this is what you are looking for:

The key here is the axis line. The at indicates which tic mark to place the label and the label says what to call that tic mark.

x <- sample(1:100, 10, replace = T) # just 10 random numbers
y <- sample(1:100, 10, replace = T) # 10 more random numbers
par(mar = c(10, 5, 5, 5)) 
    # increasing the 1st number to 10 makes 10 lines below axis 1
plot(x~y) # normal plot
axis(1, at = c(20, 40, 60, 80), labels = c("1", "2", "3", "4"), line = 5, col = 4) 
    # the "line" indicates which of the 10 lines made above to put the axis on
凉城已无爱 2024-11-06 02:07:53

这是一种快速但肮脏的方法,可以在实际 x 轴下方放置一个假的“第二个 x 轴”。我没有在这里展示它,但您可以使用layout()来更好地控制间距。

 #Build the data
 t <- 1:1000
 x1 <- 100 * sin(0.01 * t)
 x2 <- 200 * cos(0.04 * t)

 #Set up the plot area for two "crammed" plots
 par(pty="m", plt=c(0.1, 1, 0, 1), omd=c(0.1,0.9,0.1,0.9))
 par(mfrow = c(2, 1))

 #Plot x1 and x2 together
 plot(t, x1, type="l", ylim = 1.5 * range(x1, x2), xaxt="n", xlab="", ylab="", main="", col="blue", las=2)
 lines(t, x2, lwd=1, col="red")
 mtext(side=2, "Top Plot", line=5.3, cex=0.8)
 mtext(side=2, "(units)", line=4, cex=0.8)
 grid()
 mtext(side=3, "2 Plots Crammed Together", line=1.5, cex=1.2)
 legend("topright", legend=c("100 * sin(0.01 * t)", "200 * cos(0.4 * t)"), bg="white", lwd=c(1, 1), col=c("blue", "red"), cex=0.9)

 #Place the 1st x-axis
 axis(side = 1, col="blue", col.axis="blue")
 linloc <- par()$usr[3]
 abline(h=linloc, col="blue")
 mtext(side=1, "First X-Axis", line=2.5, cex=0.8, col="blue")

 #Place the fake 2nd x-axis
 xaxis2 <- 1:10
 par(plt=c(0.1,1,0.6,1))
 plot(xaxis2, type="n", xaxt="n", xlab="", yaxt="n", ylab="", xlim=range(xaxis2), bty="n")
 axis(side = 1, col="red", col.axis="red")
 linloc <- par()$usr[3]
 abline(h=linloc, col="red")
 mtext(side=1, "Second X-Axis", line=2.5, cex=0.8, col="red")

在此处输入图像描述

Here's a quick and dirty way to slap a fake "second x-axis" below the actual x-axis. I didn't show it here, but you can use layout() to get more control on the spacing.

 #Build the data
 t <- 1:1000
 x1 <- 100 * sin(0.01 * t)
 x2 <- 200 * cos(0.04 * t)

 #Set up the plot area for two "crammed" plots
 par(pty="m", plt=c(0.1, 1, 0, 1), omd=c(0.1,0.9,0.1,0.9))
 par(mfrow = c(2, 1))

 #Plot x1 and x2 together
 plot(t, x1, type="l", ylim = 1.5 * range(x1, x2), xaxt="n", xlab="", ylab="", main="", col="blue", las=2)
 lines(t, x2, lwd=1, col="red")
 mtext(side=2, "Top Plot", line=5.3, cex=0.8)
 mtext(side=2, "(units)", line=4, cex=0.8)
 grid()
 mtext(side=3, "2 Plots Crammed Together", line=1.5, cex=1.2)
 legend("topright", legend=c("100 * sin(0.01 * t)", "200 * cos(0.4 * t)"), bg="white", lwd=c(1, 1), col=c("blue", "red"), cex=0.9)

 #Place the 1st x-axis
 axis(side = 1, col="blue", col.axis="blue")
 linloc <- par()$usr[3]
 abline(h=linloc, col="blue")
 mtext(side=1, "First X-Axis", line=2.5, cex=0.8, col="blue")

 #Place the fake 2nd x-axis
 xaxis2 <- 1:10
 par(plt=c(0.1,1,0.6,1))
 plot(xaxis2, type="n", xaxt="n", xlab="", yaxt="n", ylab="", xlim=range(xaxis2), bty="n")
 axis(side = 1, col="red", col.axis="red")
 linloc <- par()$usr[3]
 abline(h=linloc, col="red")
 mtext(side=1, "Second X-Axis", line=2.5, cex=0.8, col="red")

enter image description here

梦途 2024-11-06 02:07:53

我看到问题解决了。我将向您展示我的示例,该示例有效。

#Plot
plot(x=OneMin_DataSet$AAPL_Close,type = "l",las="1", xaxt = 'n',main="Closing Prices",xlab = "", ylab = "Closing price(USD)",col="blue")

#Gridlines
for(i in c(505,510,515,520)) {
lines(c(0,1950),c(i,i),type="l",lty=2,lwd=0.5, col="black")
rm(i)
}

#Axis1
at <- seq(from = 0, to = 1950, by = 130)
axis(1,line=0,at=at,labels=c("9:30","11:40","13:50","9:30","11:40","13:50","9:30","11:40","13:50","9:30","11:40","13:50","9:30","11:40","13:50","16:00"),mgp = c(3, 0.5, 0))
mtext("Time",1,line=0.5,at=-90)

#Axis2
at2<- seq(from = 0, to = 1950, by = 390)
axis(1,line=2.5,at=at2,tick=TRUE,labels=FALSE,tck=0.1)
at3<- seq(from = 0, to = 1940, by = 390)
axis(1,line=1.5,at=at3+200,labels=c("Feb,3","Feb,4","Feb,5","Feb,6","Feb,7"),tick=FALSE,mgp = c(3, 0, 0))
mtext("Date",1,line=1.5,at=-90)

在此处输入图像描述

I saw that the problem was solved. I will show you my example, which works.

#Plot
plot(x=OneMin_DataSet$AAPL_Close,type = "l",las="1", xaxt = 'n',main="Closing Prices",xlab = "", ylab = "Closing price(USD)",col="blue")

#Gridlines
for(i in c(505,510,515,520)) {
lines(c(0,1950),c(i,i),type="l",lty=2,lwd=0.5, col="black")
rm(i)
}

#Axis1
at <- seq(from = 0, to = 1950, by = 130)
axis(1,line=0,at=at,labels=c("9:30","11:40","13:50","9:30","11:40","13:50","9:30","11:40","13:50","9:30","11:40","13:50","9:30","11:40","13:50","16:00"),mgp = c(3, 0.5, 0))
mtext("Time",1,line=0.5,at=-90)

#Axis2
at2<- seq(from = 0, to = 1950, by = 390)
axis(1,line=2.5,at=at2,tick=TRUE,labels=FALSE,tck=0.1)
at3<- seq(from = 0, to = 1940, by = 390)
axis(1,line=1.5,at=at3+200,labels=c("Feb,3","Feb,4","Feb,5","Feb,6","Feb,7"),tick=FALSE,mgp = c(3, 0, 0))
mtext("Date",1,line=1.5,at=-90)

enter image description here

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