0

I am trying to create an app using ShinyR, and am using ggplot to create some dynamic graphs. I have used :

annotate("text", -Inf, Inf, label = "Label",
         hjust = 0, vjust = 1, col = "red", fontface = "bold")

to create some wording in the top corner of my graph. I positioned it absolutely because the height/width of the graph changes with user input. This text is sometimes hard to read from the graph so I thought of 2 ways to make it easier to read, neither of which I have been able to implement:

  1. Put a box around the text.
    • Problem: I don't know how to absolutely position a box in ggplot.
  2. Move the text underneath the graph
    • Problem: when I try this I into issues because the label uses some information from the code found in renderPlot, which I don't know how to access with a renderText.

The red text, which is absolutely positioned, is what I'm hoping to make more visible with a box: The red text, which is absolutely positioned, is what I'm hoping to make more visible with a box

Z.Lin
  • 28,055
  • 6
  • 54
  • 94
eturv
  • 1
  • 1

2 Answers2

0

I think this solution would work for you: R: How can I annotate a ggplot with a text box?

Alternatively, you can try:

annotate("rect", xmin = (value), xmax = (value), ymin = (value), ymax = (value), alpha = (opacity))
  • Thank you for the reply! However, while this would work with a static graph I'm not sure how to make it stay in the upper corner as the dimensions of my graph change with user input (what values would I put as xmin, xmax etc.?) – eturv Aug 18 '18 at 02:06
  • I wonder if there is a way to get the chart size from R and use that as a variable in the options for annotate(). – Parker Quinn Aug 19 '18 at 20:07
0

Got it!

A bit of a hassle, but this seems to do it...

words <- qplot(1:10,1:10,geom="blank")+annotate("text", x=5, y=9, label= paste("Rejection Region: X <=", rejreg/input$n),col="red",fontface="bold")+annotate("text",x=5,y=7,label=paste("Hypothesized Proportion of Reps:", min*input$reps, "/", input$reps, "=", round(min,2)),col="red",fontface="bold")+annotate("text",x=5,y=5,label=paste("Alternaitve Proportion of Reps:", altmin*input$reps, "/", input$reps, "=", round(altmin,2)),col="red",fontface="bold")+
      theme(axis.title.x=element_blank(),
            axis.text.x=element_blank(),
            axis.ticks.x=element_blank(),
            axis.title.y=element_blank(),
            axis.text.y=element_blank(),
            axis.ticks.y=element_blank(),
            panel.grid.major = element_blank(),
            panel.grid.minor = element_blank(),
            panel.border = element_blank(),
            panel.background = element_blank())

The words in the image of the question now appear beneath the plot.

eturv
  • 1
  • 1