[R] Background color in plots.
Marc Schwartz
mschwartz at medanalytics.com
Tue May 20 15:38:40 CEST 2003
Wolski wrote:
> How can i set the background color in a plot.
> Eg. Instead of having with i like to have a grey background.
> Eryk
For the full window background color you can set par(bg = "color") prior
to plotting. See ?par for more information.
If on the other hand you are referring to the plot region itself (ie.
within the plot box), it is a little trickier, but you can get the
coordinates of the plot area rectangle and color it by using:
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col =
"color")
In barplot2() in the gregmisc package you can set this easily for
barplots by using the 'prcol' argument. However for most other plotting
routines you would need to plot the initial graphic, color the plot
region and then re-plot (actually add) the graphic components to the
existing plot window.
A simple example using plot():
# Create the base plotting window
# type = "n" does not plot the points
# Set the background color to "yellow"
par(bg = "yellow")
plot(1:10, type = "n")
# Now set the plot region to grey
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col =
"grey")
# Now plot the points on the existing window
points(1:10)
In some of the plotting functions to do the plot region color, like
boxplot(), you would have to plot the full graphic and then replot it
after setting the plot region color. In this case, you would use the
'add = TRUE' argument in boxplot() for the second call. You could
feasibly get the information from boxplot.stats() and draw the boxes and
lines, etc., but just calling boxplot() twice is easier I think.
A quick example for boxplot():
x <- rnorm(100)
par(bg = "thistle")
boxplot(x)
rect(par("usr")[1], par("usr")[3], par("usr")[2], par("usr")[4], col =
"grey")
boxplot(x, add = TRUE, col = "blue")
They are not pretty I know, but hopefully gets the points across.
You can use colors() to get a list of the available color names.
HTH,
Marc Schwartz
More information about the R-help
mailing list