This may be a naive question, but I was wondering if there's a better way than using text()
to adding text to a plot. Note, I'm also using layout()
as well. Specifically, I have a section of a plot where I would like to add some text with headings followed by regular text.
text()
is fine it seems for simple annotations, but to get the spacing right for several lines of text seems to require a lot of manual manipulation of the x
and y
and cex
parameters. Any suggestions?
R – text () Function text () Function in R Programming Language is used to draw text elements to plots in Base R. Parameters: x and y: numeric values specifying the coordinates of the text to plot. labels: the text to be written.
To add text to one point, specify x and y as scalars. To add text to multiple points, specify x and y as vectors with equal length. text( x , y , z , txt ) positions the text in 3-D coordinates. text(___, Name,Value ) specifies Text object properties using one or more name-value pairs.
Create Text Box AnnotationCreate a simple line plot and add a text box annotation to the figure. Specify the text description by setting the String property. Force the box to fit tightly around the text by setting the FitBoxToText property to 'on' .
Here are some alternative options to consider:
- the gplots package has a textplot
function to add some text output in a base graphics plot.
- plotrix has a function addtable2plot
- for grid graphics grid.text()
is available and in gridExtra there is a function grid.table()
(see, e.g., R-Wiki)
If you're using base graphics, then text()
is probably your best bet, and fiddling with coordinates etc is part of the game. If you want to learn a new framework, the lattice
package is a reworking of the basic approach to plotting in R. It be installed by default so help(package='lattice')
will get you started.
Here's a pretty good guide (pdf) to graphics in general in R, with a substantial section on lattice
:
download
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With