Search code examples
rgplots

How do i colour the max value on a plot() in R?


plot(1:4,dbinom(1:4,4,0.7),
     type='h',
     main='Binominal Distribution (n=4, p=0.7',
     ylab='Probability',
     xlab='# Successes',
     lwd=3,xaxt="n")

This is my plot and i want to colour only the highest bar, but i don´t know how to associate the max() to the col = " "


Solution

  • This should work:

    y <- dbinom(1:4,4,0.7)
    plot(1:4,y,
         type='h',
         col = ifelse(y == max(y), "red", "black"), 
         main='Binominal Distribution (n=4, p=0.7',
         ylab='Probability',
         xlab='# Successes',
         lwd=3,xaxt="n")
    

    enter image description here