简体   繁体   English

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

[英]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.

相关问题 如何编辑由使用图创建的其他函数生成的R图? - How to edit R plots generated by other function that uses plot to create it? 如何使用 R 中的“plot_grid”减少一列图的 plot 边距? - How to reduce plot margins of one column of plots of an arrange of plots using `plot_grid` in R? 如何使用R在一个窗口中绘制多个交互式图? - How to plot multiple interactive plots in one window using R? 使用R中的&#39;for&#39;循环在数据框中创建多个图(每列一个图) - Create multiple plots (one plot for each column) in a data frame using 'for' loop in R R:创建一个用户定义的函数来绘制数据然后导出图 - R: Create a User-defined Function that Plots data and then Exports the plot 在R中使用plotly(plot.ly)创建多个图 - Create multiple plots using plotly (plot.ly) in R 如何创建一个在 R 中绘制径向线的函数? - How to create a function that plots radials in R? 如何在 R 中使用 `ggdraw` 和 `plot_grid()` 在 X 和 Y 轴上创建一个公共标题? - How to create a common title in X and Y axis in an arrange of plots using `ggdraw` and `plot_grid()` in R? 使用 plot 和 mfrow function 在 r 中保存几个地块 - Saving several plots in r using plot and mfrow function R ggplot:如何使用边际箱形图创建散点图 - R ggplot: How to create a scatter plot with marginal box plots
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM