Plots without titles/labels in R - r

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.

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. :-)

With lattice, it's just a matter of setting the xlab, ylab, and main arguments to NULL:
library(lattice)
bwplot(rnorm(100),xlab=NULL,ylab=NULL,main=NULL)

plot(anything, main=NULL)
Still works.

I usually use
par(mar=c(1,1,1,1))
when I keep the border to a minimum.

Related

What command to use to zoom out a plot in R?

I have a barplot and the names for the bars are all vertical on the x-axis. However, the names are fairly long and don't show completely in the plot. Is there any command I can use to zoom out the plot? I have already tried the package "zoom" and zm() but it doesn't work.
One way to handle this is to increase the margin size before plotting. Here is a simple example. First, to show the problem:
LongNames = c("RatherLongName_1", "RatherLongName_2",
"RatherLongName_3", "RatherLongName_4")
barplot(1:4, names.arg=LongNames, las=3)
Then with the margins adjusted.
par(mar=c(10,4,4,2))
barplot(1:4, names.arg=LongNames, las=3)

R ylab line modified with italic

I have trouble making my plots looking similar because my labels (both xlab and ylab) move if I use italic. Consider the following short example:
plot(1:10,cex.lab=1.25,ylab="p-value")
plot(1:10,cex.lab=1.25,ylab=expression(italic(p)~"-value"))
The problem is that "p-value" is slightly nearest from the axis than "p-value". I believe it's because the p tail defines the writing line which is considered different without italic. How can I fix that easily?
I frequently use both strings and expressions for my plots and that would be complicated to use mtext (with the line argument) to manage each label for each plot.
I found a workaround playing with unicode characters.
p is \U1D631 and P is \U1D617
plot(1:10,cex.lab=1.25,ylab="p-value")
plot(1:10,cex.lab=1.25,ylab="\U1D631-value")
This solved my problem.

R Make barchart with unicode symbol instead of bars

I'd like to plot a (kind of) barchart, but rather than using bars, I'd like to use a unicode symbol. Is this possible?
I know how to use a unicode character as a symbol (points); for example:
plot(1,1,pch=-0x0001F3E2, cex=5, col="gray30")
but how can make the symbol substitute the bars?
EDIT:
As suggested by Eric, I'll try to write a minimal reproducible example...
Given variable x:
x <- c(2,3,4,2,1)
barplot(x)
I want to produce a barplot using custom symbols rather than bars. Something like:
plot(1:5,rep(0,5),pch=-0x0001F3E2, cex=x, col="gray30")
The problem is that I am not able to vertical align sysmbols as I can do with text:
plot(1:5,rep(0,5),type="n", ylim=c(0,1),bty="n")
text(1:5,0,"A",cex=c(2,5,3,3,1),adj=c(0.6,0))
So, a sort of solution to my problem, although not ideal, is to use
some custom system font with package extrafont.
For example, using font "Destiny's Little Houses" (http://www.dafont.com/destiny-little-hous.font)
I am able to produce some nice infographics:
plot(1:5,rep(0,5),type="n",ylim=c(0,1),bty="n")
text(1:5,0.1,"A",cex=c(2,5,3,3,1),adj=c(0.6,0),family="Destiny's Little Houses")
So, rather than having bars I have an image whose size changes according to some variable (using "cex").
This is not ideal, first of all because I'd like to be able to import any image (ex. using the png package).
So, is it possible to:
import an image file and use it as the symbol in plot (rather than one of the available sysmbols using pch)?
Vertical align symbols so that I get a kind of custom barchart?
Thanks
António
Here's a somewhat hackish start (might not be helpful as I don't know what your data looks like).
counts <- table(mtcars$gear)
foo <- barplot(counts, col = c("white", "white", "white"), border=c("white"), axes=F, xaxt="n")
points(foo, counts-.5, pch = -0x0001F3E2, cex=2, pos=3)
Remove , axes=F, xaxt="n" to get the labels and axis back.

Is it possible to control the whitespace of exported pdf in ggplot

When I export my plots with ggplot I get a lot of whitespace, especially when I use top arrangement for a legend.
When writing scientific paper the page limits are very strict and wasting one, two, three lines on the whitespace of a plot is really costly.
Is it possible to tweak the whitespace around plots saved with ggsave()?
I came to know that this whitespace is also known as "bleed" in design terminology, if that helps anyone...
The same is also visible here:
Add theme() with parameters to your plot. Some parameters that might be useful are plot.margin and legend.margin.

barplot design issues

I am doing a barplot of 14 columns to represent some data, I set the names.arg option to show as vertical lables, unfortunately, this caused the new vertical lables to overlap with the "sub" and "xlab" options I have. How do I prevent this from happening?
Here's my command:
par(mar=c(6, 5, 4,7.5 ))
barplot(x, main=paste("title1 \n","subtitle"),
names.arg=c(1,2,3,4,5,6,7,8,9,10,11,12,13,14),las=2, sub=("overlapping text1"),
xlab="overlapping text2", col = c("red2","darkorange3"))
Another question on my mind is, I am using a 2-line title in "main" as you can see from the command. Is it possible to make the second line font smaller, whilst keeping the first line in the same format?
Thanks,
One solution to change font size for one of titles, is to use two calls of functions mtext() in different lines with different cex= values and remove main= from barplot(). To overcome problem with overlapping text, mtext() also can be used instead of xlab= and sub=. You just have to find the right line= and increase space around plot with par(mar=..).
x<-sample(letters[1:14],300,replace=TRUE)
par(mar=c(9,3,5,2))
barplot(table(x),
names.arg=paste0("very_long_",1:14),las=2,
col = c("red2","darkorange3"))
mtext(side=3,"Title1",line=2,cex=2)
mtext(side=3,"subtitle",line=1,cex=1.5)
mtext(side=1,"overlapping text1",line=6)
mtext(side=1,"overlapping text2",line=7)
Another option to look at is the staxlab function in the plotrix package.
Also look at the mgp argument to the par function for a way to set the default placement of the axis title.

Resources