Open In App

Plot Only Text in R

Last Updated : 08 Oct, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will discuss how to plot only text in the R programming language.

Method 1: Using par() function with the mar argument

In this approach to plot only text, the user needs to call the in-built function par function with the mar argument to simply plot the empty plot in the R programming language and then call the text() function to write the text on the empty plot created by par function with the mar argument in the R language.

Syntax: par(mar, mgp, las)

Parameters:

  • mar – A numeric vector of length 4, which sets the margin sizes in the following order: bottom, left, top, and right.
  • mgp – A numeric vector of length 3, which sets the axis label locations relative to the edge of the inner plot window.
  • las – A numeric value indicating the orientation of the tick mark labels and any other text added to a plot after its initialization.

Example:

R




par(mar = c(0, 0, 0, 0))
 
plot(x = 0:10, y = 0:10, ann = F,bty = "n",type = "n",
     xaxt = "n", yaxt = "n")
 
text(x = 5,y = 5,"Knowledge is power!\nCome with us and
contribute your knowledge about Computer Science \into our world
of Geeks!\n~GeeksForGeeks")


Output:

Method 2: Using annotate() and theme_void() functions of ggplot2 

In this approach to plotting only text, the user first needs to install and import in the R console and call annotate() and theme_void() functions of ggplot2 package with the required parameters in the R programming language to add the only text in the plot in the R programming language.

annotate() function adds geoms to a plot, but unlike typical a geom function, the properties of the geoms are not mapped from variables of a data frame but are instead passed in as vectors. 

Syntax: annotate(geom,x = NULL,y = NULL,xmin = NULL,xmax = NULL,ymin = NULL,ymax = NULL,xend = NULL, yend = NULL,…,na.rm = FALSE)

Parameter:

  • geom: name of geom to use for annotation
  • x, y, xmin, ymin, xmax, ymax, xend, yend: positioning aesthetics – you must specify at least one of these.
  • …: Other arguments passed on to layer(). These are often aesthetics, used to set an aesthetic to a fixed value, like colour = “red” or size = 3.
  • na.rm: If FALSE, the default, missing values are removed with a warning. If TRUE, missing values are silently removed.

theme_void() function is a completely empty theme, useful for plots with non-standard coordinates or for drawings.

Example:

R




library("ggplot2"
 
ggplot() +
  annotate("text", x = 10,  y = 10,
           size = 6,
           label = "Knowledge is power!\nCome with us and
contribute your knowledge about Computer Science into our
world of Geeks!\n~GeeksForGeeks") + theme_void()


Output:



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads