Graphics in R
Posted on Nov 11, 2012 in Programming
Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives.
** Things under legendu.net/outdated are outdated technologies that the author does not plan to update any more. Please look for better alternatives. **
-
par("din")
gets the dimension (i.e. width and height) of the graphics device. If no graphics device has been open, it opens a new one. -
To force a plot to have a fixed aspect ratio, you can use option
asp=aspRatio
in plotting function. For example,plot(x, y, asp=1)
makes a scatter plot with aspect ratio 1. This is helpful when you do not want a plot to get stretched. -
The function
title
adds titles and axes labels for an existing plot, so if you forget to specify a title or axes labels when you use produce a plot, you can usetitle
to add them into the plot instead of producing a new plot (sometimes it is very time-consuming to produce a plot, e.g., when you analyze gene data.).legend
adds legends for an existing plot. For example, see the code which produces the following image (need to add title).
-
Functions
pdf
andpostscript
starts the graphics device driver for producingpdf
andps
graphics respectively, which are vectorgraph. Similarly,jpeg
,bmp
,png
andtiff
start graphics device driver for producingjpeg
,bmp
,png
andtiff
graphics, which are bitmaps. -
There are several ways to divide a window device to sub window devices (to make plots on them). First, you can use
par
, e.g. to divide a window device to \(2\times3\) sub window devices, you can usepar(mfrow=c(2,3))
orpar(mfcol=c(2,3))
. The difference between them is the first one display plots by rows while the second one display plots by columns. Second, you can uselayout{graphics}
, e.g. to divide a window device to two parts vertically and then further divide the bottom part into two horizontal parts, you can uselayout(matrix(c(1,2,1,3)))
. Same values in the matrix stand for the same area, and they also stand for the order they get used among all the sub window devices. Third, you can usesplit.screen
, e.g., to achieve what we did usinglayout
, you can use the following command
split.screen(c(2,1))
split.screen(c(1,2),2)
To make a plot on sub window device generated by split.screen
,
you must first active it.
If you use functions in lattice
,
you will find that these basically ignore all these 3 functions.
lattice
has its own way to split window devices.
To do this,
you have first save plots made by functions in lattice
,
and then print these plot at appropriate locations using the generic print
.
For example,
the following code make two plots using functions in lattice
,
and then using print
to display them vertically.
px1 = histogram(rnorm(1000))
px2 = bwplot(rnorm(1000))
# arrange the 2 plots vertically
print(px1, position=c(0, 0.6, 1, 1), more=TRUE)
print(px2, position=c(0, 0, 1, 0.4))
The first 2 values and the last 2 (3rd and last) values of the option position
stand for x and y coordinates of the bottom-left and top-right corner,
respectively,
of the panel to display the corresponding plot.
Generally speaking,
par
is most easy one to use,
but it can only divide a window device into rectangular sub window devices,
and you must make plots in the right order for them to show up at the right places;
layout
is also easy to use.
You can divide a window device into irregular shapes,
but still you have to make plots in the right order
to make them to show up at the right place;
split.screen
is not as convenient as par
and layout
,
but you can divide a window device into irregular sub window devices and
make plot on any sub window device (by calling screen
first) whenever you want;
perhaps it is most inconvenient to use lattice
to achieve the same purpose.
You must use functions in lattice
,
and you have to calculate positions for each sub window device
and pass it to print
.
But lattice
has some unique plotting functions
and it uses more advanced technologies,
you can reuse saved plots instead of replotting them every time.
This is similar to how Mathematica handles graphics.
My suggestion is that you never use lattice
for this kind of job unless necessary.
-
Function
plot
is a generic function (actually many functions in R are generic functions), which be applied to many different types of objects in R. -
Function
boxplot
can make plot of different data sets without using function par(). -
Into order to recover to the default graph setting, we can save the information of
par
first and reuse it whenever we want to go back to the default settings. -
points
add points to a graph;segments
add segments to a graph. Though these job can be done byplot
, these low level functions are still useful. -
Mathematica symbols can be used in title and labels of plots using the
expression
function. For example,
plot(1, xlab=expression(alpha+beta))
-
We can use math expression when plot a graph. To do this, we just need specify the corresponding label as
expression(expr)
, where "expr" is an appropriate expression in R. For example, if we want to use Greek symbol \(\alpha\), we can specify "xlab=expression(alpha)". Whenver we forget how to write mathematica expressions, we can type in command "demo(plotmath)" which will display corresponding expression in R for all kinds of math expressions. -
sna::gplot.arrow
makes neat arrows. -
plot(..., type = "h")
gives us needle plot -
chull
computes convex hull of a given set. -
dev.copy2pdf
copies the graphics content on the current graphics device to a pdf file. -
You'd better use pdf with dev.off() to make plots, this is a universal way ...
avoid problems when working on remote servers