Несколько осей внизу - PullRequest
       21

Несколько осей внизу

3 голосов
/ 05 апреля 2011

Я пытаюсь поместить ось ниже нижней оси x на моем графике в R. В настоящее время у меня есть ось следующим образом:

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

Теперь я хочу добавить еще одну ось нижеодин, с разными значениями.Как я могу это сделать?

Ответы [ 3 ]

6 голосов
/ 05 апреля 2011

Я думаю, это то, что вы ищете:

Ключ здесь - строка 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
4 голосов
/ 05 апреля 2011

Вот быстрый и грязный способ ударить фальшивую «вторую ось 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")

enter image description here

2 голосов
/ 17 июня 2014

Я видел, что проблема была решена.Я покажу вам мой пример, который работает.

#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

...