In R is there any way to produce plots which have no title and which use the space the title would otherwise have taken up?
In plot()
, main
, sub
, xlab
, and ylab
all default to NULL
, but this just leaves blank space where they would have been, ditto for setting them to ''. It would be nice if not including them meant that the entire plot space was utilized rather than leaving extra empty space on the edges. This is all especially relevant in printing plots to file devices like pdf()
, png()
, etc.
When we create a plot in R, the Y-axis labels are automatically generated and if we want to remove those labels, the plot function can help us. For this purpose, we need to set ylab argument of plot function to blank as ylab="" and yaxt="n" to remove the axis title.
Use the title( ) function to add labels to a plot. Many other graphical parameters (such as text size, font, rotation, and color) can also be specified in the title( ) function. # labels 25% smaller than the default and green.
Directly by specifying the titles to the plotting function (ex : plot() ). In this case titles are modified during the creation of plot. the title() function can also be used. It adds titles on an existing plot.
To set labels for X and Y axes in R plot, call plot() function and along with the data to be plot, pass required string values for the X and Y axes labels to the “xlab” and “ylab” parameters respectively. By default X-axis label is set to “x”, and Y-axis label is set to “y”.
See tip 7 about adjusting the margins.
Excerpt:
To remove the space reserved for labels, use par(mar=...). For example
png(file="notitle.png",width=400, height=350) par(mar=c(5,3,2,2)+0.1) hist(rnorm(100),ylab=NULL,main=NULL) dev.off()
If you're willing to entertain an alternate plotting package, ggplot2 does this automatically when you set xlab
/ylab
to NULL
(and there is no plot title/main
by default). For simple plots, just require(ggplot2)
and replace plot
by qplot
.
Really, ggplot2 is the most fun I've had with plotting in years and I can't resist the opportunity to evangelize it to everyone I meet. :-)
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