R: Manipulate and ggplot2 moving density curve not working -


thought ask question setup pair of examples:

  • the first example (first chunk of code) has normal distribution of products, , moving limit determines passes , fails, producing yield (this 1 works, , moving geom_vline, or red vertical line).
  • the second example (second chunk of code) has normal distribution of varying mean, , limit stationary @ 0 (i tried moving geom_density using manipulate , couldn't work)

question: how manipulate work ggplot2 move density curve (and second example work)?

first example:

require(manipulate) require(ggplot2)  set.seed(10) data <- data.frame(dens=rnorm(1000, mean=20, sd=3))  # moving limit, production same manipulate( {     pass <- sum(data>limit)     fail <- sum(data<=limit)     yield <- pass/(pass+fail)*100     labpass <- paste0("pass=",tostring(pass))     labfail <- paste0("fail=",tostring(fail))     labyield <- paste0("yield=",formatc(yield,format="f", digits=2),"%")      ggplot(data) +         geom_density(aes(x=dens), fill="white") +         labs(title="product yield") +         xlim(-5,30) +         xlab("normal dist. mean=20, sd=3") +         ylab("density") +         geom_vline(xintercept=limit, linetype="longdash", colour="red") +         annotate("text", x=limit-3, y=0.10, label=labpass)+         annotate("text", x=limit-3, y=0.09, label=labfail)+         annotate("text", x=limit-3, y=0.08, label=labyield) }, limit=slider(5,20, initial=5)) 

second example (move geom_density <- not working):

require(manipulate) require(ggplot2)  set.seed(10) data <- data.frame(dens=rnorm(1000, mean=20, sd=3))  # limit same, production mean moves manipulate( {     pass <- sum(data-20+movem>0)     fail <- sum(data-20+movem<=0)     yield <- pass/(pass+fail)*100     labpass <- paste0("pass=",tostring(pass))     labfail <- paste0("fail=",tostring(fail))     labyield <- paste0("yield=",formatc(yield,format="f", digits=2),"%")      ggplot(data) +         geom_density(aes(x=dens-20+movem), fill="white") +         labs(title="product yield") +         xlim(-5,30) +         xlab("normal dist. mean=variable, sd=3") +         ylab("density") +         geom_vline(xintercept=0, linetype="longdash", colour="red") +         annotate("text", x=-3, y=0.10, label=labpass)+         annotate("text", x=-3, y=0.09, label=labfail)+         annotate("text", x=-3, y=0.08, label=labyield) }, movem=slider(0,20, initial=20)) 

image first example: enter image description here

based on solution lukea's comment, here code moves density curve mean.

yield curve in r moving mean of distribution, , having stationary acceptance limit:

require(manipulate) require(ggplot2)  set.seed(10) data <- data.frame(dens=rnorm(1000, mean=20, sd=3))  # limit same, production mean moves manipulate( {     pass <- sum(data$dens-20+movem>0)     fail <- sum(data$dens-20+movem<=0)     yield <- pass/(pass+fail)*100     labpass <- paste0("pass=",tostring(pass))     labfail <- paste0("fail=",tostring(fail))     labyield <- paste0("yield=",formatc(yield,format="f", digits=2),"%")     # answer question: use column in dataframe     data$shiftdens <- data$dens - 20 + movem      ggplot(data) +         geom_density(aes(x=shiftdens), fill="white") +         labs(title="product yield") +         xlim(-5,30) +         xlab("normal dist. mean=variable, sd=3") +         ylab("density") +         geom_vline(xintercept=0, linetype="longdash", colour="red") +         annotate("text", x=-3, y=0.10, label=labpass)+         annotate("text", x=-3, y=0.09, label=labfail)+         annotate("text", x=-3, y=0.08, label=labyield) }, movem=slider(0,20, initial=20)) 

enter image description here


Comments

Popular posts from this blog

php - failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request -

java - How to filter a backspace keyboard input -

java - Show Soft Keyboard when EditText Appears -