Font in R plot

Many high level plotting functions (plot, hist, boxplot, etc.) allow you to include axis and text options (as well as other graphical paramters).

three key parameters:

font eg font.main=20, font.lab=20
col eg col.main=”red”, col.lab=”green”
cex eg cex.main=2, cex.lab=0.75

For example

# Specify axis options within plot()
plot(x, y, main=”title”, sub=”subtitle”,
xlab=”X-axis label”, ylab=”y-axix label”,
xlim=c(xmin, xmax), ylim=c(ymin, ymax))

For finer control or for modularization, you can use the functions described below.

Use the title( ) function to add labels to a plot.

title(main=”main title”, sub=”sub-title”,
xlab=”x-axis label”, ylab=”y-axis label”)

Many other graphical parameters (such as text size, font, rotation, and color) can also be specified in the title( ) function.

# Add a red title and a blue subtitle. Make x and y
# labels 25% smaller than the default and green.
title(main=”My Title”, col.main=”red”,
sub=”My Sub-title”, col.sub=”blue”,
xlab=”My X label”, ylab=”My Y label”,
col.lab=”green”, cex.lab=0.75)
Text Annotations

Text can be added to graphs using the text( ) and mtext( ) functions. text( ) places text within the graph while mtext( ) places text in one of the four margins.

text(location, “text to place”, pos, …)
mtext(“text to place”, side, line=n, …)

Common options are described below.
option description
location location can be an x,y coordinate. Alternatively, the text can be placed interactively via mouse by specifying location as locator(1). pos position relative to location. 1=below, 2=left, 3=above, 4=right. If you specify pos, you can specify offset= in percent of character width. side which margin to place text. 1=bottom, 2=left, 3=top, 4=right. you can specify line= to indicate the line in the margin starting with 0 and moving out. you can also specify adj=0 for left/bottom alignment or adj=1 for top/right alignment.

Other common options are cex, col, and font (for size, color, and font style respectively).
Labeling points

You can use the text( ) function (see above) for labeling point as well as for adding other text annotations. Specify location as a set of x, y coordinates and specify the text to place as a vector of labels. The x, y, and label vectors should all be the same length.

# Example of labeling points

png (“font-plot-r.png”, width=600)
t<-paste (“pt col “,xx, sep=””)
plot(x,y, main=”Font in R plot”, pch=xx,col=xx, xlim=c(1,5)) text(x, y, t,pos=4, col=xx,font=20, cex=1)

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s