Subscript in axis label when a line break is included? - r

I've gone round in circles with this.
What I would like is the axis label to read "Difference in relative oxygen consumption" with "(V0sub(2peak))" below [the '2peak' in subscript]. I've tried substitute, bquote and a number of different combinations with expression and paste/paste0, whenever I have the link break in there it seems to add a large gap before the subscript "2peak".
qplot(1:10)+
labs(x="", y=expression(paste0("Difference in relative oxygen consumption\n",(V0[2][peak]))))
Using paste instead of paste0 will get around having the big gap, but then the second line is right aligned.
Thanks

paste is generally not needed and even confuses things. If you read the ?plotmath page you see that you cannot get linefeeds with \n. The atop plotmath function can be used:
qplot(1:10)+ labs(x="", y=expression(atop(
Difference~'in'~relative~oxygen~consumption,
"("*V0[`2peak`]*")")) )
Your intent w.r.t. the O\sub2 and "peak" was not clear. One glitch was that the word "in" is reserved which was why it needed to be quoted. The parser does not recognize tokens with initial digits which was why the '2peak' needed quoting.

I think #BondedDust's suggestion is best, but I also adapted a solution for a different question to possibly work as well. Here we do some pretty low-level grob hacking.
#helper function
library(grid)
doubleYTitle <- function(a,b) {
gTree(children=gList(
textGrob(a, gp=gpar(fontsize=13, fontface=2), y=.5, x=0,
vp=viewport(layout.pos.row=1, layout.pos.col=1), rot=90),
textGrob(b, gp=gpar(fontsize=12, fontface=1), y=.5, x=0,
vp=viewport(layout.pos.row=1, layout.pos.col=2), rot=90)
), vp=viewport(layout=grid.layout(nrow=1, ncol=2)), cl="doubleytitle")
}
widthDetails.doubleytitle <- function(x, recording=T) {
Reduce(`+`, lapply(x$children, grid:::widthDetails.text)) * 5
}
Now we put it to use
gg <- qplot(1:10)+labs(x="", y="Difference")
gb <- ggplot_build(gg)
gt <- ggplot_gtable(gb)
xx <- doubleYTitle("Difference in relative oxygen consumption",
expression((V0[2][peak])))
gt$grobs[[7]]<-xx
plot(gt)

Related

R rasterVis levelplot: a white line erroneously appears

