Multi-panel titles in R - r

I have an ultra short question about R
My aim is to assign a common title to a multi-panel plot generated using par, e.g.
par(mfrow=c(1,2))
plot(rnorm(1000))
plot(rnorm(1000))
So, something like "main" for the plot function, but extended to both plots. Is there a canonical way to do this?
Thanks for any answer :-)

Use mtext with option outer:
set.seed(42)
oldpar <- par(mfrow=c(1,2), mar=c(3,3,1,1), oma=c(0,0,3,1)) ## oma creates space
plot(cumsum(rnorm(100)), type='l', main="Plot A")
plot(cumsum(rnorm(100)), type='l', main="Plot B")
mtext("Now isn't this random", side=3, line=1, outer=TRUE, cex=2, font=2)
par(oldpar)

Related

Overlapping histograms on one page in R

I am a beginner with R. I managed to plot my data into overlapping histograms. However, I would like to place all the histograms on one page. I am struggling as I am not able to tell R, which sets to pick (only manage to plot one of the plots).
This is the code:
df<-read.csv("Salt dshalo sizes.csv",header=T)
#View(df)
library(ggplot2)
DSA<-df[,1]
DS1<-df[,2]
DSB<-df[,5]
DS2<-df[,6]
DSC<-df[,9]
DS3<-df[,10]
#remove the NA column by columns separately or it will chop the data
DSA=na.omit(DSA)
DS1=na.omit(DS1)
DSB=na.omit(DSB)
DS2=na.omit(DS2)
DSC=na.omit(DSC)
DS3=na.omit(DS3)
#plot histograms for DSA, DSB and DSC on one same graph
hist(DSA, prob=TRUE, main="Controls", xlab="Sizes (um)", ylab="Frequency", col="yellowgreen",xlim= c(5,25), ylim=c(0,0.5), breaks=10)
hist(DSB, prob=TRUE, col=rgb(0,0,1,0.5),add=T)
hist(DSC, prob=TRUE, col=rgb(0.8,0,1,0.5),add=T)
#add a legend to the histogram
legend("topright", c("Control 1", "Control2", "Control3"), text.width=c(1,1,1),lwd=c(2,2,2),
col=c(col="yellowgreen", col="blue", col="pink",cex= 1))
box()
#plot histograms for DS1, DS2 and DS3 on one same graph
hist(DS1, prob=TRUE, main="Monoculture Stressed", xlab="Sizes (um)", ylab="Frequency", col="yellowgreen",xlim= c(5,25), ylim=c(0,0.5), breaks=10)
hist(DS2, prob=TRUE, col=rgb(0,0,1,0.5),add=T)
hist(DS3, prob=TRUE, col=rgb(0.8,0,1,0.5),add=T)
#add a legend to the histogram
legend("topright", c("DS1", "DS2", "DS3"), text.width=c(1,1,1),lwd=c(2,2,2),
col=c(col="yellowgreen", col="blue", col="pink",cex= 1))
box()
# put both overlapping histograms onto one page
combined <- par(mfrow=c(1, 2))
plot(hist(DSA),main="Controls")
plot(hist(DS1),main="Monoculture stressed")
par(combined)
Basically, I get two separate overlapping histograms, but cannot put them on the same page.
EDIT: I evidently didn't read your question thoroughly. I see you figured out the add =T.
I assume what you are looking for then is the comment I made first:
par(mfrow = c(a,b)) where a and b are the number of rows and columns you want the graphics objects to be printed. I used c(2,2) for this pic.
I made a comment, but sounds like you may be talking about the add=T option.
a=rnorm(100, 2, 1)
b=rnorm(100, 4, 1)
hist(a, xlim=c(0,10), col="yellow")
hist(b, add=T, col="purple" )
you can play around with transparency options on colors to see both overlap. Such as rgb(1,0,0,1/4) as the color.
With transparency colors:
a=rnorm(100, 2, 1)
b=rnorm(100, 4, 1)
hist(a, xlim=c(0,10), col=rgb(1,1,0,1/4))
hist(b, add=T, col=rgb(1,0,0,1/4) )

