Home > Mobile >  plot lines : How do I put the plot over the line in a different color?
plot lines : How do I put the plot over the line in a different color?

Time:04-29

Plot on the left:

Hi, I would like to put the line which is in black right now in a different color when the black line is above the red one.

 if (input$loi0=="norm" & input$loi1=="norm"){
     
    rmelange = function (n, alpha, l0, l1, p0, p1) {
      z = rbinom(n, 1, alpha)
      f1 = eval(parse(text = paste('r', l1, '(', paste(c(n, p1), collapse = ','), ')', sep = '')))
      f0 = eval(parse(text = paste('r', l0, '(', paste(c(n, p0), collapse = ','), ')', sep = '')))
      x = z * f1   (1 - z) * f0
      return (x = x)
    }
    dmelange = function (t, alpha, l0, l1, p0, p1) {
      res = alpha * eval(parse(text = paste('d', l1, '(t,', paste(p1, collapse = ','), ')', sep = '')))   (1 - alpha) * eval(parse(text = paste('d', l0, '(t,', paste(p0, collapse = ','), ')', sep = '')))
    }
    x = rmelange(input$n, input$alpha, input$loi0, input$loi1, c(input$y2p0, input$y2p00), c(input$y2p1, input$y2p11))
    uii = dmelange(seq(-35, 35, length.out = input$n), input$alpha, input$loi0, input$loi1, c(input$y2p0, input$y2p00), c(input$y2p1, input$y2p11))

    plot(density(x, bw = input$bw, kernel = input$kernel), ylim = c(0, 0.7), main="Densite donne par notre estimateur contre la veritable densite", cex.main=0.99)
        lines(seq(-35, 35, 0.001), dmelange(seq(-35, 35, 0.001), input$alpha, input$loi0, input$loi1, c(input$y2p0, input$y2p00), c(input$y2p1, input$y2p11)), col = 'red')

Do you have any ideas ? It should be a pretty simple code according to my teacher.

CodePudding user response:

Without having your data it is hard to reproduce your problem, but it seems like you can just assign col="blue" for example in your plot function like this:

if (input$loi0=="norm" & input$loi1=="norm"){
  
  rmelange = function (n, alpha, l0, l1, p0, p1) {
    z = rbinom(n, 1, alpha)
    f1 = eval(parse(text = paste('r', l1, '(', paste(c(n, p1), collapse = ','), ')', sep = '')))
    f0 = eval(parse(text = paste('r', l0, '(', paste(c(n, p0), collapse = ','), ')', sep = '')))
    x = z * f1   (1 - z) * f0
    return (x = x)
  }
  dmelange = function (t, alpha, l0, l1, p0, p1) {
    res = alpha * eval(parse(text = paste('d', l1, '(t,', paste(p1, collapse = ','), ')', sep = '')))   (1 - alpha) * eval(parse(text = paste('d', l0, '(t,', paste(p0, collapse = ','), ')', sep = '')))
  }
  x = rmelange(input$n, input$alpha, input$loi0, input$loi1, c(input$y2p0, input$y2p00), c(input$y2p1, input$y2p11))
  uii = dmelange(seq(-35, 35, length.out = input$n), input$alpha, input$loi0, input$loi1, c(input$y2p0, input$y2p00), c(input$y2p1, input$y2p11))
  
  plot(density(x, bw = input$bw, kernel = input$kernel), ylim = c(0, 0.7), main="Densite donne par notre estimateur contre la veritable densite", cex.main=0.99, col = "blue")
  lines(seq(-35, 35, 0.001), dmelange(seq(-35, 35, 0.001), input$alpha, input$loi0, input$loi1, c(input$y2p0, input$y2p00), c(input$y2p1, input$y2p11)), col = 'red')

Here I give you a simple example using the iris data:

plot(density(iris$Sepal.Length), col = "blue")
lines(density(iris$Sepal.Width), col = "red")

Output:

enter image description here

As you can see the lines are colored.

CodePudding user response:

the data are produced in the rmelange and dmelange function, its a mix between two random law like 2 normal laws with differents parameters of mean and standard deviation. I don't want the plot to be blue I just want the part where the black line is above the red line like in this picture. [1]: https://i.stack.imgur.com/eOfZQ.png

  • Related