在一个图中绘制两个x轴?

时间:2015-04-14 19:31:56

标签: r plot

这可以很容易地绘制。

xvals <- c(5.5, 15.5, 25.5, 35.5, 45.5, 55.5, 65.5, 75.5, 85.5, 95.5)
yvals <- c(81, 63, 45, 27, 9, -9, -27, -45, -63, -81)
xn <- rep(1000, 10)
plot(xvals, yvals)

xvals和xn共享相同的yvals,因此我想在一个图中绘制:

   yaxis:
   xaxis lower:xvals
   xaxis upper:xn

enter image description here

我只想将xn添加到与x轴(上)轴(3)相同的图中。 对此有任何想法!

1 个答案:

答案 0 :(得分:2)

将代码保留为plot(xvals, yvals)。然后添加以下内容:

#plot the first plot
plot(xvals, yvals)
#start new overlaid plot
par(new=TRUE)
#plot xn but remove the xaxis and the x label for now
plot(xn, yvals, xaxt='n', xlab='')
#add those at the top of the graph (3)
axis(3, xn)
#bonus. add this line to add a secondary xlabel on top
mtext(side = 3, line = 2, "xn")

结果:

enter image description here