0

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

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

5 Comments

how do I use this in R Shiny? how can I simply return one object, aka this plot, in a reactive function? in your code, each line builds upon each other, so I don't know what to do...
@sweetmusicality in renderPlot, you don't have to return "one object"
don't you have to only return one object though?
nvm, it works - thanks a bunch! quick question - how do I remove all the excess white space around the plot? I have updated my question to show you what I mean
@sweetmusicality Start with plot(0, 0, xlim=c(0,10), ylim=c(0,5)) and adapt xlim and ylim if needed.
1

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

4 Comments

where do I specify for it to be "0.35" of the way in? bc right now your "0.35" is almost "0.66" and I tried other values in your code in your arrows function, and couldn't figure out where to change it
thanks :) one last question - how do I add the value underneath, or at least near, the arrow?
try text(0.35, 0.1, labels = '0.35', cex = 0.8)
thanks! how can I remove the white space from the image (see edited question: your plot also leads to the same issue)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.