[英]How to create a function that combines 3 plots in one plot using R
I want to combine three plots that I made using the following function 我想结合使用以下功能绘制的三个图
#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
The first plot is named Nts
, the second, n0450
, and the last, n05
. 第一个图命名为Nts
,第二个图命名为n0450
,最后一个命名为n05
。 Also, I want to use the matplot
command somehow. 另外,我想以某种方式使用matplot
命令。
Here's one using base R. First plot
one of them and then add others using lines
. 这是一个使用基准R的plot
。首先plot
其中一个,然后使用lines
添加其他plot
。 Make sure you set the ylim
to encompass the whole range of data. 确保将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"))
With matplot+matlines
: 使用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)
With matplot
only: 仅使用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)
With ggplot
: 使用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.