R - Plot Only Text

26,397

Solution 1

You can do this using base graphics. First you'll want to take away all of the margins from the plot window:

par(mar = c(0,0,0,0))

And then you'll plot an empty plot:

plot(c(0, 1), c(0, 1), ann = F, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')

Here's a guide to what's going on here (use ?plot.default and ?par for more details):

  • ann - Display Annotoations (set to FALSE)
  • bty - Border Type (none)
  • type - Plot Type (one that produces no points or lines)
  • xaxt - x axis type (none)
  • yaxt - y axis type (none)

Now to plot the text. I took out the extra spaces because they didn't seem to be necessary.

text(x = 0.5, y = 0.5, paste("The following is text that'll appear in a plot window.\n",
                             "As you can see, it's in the plot window\n",
                             "One might imagine useful informaiton here"), 
     cex = 1.6, col = "black")

enter image description here

Now to restore the default settings

par(mar = c(5, 4, 4, 2) + 0.1)

I hope that helps!

Solution 2

You could use annotate in ggplot2 like

library(ggplot2)
text = paste("\n   The following is text that'll appear in a plot window.\n",
         "       As you can see, it's in the plot window\n",
         "       One might imagine useful information here")
ggplot() + 
  annotate("text", x = 4, y = 25, size=8, label = text) + 
  theme_void()

enter image description here

And you can of course remove the plot margins, axes, etc. to have just the text

Solution 3

Here's a handy example to play with too:

par(mar = c(0,0,0,0))
plot(c(0, 1), c(0, 1), ann = F, bty = 'n', type = 'n', xaxt = 'n', yaxt = 'n')

text(x = 0.34, y = 0.9, paste("This is a plot without a plot."), 
     cex = 1.5, col = "black", family="serif", font=2, adj=0.5)

text(x = 0.34, y = 0.6, paste("    Perhpas you'll:"), 
     cex = 1.2, col = "gray30", family="sans", font=1, adj=1)
text(x = 0.35, y = 0.6, paste("Find it helpful"), 
     cex = 1.2, col = "black", family="mono", font=3, adj=0)

enter image description here

Solution 4

Read up on ?par . There is limited capability to select the font type via the family and font arguments.

Share:
26,397

Related videos on Youtube

EconomiCurtis
Author by

EconomiCurtis

Updated on August 15, 2020

Comments

  • EconomiCurtis
    EconomiCurtis almost 4 years

    Curious how one might create a plot with only text information. This will essentially be a "print" for the plot window.

    The best option I've found so far is the following:

      library(RGraphics)
      library(gridExtra)
    
        text = paste("\n   The following is text that'll appear in a plot window.\n",
               "       As you can see, it's in the plot window",
               "       One might imagine useful informaiton here")
        grid.arrange(splitTextGrob(text))
    


    enter image description here


    However, one doesn't have control (as far as I can tell) over font type, size, justification and so on.

    • baptiste
      baptiste over 10 years
      you don't need grid.arrange or gridExtra, simply grid.draw()
  • EconomiCurtis
    EconomiCurtis over 10 years
    Also helpful, here's a link to tips to implement different font/typefaces here too: statmethods.net/advgraphs/parameters.html - Thanks for the tip!
  • EconomiCurtis
    EconomiCurtis over 10 years
    Also, to add info on right and left justify in the text(...) function: "adj = 1" => left justify, "adj = 0" => right justify, "adj = NULL" (the default) => center
  • EconomiCurtis
    EconomiCurtis over 10 years
    Re Center Adjustment, I was mistaken above. Center adjustment is "adj = 0.5"