繁体   English   中英

如何使用R创建在一个绘图中合并3个绘图的函数

[英]How to create a function that combines 3 plots in one plot using R

我想结合使用以下功能绘制的三个图

#FUNCTION
dlogistic<-function(k=300,rd=0.599,NO1=2,t=20){
    N<-c(NO1,numeric(t))
    for(i in 1:t){
        N[i + 1] <- N[i] + rd* N[i] * (1-N[i]/k)
    }
    return(N)
}

#Plot #1
Nts <- dlogistic()
Nts

#Plot #2
K450<-300*1.5
n450=dlogistic(k=K450)
n450

#Plot#3
k05<-300*0.5
n05=dlogistic(k=k05)
n05

第一个图命名为Nts ,第二个图命名为n0450 ,最后一个命名为n05 另外,我想以某种方式使用matplot命令。

这是一个使用基准R的plot 。首先plot其中一个,然后使用lines添加其他plot 确保将ylim设置为涵盖整个数据范围。

graphics.off()
windows(width = 6, height = 6)
plot(Nts, type = "l", ylim = c(min(Nts, n450, n05), max(Nts, n450, n05)))
lines(n450, type = "l", col = "red")
lines(n05, type = "l", col = "blue")
legend('topleft', legend = c("Nts", "n450", "n05"), lty = 1, col = c("black", "red", "blue"))

在此处输入图片说明

使用matplot+matlines

matplot(1:length(Nts), cbind(Nts, n450, n05), pch=19, xlab='x', ylab='y')
matlines(1:length(Nts), cbind(Nts, n450, n05), xlab='x', ylab='y')
legend('topleft', legend=c('Nts','n450','n05'), col=1:3, pch=19, lwd=1, lty=1:3)

在此处输入图片说明

仅使用matplot

matplot(1:length(Nts), cbind(Nts, n450, n05), type='l', xlab='x', ylab='y')
legend('topleft', legend=c('Nts','n450','n05'), col=1:3, lwd=1, lty=1:3)

在此处输入图片说明

使用ggplot

library(ggplot2)
ggplot() +
  geom_line(aes(x=1:length(Nts), y=Nts, col='Nts')) +
  geom_line(aes(x=1:length(n450), y=n450, col='n450')) +
  geom_line(aes(x=1:length(n05), y=n05, col='n05')) +
  xlab('x')+
  ylab('y')+
  theme_bw()

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM