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

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:

Related

R: Different colours for barplot labels

I have a barplot and would like to have the label showing different colours and the bars to be grey.
I tried:
col_lab <- c("red","green","grey","red","red","blue")
barplot(1:6,names.arg=1:6,main="barplot",las=1,horiz=TRUE,col="grey",xaxt="n",col.names=col_lab)
but I got an error.
Thank you for your help.
Use yaxt="n" and do an mtext. To get the right positions, use the coordinates barplot throws invisibly.
b <- barplot(1:6, names.arg=1:6, main="barplot", las=1, horiz=TRUE, col="grey",
xaxt="n", yaxt="n")
mtext(1:6, 2, .5, at=b, las=2, col=col_lab, font=2)

Automatically choose "line" in axis with stacked axis labels in R

Long story short, I'm creating a type of plot where on the x-axis, I have two different variables whose values are crossed with one another (in this minimal example, I have a=1,2 and b=1:5). I want to show how another variable (the one on the y axis) varies as a function of both a and b. What I'm trying to do is figure out a way to automatically place the second group of labels (in this case, the "b" variable). Here's a minimal example:
set.seed(1)
par(mar=c(7,3.5,1,1))
plot(1:10, 1:10+runif(10, -1, 1), xaxt="n", xlab="", ylab="", type="o")
axis(1, 1:10, rep(c("a=1", "a=2"), 5), las=2)
axis(1, seq(from=1.5, to=9.5, by=2), paste0("b=", 1:5), las=2, line=2, lwd=0)
Which produces the following graphic:
In this case, I got lucky and chose "line=2" as the correct placement. But if I modify things a bit (and make my variable labels a little more "bulky"):
set.seed(1)
par(mar=c(7,3.5,1,1))
plot(1:10, 1:10+runif(10, -1, 1), xaxt="n", xlab="", ylab="", type="o")
axis(1, 1:10, rep(c("Control", "Treatment"), 5), las=2)
axis(1, seq(from=1.5, to=9.5, by=2), paste0("b=", 1:5), las=2, line=2, lwd=0)
Now the groups of labels overlap:
Is there a way to automatically determine what "line" the axis is at so I can place my second group of labels without overlapping the first?
Here's something you could try. Instead of using line, which can be unreliable as you noted, you could pad white spaces to you second axis call using sprintf. Below, I first calculate the maximum number of characters in the first axis call, and multiply by 2.5, which seems to work well in this case. You might want to tweak this. Then, I use the default line=1 but the with spaces will "push" the second axis call away from the axis line. BTW, you might want to do something like max(nchar(first axis)) + max(nchar(second axis)) to calculate the padding.Finally, note that the "-" sign inside sprintf means add white spaces to the right.
set.seed(1)
par(mar=c(7,3.5,1,1))
max_nchar <- max(nchar(rep(c("Control", "Treatment"), 5))) *2.5
plot(1:10, 1:10+runif(10, -1, 1), xaxt="n", xlab="", ylab="", type="o")
axis(1, 1:10, rep(c("Control", "Treatment"), 5), las=2)
axis(1, seq(from=1.5, to=9.5, by=2),
sprintf(paste0("%-",max_nchar,"s"), paste0("b=", 1:5)), las=2, lwd=0)

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)

non-numeric tick marks in R

Newbie Question!
I'm trying to plot a line graph, but the tick-labels on the x-axis have to be non-numeric. How can I do this? So far I have this:
plot(disparitybin$Variance[1,], type="l", xlim=c(0,5.0), ylim=c(0, 0.05), frame=FALSE, xlab="Time", xaxt='n', yaxt='n', ylab="Dispairty (Sum of Variance)", main="Dispairty Through Time", cex.lab=1.25, cex.main=1.75)
axis(1, pos=0, las=0)
axis(2, pos=0, las=2)
I'm trying to specify the names of each tick mark along the x-axis: Eocene, Oligocene, Miocene, Pliocene. How do I get the labels control for the axis() command to accept this info?
Thanks for your help!
Use axis as follows:
axis.labels <- c("Eocene", "Oligocene", "Miocene", "Pliocene")
axis(1, labels=axis.labels, pos=0, las=0)
This assumes that you are plotting exactly four points of data. Also, you were correct to make the call to axis after the call to 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)

Resources