ggplot2具有多个参数的绘图函数 [英] ggplot2 plot function with several arguments
问题描述
我有这个功能。
$ b $ pre $ lt; code> change < - function(score,d,k,p){k *(score - 1 /(1 + k ^(d / p)))}
我想在一个单独的情节中绘制所有这个函数的结果用于一系列参数d和p。在基地r它就是这样。
参数<-c(100:400)
colorshelf< -bowbow(length(parameters))#红色为低
为(我在seq_along(参数)){
print(i)
曲线(变化(分数= 1,d = x,k = 100,p = parameters [i ]),从= 0到= 500,add = T,col = colorshelf [i])
}
但我认为这在ggplot2中必须是可能的,但是不能把我的头包裹在这里。我目前坚持这一点。任何帮助表示赞赏。
ggp < - ggplot(data.frame(Ds = c(0:1000),Ps = c(0:1000 )),aes(x = Ds,col = Ps))+
stat_function(fun = change,args = list(score = 1,d = Ds,k = 100,p = Ps))
ggp
C> GGPLOT2 。我认为可能期望 ggplot
能够引导两个不同的参数......
tidyverse,但是可以很容易地用 apply
来完成。
library (dplyr)
变化< - 函数(得分d,k,p){k *(得分-1 /(1 + k ^(d / p)))}
dd < - expand.grid(d = 0:1000,p = 0:100)
dd%>%rowwise%>%
mutate(c = change(score = 1,d = d,k = 100,p = p)) - >
dd2
library(ggplot2)
ggp< - ggplot(dd2,aes(d,c,col = p,group = p))+
geom_path ()
我只从0开始执行 p
到100(而不是0到1000),因为100万个点对于ggplot来说是一个相当大的数据集。 (你真的需要看到1000个单独的值吗?也许 seq(0,1000,长度= 100)
?
I got this function.
change <- function(score, d, k, p) {k*(score - 1/(1+k^(d/p)))}
I would like to plot, in one single plot, all results of this function for a range of arguments d and p. In base r it would be this.
parameters <- c(100:400)
colorshelf <-rainbow(length(parameters)) #red is low
for(i in seq_along(parameters)) {
print(i)
curve(change(score=1, d=x, k=100, p=parameters[i]), from=0, to=500, add=T, col=colorshelf[i])
}
But I thought this must be possible in ggplot2, but can not wrap my head around this. I am currently stuck with this. Any help is appreciated.
ggp <- ggplot(data.frame(Ds=c(0:1000), Ps=c(0:1000)), aes(x=Ds, col=Ps)) +
stat_function(fun=change, args=list(score=1, d=Ds, k=100, p=Ps))
ggp
I would do this outside of ggplot2
. I think it might be too much to expect ggplot
to vectorise over two different parameters ...
This is with tidyverse, but could easily be done with apply
as well.
library(dplyr)
change <- function(score, d, k, p) {k*(score - 1/(1+k^(d/p)))}
dd <- expand.grid(d=0:1000,p=0:100)
dd %>% rowwise %>%
mutate(c=change(score=1,d=d,k=100,p=p)) ->
dd2
library(ggplot2)
ggp <- ggplot(dd2,aes(d,c,col=p,group=p))+
geom_path()
I only did p
from 0 to 100 (rather than 0 to 1000) because 1 million points is a fairly large data set for ggplot. (Do you really need to see 1000 separate values? Maybe seq(0,1000,length=100)
?
这篇关于ggplot2具有多个参数的绘图函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!