[R] ggplot inside of the function

Thevaraja, Mayooran M@Thev@r@j@ @end|ng |rom m@@@ey@@c@nz
Mon Nov 25 02:53:54 CET 2019


Hello Folks
               I am working some R package development. When I was using ggplot inside of the function, I need to get the output graph's legend must be corresponding to the input parameters numerical value (need to be automatically changed when we input different parameters).  Therefore anyone has any ideas? Here, I have given below a simple example. I need to get my output graph's legend should be Norm(mu=0.2, var=0.8),...


example <- function(mu1,mu2,mu3,var1,var2,var3){
  x <- seq(-3,3, by=0.01)
  p_1 <- dnorm(x,mu1,var1)
  p_2 <- dnorm(x,mu2,var2)
  p_3 <- dnorm(x,mu3,var3)
  Prob_df <- data.frame(x,p_1,p_2,p_3)
  Prob <- plyr::rename(Prob_df, c("p_1"="Norm(mu=mu1, var=var1)","p_2"="Norm(mu=mu2, var=var2)","p_3"="Norm(mu=mu3, var=var3)"))
  melten.Prob <- reshape2::melt(Prob, id = "x", variable.name = "Methods", value.name = "Prob")
ggplot2::ggplot(melten.Prob)+
    ggplot2::geom_point(ggplot2::aes(x = x, y = Prob, group= Methods, colour = Methods ))+
    ggplot2::geom_line(ggplot2::aes(x = x, y = Prob, group= Methods, colour = Methods ))

}

mu1 <- 0.2
mu2 <- 0.5
mu3 <- 1
var1 <- 0.8
var2 <- 1.2
var3 <- 2.1
example(mu1,mu2,mu3,var1,var2,var3)


Thanks
Mayooran

	[[alternative HTML version deleted]]



More information about the R-help mailing list