Adding Text to R Plot

[This article was first published on DataScience+, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Diversity is a real strength. By now it is common knowledge. I often see institutions openly encourage multinational environment and multidisciplinary professionals, with specific “on-the-job” training to tailor for own needs. No one knows a lot about a lot, so bringing different together enhance independent thinking and knowledge available to the organization. Clarity of communication then becomes even more important, and making sure your figures are quickly understandable goes a long way.

Same-field quickly understand what others from the same field are doing simply by reading few formulas or looking at few charts, but different-field needs longer to digest. One thing we can do is use available spaces to better explain what is it that we see in a chart, by that speeding communication and sharing. I have created a concise summary of all (most?) available options.

The code and relevant functions below.

# Split the screen using the layout function
layout(matrix(c(1,1,2,2), 2, 2, byrow = T), heights = c(4,1))

# plot the figure
plot(rnorm(10),ylab="", xlab="Use the xlab argument", main="Use the title", ty="o", pch=19)

# Add text
mtext("Use the mtext() function")
temp <- locator(1) # On the chart, click where you would like the text to appear
text(temp,"Use the text() with or without the locator() function")

# Add text using the following Corner_text function:
Corner_text <- function(text, location="topright"){
legend(location,legend=text, bty ="n", pch=NA) 
}
Corner_text(text="Use Corner_text() function")
Corner_text(text="Use the Corner_text() function",location= "bottomright")

# use the title function 
title(sub="Add subtitle using the title() function")

# load a library for creating more text
library(gplots)

# ?textplot # use this for more information about the function

# layout.show(2) # show the split between top and bottom screens
temptext1 <- "Add here notes. The plot above represents 10 random points drawn from Normal distribution. The plot is generated in order 
to visualize all the different options one can use for adding text to a plot. 
You can control this text size using the usual "cex" argument.
nYou can increase readability if you skip lines. 
nUse this space when you have complicated charts that require some more details. Add explanations and preemt possible questions, by that 
saving readers' time (e.g. "is it percentage or basis points?" )."
temptext2 <-  "You can also add more text in the usual way by using again the locator(1)+text() functions" 
textplot(temptext1, valign="center", cex=0.8, halign= "left", mar=c(0,0,0,0), col=2) 

# mar=c(0,0,0,0) removes the margins
temp <- locator(1)
text(temp, temptext2,col=4)

Here is the output of the code above (click on the image to better see the result).
Addit_text_to_R_plot

If you have any question, please feel free to ask by leaving a comment below.

To leave a comment for the author, please follow the link and comment on their blog: DataScience+.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)