I am plotting maps of atmospheric pollutant fields, or meteorological field, difference between such fields, often overlayed with orography.
My fields are gridded.
A white line misteriously appears, sometimes two.
This seems to happen a bit randomly. I mean: same code and fields, same line; but when I change fields, or color scales, it changes position, or it disappears, or another one appears. Sometimes horizontal, sometimes vertical.
Here is my code
#!/usr/bin/env Rscript
library(rasterVis)
library(RColorBrewer)
NX <- 468
NY <- 421
hgt <- matrix(0.,NX,NY)
# read from file:
ucon <- file("hgt.dat", open="rb")
for (n in seq(1,NX)) {
hgt[n,] <- readBin(ucon, "numeric", n=NY, size=4)
}
close(ucon)
hgtbks <- c(-100,10,500,1000,1500,2000,2500,3000,3500)
hgtcols <- colorRampPalette(c("gray30","white"))(length(hgtbks)-1)
tit <- "Orography"
bkstart=50.0; bkmax=1500.; bkby=100.
bks <- seq(bkstart, bkmax, bkby)
nbks <- length(bks)
cols <- rev(colorRampPalette(brewer.pal(11,"Spectral"))(nbks-2))
cols <- c("white",cols)
legendbreaks <- seq(1,nbks)
legendlabels <- formatC(bks,digits=3)
legendlabpos <- legendbreaks
rpl <-
levelplot(hgt, margin=FALSE , col.regions= hgtcols, at= hgtbks
, main= list(label=tit, cex=1.8)
, colorkey=list(draw= TRUE, col=cols, at=legendbreaks
, labels=list(labels=legendlabels, at=legendlabpos, cex=1.2))
, xlab=NULL, ylab=NULL, scales= list(draw= FALSE))
png("whiteline.png", width=800, height=840)
plot(rpl)
graphics.off()
I would really like to upload a file with my data, but for the moment
I could not find a way to do it (I don't think I can do it, not even an ASCII file). The data matrix (468x421) is too big to be explicitly included in the code, but it really is the orography file
shown in the picture (elevation in meters above mean sea level).
And here is the resulting "white line" map:
Really, I think this might be a levelplot bug. It seems to happen both when hgt is a matrix and when it is a proper raster object: this doesn't seem to make a difference.
Any idea?
I think I found a workaround.
By setting zero padding on the 4 sides, I managed to make the whiteline disappear from a series of maps.
First I defined:
zpadding <- list(layout.heights= list(top.padding=0, bottom.padding=0),
layout.widths= list(left.padding=0, right.padding=0))
then I added, among the parameters of the levelplot call:
par.settings=zpadding
As I said, I don't think this is a proper solution, but a workaround.
The problem seems related to any rescaling of the plot area.
In fact, when a rescaling is forced by, for example, having 4 or 5 digits (instead of 2 or 3) in the colorbar labels, a white line may reappear.
I hope this may point in the right direction other people, either users or developers of levelplot and related software.

ggplot Multiline Title with Different Font Sizes

<SlightlyLookingAway>I am attempting to reproduce an excel plot in R.</SlightlyLookingAway> The Excel plot has a two line title. I know how to handle this by putting a '\n' in the title text. What I do not know how to handle is that the first line has a larger font size than the second row of the title... I have done some google searching and have come up with a general lack of response.
I realize that I might be able to cobble something together with an annotation of some kind but that seemed like a kludge. If that is the only answer then it is, but I wanted to ask the community first.
Any ideas?
It looks as though I have found a hacked solution which gets the job done but does not offer a lot of flexability. The idea is to put in a math expression using the atop() command along with the bold() and scriptstyle() functions.
myplot +
ggtitle(expression(atop(bold("This is the Top Line"), scriptstyle("This is the second line")))) +
theme(plot.title = element_text(size = 20))
If you know of a better solution with more control over the line spacing and even being able to adjust the font face, please let me know...
try this,
library(gridExtra)
titleGrob <- function(x=c("First line", "second line"), size=10, ...){
n <- length(x)
size <- rep(size, length.out=n)
one_label <- function(x, size, ...)
textGrob(x, gp=gpar(fontsize=size), ...)
lg <- mapply(one_label, x=x, size=size, ..., SIMPLIFY=FALSE)
wg <- lapply(lg, grobWidth)
hg <- lapply(lg, grobHeight)
widths <- do.call(unit.c, wg)
heights <- do.call(unit.c, hg)
maxwidth <- max(widths)
g <- frameGrob(layout = grid.layout(n, 1, width=maxwidth, height=heights) )
for(ii in seq_along(lg))
g <- placeGrob(g, lg[[ii]], row=ii)
g
}
grid.newpage()
g <- titleGrob(size=c(18,12))
grid.arrange(qplot(1,1), top=g)
To perfectly center everything (which \n will not do), adapt every size of text whatever the number of lines and at the same time being able to adjust the interlinear space, use this instead:
e.g. for smaller to larger text size
ggtitle(expression(atop(scriptscriptstyle("whateverline1"),atop(scriptstyle("whateverline2"),atop(scriptscriptstyle(""),textstyle("whateverline3"))))))
Then use labeller=label_parsed
This also works for facet_grid, xlab and ylab
Note the scriptscriptstyle("") to control spacing between lines. You can also use varied relative sizes of text using scriptstyle or scriptscriptstyle or textstyle depending on your needs and of course use element_text(size=whatevernumber) in the theme section

Combining `expression()` with `\n`

I have a ggplot where I have used expression(phantom(x) >=80) in the label text to get a proper greater-than-or-equal symbol.
However I also need to have (N=...) immediately underneath:
require(ggplot2)
.d <- data.frame(a = letters[1:6], y = 1:6)
labs <- c("0-9\n(N=10)","10-29\n(N=10)","30-49\n(N=10)", +
"50-64\n(N=10)","65-79\n(N=10)", expression(phantom(x) >=80))
ggplot(.d, aes(x=a,y=y)) + geom_point() +
scale_x_discrete(labels = labs)
How can I combine the expression() with the escape \n ?
As #otsaw said in his answer to your earlier question, plotmath (and therefore expression) doesn't allow linebreaks.
However, as a hack, you can use atop to let ≥80 appears on top of (N=10). But as you will soon see it doesn't match with the other labels:
labs <- c("0-9\n(N=10)","10-29\n(N=10)","30-49\n(N=10)",
"50-64\n(N=10)","65-79\n(N=10)",
expression(atop(phantom(x) >=80, (N==10))))
So, as a further hack, you can pass the other labels as expressions:
labs <- c(expression(atop(0-9,(N==10))),expression(atop(10-29,(N==10))),
expression(atop(30-49,(N==10))), expression(atop(50-64,(N==10))),
expression(atop(65-79,(N==10))), expression(atop(phantom(x) >=80, (N==10))))
But of course you have #otsaw solution (using Unicode) that is considerably less wordy:
labs <- c("0-9\n(N=10)","10-29\n(N=10)","30-49\n(N=10)",
"50-64\n(N=10)","65-79\n(N=10)",
"\u2265 80\n(N=10)")
Another approach would be to use the recently archived tikzDevice. This creates the plots as tikz which are latex friendly format.
This will allow you to pass any latex expression as a character string to your labels.
It has the added benefit that you can compile the documents with the same preamble as your whole document so that the fonts etc are consistent.
All this can be automated using knitr using opts_chunk$set(dev = 'tikz')

Write x̄ (meaning average) in legend and how to prevent linebreak?

Good day!
I am not that familiar to R so I'd be glad to get a little help.
Assume I have the following minimal example:
test <- c(10,20,40,80,80)
avg <- mean(test)
avg <- format(avg,digits=2)
plot(test, xlab="x", ylab="y", pch = 4)
legend("topleft", legend= c("Average: ", avg))
I'd like to write x̄ instead of "average" - wonder if this is event possible as it's not a regular symbol - merely a combination of two (letter plus overline).
The other thing I'd like to get rid of is the line break after the word "Average (see arrow in graphic below):
There are two issues here. The first is that this is handled using ?plotmath in R. The operator you are looking for is bar(). This is not a function but markup that plotmath understands.
The second is that you need an expression in which avg is converted to its value. You need an expression because that is what plotmath works with. There are several solutions to this problem, but the one I use below is bquote(). You provide it an expression and anything wrapped in .( ) will be converted its value by evaluating the thing inside the .( ).
Here is your code and a suitably modified legend() call:
test <- c(10,20,40,80,80)
avg <- mean(test)
avg <- format(avg,digits=2)
plot(test, xlab="x", ylab="y", pch = 4)
legend("topleft", legend = bquote(bar(x)*":" ~ .(avg)))
Do note that this will insert exactly what is in avg. You may need to do
avg <- round(avg)
or some other formatting fix to get something nice and presentable.

add labels to lattice barchart

I would like to place the value for each bar in barchart (lattice) at the top of each bar. However, I cannot find any option with which I can achieve this. I can only find options for the axis.
Create a custom panel function, e.g.
library("lattice")
p <- barchart((1:10)^2~1:10, horiz=FALSE, ylim=c(0,120),
panel=function(...) {
args <- list(...)
panel.text(args$x, args$y, args$y, pos=3, offset=1)
panel.barchart(...)
})
print(p)
I would have suggested using the new directlabels package, which can be used with both lattice and ggplot (and makes life very easy for these labeling problems), but unfortunately it doesn't work with barcharts.
Since I had to do this anyway, here's a close-enough-to-figure it out code sample along the lines of what #Alex Brown suggests (scores is a 2D array of some sort, which'll get turned into a grouped vector):
barchart(scores, horizontal=FALSE, stack=FALSE,
xlab='Sample', ylab='Mean Score (max of 9)',
auto.key=list(rectangles=TRUE, points=FALSE),
panel=function(x, y, box.ratio, groups, errbars, ...) {
# We need to specify groups because it's not actually the 4th
# parameter
panel.barchart(x, y, box.ratio, groups=groups, ...)
x <- as.numeric(x)
nvals <- nlevels(groups)
groups <- as.numeric(groups)
box.width <- box.ratio / (1 + box.ratio)
for(i in unique(x)) {
ok <- x == i
width <- box.width / nvals
locs <- i + width * (groups[ok] - (nvals + 1)/2)
panel.arrows(locs, y[ok] + 0.5, scores.ses[,i], ...)
}
} )
I haven't tested this, but the important bits (the parts determining the locs etc. within the panel function) do work. That's the hard part to figure out. In my case, I was actually using panel.arrows to make errorbars (the horror!). But scores.ses is meant to be an array of the same dimension as scores.
I'll try to clean this up later - but if someone else wants to, I'm happy for it!
If you are using the groups parameter you will find the labels in #rcs's code all land on top of each other. This can be fixed by extending panel.text to work like panel.barchart, which is easy enough if you know R.
I can't post the code of the fix here for licencing reasons, sorry.

Resources