我正在尝试在R的底图上放置一个轴在底部x轴下方。目前,我有一个轴如下:
axis(side=1, col="blue")
现在我想在那个下面添加另一个轴,使用不同的值。我怎么能这样做?
答案 0 :(得分:6)
我认为这就是你要找的东西:
这里的关键是axis
行。 at
表示放置标签的tic标记,而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
答案 1 :(得分:4)
这是一种快速而肮脏的方法,可以在实际的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")
答案 2 :(得分:2)
我看到问题解决了。我会告诉你我的例子,它有用。
#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)