sweetmusicality
sweetmusicality

Reputation: 937

Creating a reactive rectangular plot

I am relatively new to R. I am making an R Shiny app, and based on the input of the user, I would like to analyze the data and output a bar that shows the Jaccard index. This is what I want it to look like, although obviously a smooth gradient:

enter image description here

Please note that the Jaccard index (in this case, 0.35) will change after each input, so I'd like something reactive. I just have no idea where to start or if making plots like this is even possible in R.

Thanks.

edit: I used an online gradient generator to come up with this plot instead: how could I overlay a vertical line with its corresponding Jaccard index and corresponding location on the bar on this particular image?

enter image description here

edit: I want to remove the white space before the actual plot and after my text. any ideas? enter image description here

Upvotes: 0

Views: 236

Answers (2)

Ameya
Ameya

Reputation: 1730

For something in base R, an imperfectly modified version of this solution, might work.

color.bar <- function(lut, min, max=-min, nticks=11, ticks=seq(min, max, len=nticks), title='') {
  scale = (length(lut)-1)/(max-min)
  dev.new(width=1.75, height=5)
  plot(c(min,max), c(0,10), type='n', bty='n', xaxt='n', xlab='', yaxt='n', ylab='', main=title)
  for (i in 1:(length(lut)-3)) {
    x = (i-1)/scale + min
    rect(x, 0 ,1, 30/scale, col=lut[i], border=NA)
  }
}

Then make the graph as follows -

color.bar(colorRampPalette(c("light green", "yellow", "orange", "red"))(100), 0, 1)

At this point perhaps you can add an abline(v = 0.35) to get what you want? You could even try pointing to the appropriate position using an arrow using

arrows(0.35, -1, 0.35, 0, length = 0.07, angle = 25)

enter image description here

Upvotes: 1

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

With the plotrix package:

library(plotrix)
# get an empty box
plot(0:10, type="n", axes=FALSE, xlab=NA, ylab=NA)
# rectangle filled with a gradient
gradient.rect(0, 0, 10, 5, col=smoothColors("red",38,"blue"), border=NA)
# vertical bar 
segments(3.5, 0, 3.5, 5, lwd=2)
text(3.5, 0, "0.35", pos=1, xpd=TRUE)

enter image description here

Upvotes: 1

Related Questions