r - How to highlight a point of interest when using manipulate()? -


i'm trying use interactive plots generated r show students how changing rise , run affect line's slope. i've done following code, i'd find way highlight points (x_1, y_1), (x_2, y_2) well:

manipulate(    plot(x<-seq(from = -3,           = 3,            = .02),     y=(y_2-y_1/(x_2-x_1))*x,     type = "l",     ylim = c(-5,5),     panel.first = grid()),  y_2=slider(min = -3, 3, initial = 1, step = .5), y_1=slider(min = -3, 3, initial = 0, step = .5), x_2=slider(min = -3, 3, initial = 1, step = .5), x_1=slider(min = -3, 3, initial = 0, step = .5) ) 

i appreciate can offer

the first argument manipulate() expression (re)evaluated every time parameters changed. can make braced block evaluate multiple statements, allow drawing additional points after main plot() call. here's how this:

library('manipulate'); manipulate(     {         x <- seq(-3,3,.02);         y <- y_2-y_1/(x_2-x_1)*x;         plot(x,y,type='l',ylim=c(-5,5),panel.first=grid());         col <- 'red';         points(x[c(1,length(x))],y[c(1,length(y))],pch=21,cex=1.5,col=col,bg=col);     },     y_2=slider(-3,3,1,step=.5),     y_1=slider(-3,3,0,step=.5),     x_2=slider(-3,3,1,step=.5),     x_1=slider(-3,3,0,step=.5) ); 

plot-with-points

(see answer what "{" class in r? if you're interested in more insight meaning of braced blocks in r language.)


Comments