如何为参数的不同值绘制分布?

时间:2017-12-13 11:00:36

标签: r ggplot2

我有以下脚本:

library(ggplot2)

values <- c(0.1,0.15,0.2,0.3,0.5,1,1.5,2,2.5)
colours <- palette()[1:length(values)];


p <- ggplot(data.frame(x=c(0, 2)), aes(x)) +
  stat_function(fun= function(x) dlnorm(x,mean = 0, sd = 0.1),aes(colour="0.1"))+
  stat_function(fun= function(x) dlnorm(x,mean = 0, sd = 0.15),aes(colour="0.15"))+
  stat_function(fun= function(x) dlnorm(x,mean = 0, sd = 0.2),aes(colour="0.2"))+
  stat_function(fun= function(x) dlnorm(x,mean = 0, sd = 0.3),aes(colour="0.3"))+
  stat_function(fun= function(x) dlnorm(x,mean = 0, sd = 0.5),aes(colour="0.5"))+
  stat_function(fun= function(x) dlnorm(x,mean = 0, sd = 1),aes(colour="1"))+
  stat_function(fun= function(x) dlnorm(x,mean = 0, sd = 1.5),aes(colour="1.5"))+
  stat_function(fun= function(x) dlnorm(x,mean = 0, sd = 2),aes(colour="2"))+
  stat_function(fun= function(x) dlnorm(x,mean = 0, sd = 2.5),aes(colour="2.5"))+
  scale_colour_manual("Sigma:", values=colours, breaks=as.character(values))

p

对于参数的不同值,它产生以下图表(减去标题):对数的正态分布如何:

graph of log-normal distribution

如何用更贴心的东西替换那些复制粘贴的线?

stat_function(fun= function(x) dlnorm(x,mean = 0, sd = values),aes(colour=as.character(values)))

这样的东西

编辑:与建议重复的问题不同,我处理的是函数,而不是数据,因此我不确定如何应用这些建议

我也试过像这样的for循环:

library(ggplot2)

values <- c(0.1,0.15,0.2,0.3,0.5,1,1.5,2,2.5)
colours <- palette()[1:length(values)];


p <- ggplot(data.frame(x=c(0, 2)), aes(x)) +
  ggtitle(expression(paste("Log-normaal distributie met verschillende waarden van ", sigma)))+
  ylab(expression(paste(f[X](x)))) 


for(i in 1:length(values)){
  p <- p + stat_function(fun= function(x) dlnorm(x,mean = 0, sd = values[i]), aes(colour=as.character(values[i])))
}


p +   scale_colour_manual("Sigma:", values=colours, breaks=as.character(values))

但它不起作用:它只绘制最后一条曲线。

我也尝试了一个建议的答案,它工作正常,但我无法为颜色添加标签:

ggplot(data.frame(x=c(0, 2)), aes(x)) + 
  mapply(function(col, mean, sd) {
    stat_function(fun = dlnorm, args = list(mean = mean, sd = sd), aes(colour=col))}, 
    mean = 0, sd = values, col = as.character(values)) +
  scale_colour_manual("Sigma:", values=colours, breaks=as.character(values))

它产生错误说: Error in (function (..., row.names = NULL, check.rows = FALSE, check.names = TRUE, : arguments imply differing number of rows: 0, 2

1 个答案:

答案 0 :(得分:3)

希望这有帮助!

library(ggplot2)

values <- c(0.1, 0.15, 0.2, 0.3, 0.5, 1, 1.5, 2, 2.5)
colours <- c(palette(), "orange");

ggplot(data.frame(x=c(0, 2)), aes(x)) + 
  mapply(function(col, mean, sd) {
    stat_function(fun = dlnorm, args = list(mean = mean, sd = sd), colour = col)}, 
    mean = 0, sd = values, col = colours)

修改更新
似乎您想要使用stat_function添加颜色图例,然后您需要多次重复,而不是将其放在for循环或mapply中。


如果您对其他解决方案感兴趣,那么您可以尝试下面的代码块:

library(dplyr)
library(ggplot2)

df <- data.frame(mean = rep(0,9),
                 sd = c(0.1, 0.15, 0.2, 0.3, 0.5, 1, 1.5, 2, 2.5))
colours <- c(palette(), "orange");

x <- seq(0,2,0.01)
my_func <- mdply(df, function(mean,sd){
  data.frame(x=x, y=dlnorm(x,mean, sd), sigma=as.character(sd))}) %>% 
  bind_rows

ggplot(my_func, aes(x=x, y=y, colour=sigma)) + 
  geom_line() +
  scale_colour_manual(values=colours) +
  theme_bw()

Output plot