Removing axes in beeswarm plot

I have a following "beeswarm" (a single-dimensional scatterplot)
library(beeswarm)
data(breast)
beeswarm(breast$time_survival,horizontal=TRUE)
Here is the resulting plot:
How can I get rid of the axes and the box around the plot, so that I can reintroduce only the X axis and nothing else around it?
If you create an empty plot first
plot(rnorm(10), type="n", axes=FALSE, xlim=c(0, 200), ylim=c(0.4, 1.6),
xlab="", ylab="")
Then you can use the add argument to get what you want
beeswarm(breast$time_survival,horizontal=TRUE, add=TRUE)
You can use the "axes" argument (described in ?plot.default).
beeswarm(breast$time_survival, horizontal=TRUE, axes = FALSE)

Combining paste() and expression() in plot tick mark labels

Consider this example:
labs <- c("AT~frac(T,C)~G","GGAA","TTAA","AAAA")
plot(c(1:4), c(1:4), axes=F, xlab="",ylab="", par(mar=c(8,3,2,1)))
axis(1, at=c(1:4), labels=labs, las=2)
that generates this:
My intention is to have something like this:
that I hardcoded as:
plot(c(1:4), c(1:4), type="n", axes=F, xlab="",ylab="")
axis(1, at=c(1:4), labels=c(expression(AT~frac(T,C)~G), expression(GGAA), expression(TTAA), expression(AAAA)), las=2)
The closest answer I got was this.
Getting expression() to work as I desired is really very confusing for me. I intend to have these x-axis tick mark labels dynamically generated from available data using a vector of "expression strings".
You were close! frac() is a function that you need to call. it can be used with strings as an argument. This sample
labs <- expression(paste("AT"~frac("T","C")~"G",sep=""),"GGAA","TTAA","AAAA")
plot(c(1:4), c(1:4), axes=F, xlab="",ylab="")
axis(1, at=c(1:4), labels=labs, las=2)
generates this plot:

Adding simple legend to plot in R

I would like to create a very simple plot.
I am using this data:
a <- read.table("https://dl.dropbox.com/u/22681355/a.csv", sep=',', header=TRUE)
plot(a$X25, type="l",col="red", ylim=c(0,100))
lines(a$X25.1, type="l", col="blue")
lines(a$X25.2, type="l", col="green")
lines(a$X25.3, type="l", col="brown")
Now I would like to add a simple legend that indicates which color is which variable.
I understand that I can use the legend() command, but my problem is that I don't know how to put colors next to the text in the legend.
What's the simplest command that would do this?
Take a look at ?legend and try this:
legend('topright', names(a)[-1] ,
lty=1, col=c('red', 'blue', 'green',' brown'), bty='n', cex=.75)

R - Common title and legend for combined plots

I would like to know how to provide a common title and legend for combined plots in R. I have four plots which I have combined into one. Each plot has its own Title. I want to specify a common Title on the top center and common legend on the top left corner of the combined plot. I generated the combined plot using par(). I have provided my plot below
You can use the oma parameter to increase the outer margins,
then add the main title with mtext,
and try to position the legend by hand.
op <- par(
oma=c(0,0,3,0),# Room for the title and legend
mfrow=c(2,2)
)
for(i in 1:4) {
plot( cumsum(rnorm(100)), type="l", lwd=3,
col=c("navy","orange")[ 1+i%%2 ],
las=1, ylab="Value",
main=paste("Random data", i) )
}
par(op) # Leave the last plot
mtext("Main title", line=2, font=2, cex=1.2)
op <- par(usr=c(0,1,0,1), # Reset the coordinates
xpd=NA) # Allow plotting outside the plot region
legend(-.1,1.15, # Find suitable coordinates by trial and error
c("one", "two"), lty=1, lwd=3, col=c("navy", "orange"), box.col=NA)

Resources