我如何绘制R中的多个函数? [英] How can I plot multiple functions in R?

查看:97
本文介绍了我如何绘制R中的多个函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用ggplot,是否有在同一个图上绘制几个函数的方法?我希望使用来自文本文件的参数作为函数的参数,并将它们覆盖在同一个图上。



我理解,但我不知道如何将可视化函数加在一起,如果我循环。
这是哈德利想法的实现。

  library( (x)x * log(x),函数(x)x ^ 2,exp)
cols< - 热量对于(i in 1:length(funcs)),颜色(5,1)
p <-ggplot()+ xlim(c(1,10))+ ylim(c(1,10))
。 ))
p <-p + stat_function(aes(y = 0),fun = funcs [[i]],color = cols [i])
print(p)


Using ggplot, is there a way of graphing several functions on the same plot? I want to use parameters from a text file as arguments for my functions and overlay these on the same plot.

I understand this but I do not know how to add the visualized function together if I loop through.

解决方案

Here is an implementation of Hadley's idea.

library(ggplot2)
funcs <- list(log,function(x) x,function(x) x*log(x),function(x) x^2,  exp)
cols <-heat.colors(5,1)
p <-ggplot()+xlim(c(1,10))+ylim(c(1,10))
for(i in 1:length(funcs))
    p <- p + stat_function(aes(y=0),fun = funcs[[i]], colour=cols[i])
print(p)

这篇关于我如何绘制R中的多个函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