如何使用R中的matplot为轨迹图创建标签和子图?

时间:2015-09-02 14:36:30

标签: r matplotlib

我正在尝试复制使用python中的matplotlib创建的下面的图像。我不知道如何为x-,y-和z-lim以及x-,y添加代码 - 和z-lab,并希望有人可以帮助我?另外,有没有办法在主图的中间放置一个子图,放大轨迹图的窄极限范围?

到目前为止我的代码是:

matplot(out, type = "p")

我的旧Python图

enter image description here

新的R图,正在开发中:

enter image description here

@ eipi10帮助后更新。

        #Main Plot with labels and axis limits
matplot(out, main="", type = "l",
        xlab = "x label", ylab = "y label",
        xlim = c(0, 100), ylim = c(-100, 500),
        lwd = c(1,1,2))

#Add legend
legend(0, -50, c("x", "y1", "y2"),
       lty = c(1,2,3), lwd = 2,
       col = c("black", "red", "green"))

#Add subplot
subplot(matplot(out, type = "l",
       xlab = "", ylab="", lwd = c(1,1,2),
       xlim = c(50,70), ylim = c(-10,10),
       cex.axis = 0.8, las = 1), 
       125, 110, size = c(2, 1))

1 个答案:

答案 0 :(得分:2)

以下是假数据的示例:

library(Hmisc)  # For subplot function

# Fake parameters (for adding to plot title)
a = 0.158
b = 0.004
g1 = 1.5 

# Fake data
x = seq(0,100,0.1)
y1 = x/2*cos(x)
y2 = x^2/100

# Main plot with labels and axis limits
matplot(as.matrix(data.frame(x,y1,y2)), type="l", 
        xlab="x label", ylab="y label",
        xlim=c(0,1000), ylim=c(-100,150),
        lwd=c(1,1,2), las=1)

# Add title
title(substitute(paste("Parameters: ", lambda[1], "=", g1), list(g1=g1)), line=3.2)
title(substitute(paste(alpha, "=", a,", ", beta, "=", b), list(a=a, b=b)), line=2)
title(paste0("Or without math symbols: alpha = ", a, ", beta = ", b),
      line=0.65, font.main=1)

# Add legend
legend(0, -50, c("x","y1","y2"),
       lty=c(1,2,3), lwd=2,
       col=c("black","red","green"))

# Add subplot
subplot(matplot(as.matrix(data.frame(x,y1,y2)), type="l", 
                xlab="", ylab="", lwd=c(1,1,2),
                xlim=c(50,70), ylim=c(-10,10),
                cex.axis=0.8, las=1),
        125, 110, size=c(2, 1.75))

enter image description here