R: How do I write "≥2: n=nrow(x)" in plot legend? - r

I am doing boxplots and have problems with the legend. Specifically, I want to write "≥2: n=formatC(nrow(x))" but can not combine the commands for the ≥ symbol, the function that calculates nrow(x) and formatC(nrow(x), bigmark=",") that should give the nrow number with a thousand separator.
What I tried so far:
smoke <- matrix(c(1:1200),ncol=1,byrow=TRUE)
colnames(smoke) <- c("High")
smoke <- as.table(smoke)
pdf('test.pdf')
plot(NA,xlim=c(0,100),ylim=c(0,100))
legend(10,70,bquote(paste(NA>=2, ": n=", .(formatC(nrow(smoke)), big.mark=","))))
dev.off()
which gives: ≥ 2: n=1200
I would like to have: ≥2: n=1,200
It seems that formatC does not work under bquote and I would also like to remove the space after the ≥ symbol.
I also tried:
legend(x,y, legend=c(expression(NA>=2), paste(": n=", formatC(nrow(smoke)), sep="")))
which gives the legend in two lines:
≥ 2
: n=1200
Putting paste before expression gives one line but does not convert the >= to ≥.
I am exporting the graph as pdf, which currently works for the ≥ symbol. I would prefer to keep that. Unicode does not work with pdf in my hands.
Thanks in advance,
Philipp

You have a ) in the wrong place right after smoke, so it takes the big.mark argument as part of paste and not formatC. Try this:
legend(10,70,bquote(paste(NA>=2, ": n=", .(formatC(nrow(smoke), big.mark=",")))))

Related

bquote, parsing, expression to get multiple lines labels in ggplot with greek letters and variables as subscripts

Let's say I have
paste0("Year = ",index,"\nN = ",length((dfGBD %>% filter(year==index))[[vbl]]),
" Bandwidth = ",round(stats::bw.nrd(log((dfGBD %>% filter(year == index))[[vbl]])),2),
"\nSkewness:", round(e1071::skewness(log((dfGBD %>% filter(year==index))[[vbl]])), 2),
" Kurtosis:",round(e1071::kurtosis(log((dfGBD %>% filter(year==index))[[vbl]])),2),
"\nmu[",vbl,"] = ", round(mean((dfGBD %>% filter(year==index))[[vbl]]),2),
" sigma[",vbl,"] = ",round(sd((dfGBD %>% filter(year==index))[[vbl]]),2)
)
inside a sapply through index years. Further, vbl is a string with the name of a variable. The sapply produces a vector of labels for a factor variable.
Applying ggplot I obtain labels similar to the next:
Year = 2000
N = 195 Bandwidth = 0.09
Skewness: 0 Kurtosis: -0.56
mu[Mortality] = 7750.85 sigma[Mortality] = 1803.28
Till here, all ok. I have already written mu[vbl], sigma[vbl] thinking in parsing and subscript notation to get the greek letters with the name of the variable saved in vbl as subscript.
First I tried facet_wrap with option labeller = "label_parsed". I obtained an error which I only solved writting the string between backticks ``, but then \n has no effect. I tried many options using bquote and/or parse and/or expression and/or atop etc. in order to get this multiple lines result with the desired output I described above. But only get or one line or very ugly outputs or, mostly, errors, and I couldn't see yet the greek letters.
So what/how should I do?
PS: as answered in other stackoverflow's, \n does not work in this context, so a list with bquote's for each line is suggested. I tried it, but then I got an error that I think is due to incompatibility of number of elements of all the lists and number of labels of a factor (a label may not be a list?).
Thank you!

r bquote: remove the space before approximately equal plotmath symbol

This is almost what I want as a plot heading:
plot(1:10)
ylabs<-c("All","Native","Exotic")
i=1
mtext(bquote("("*.(letters[i])*")"~.(ylabs[i])~"("%~~%italic("H'")*")"),side=3)
But I don't want the space after "(" and before the approx. equal sign. Adding the * separator before the symbol gives an error
mtext(bquote("("*.(letters[i])*")"~.(ylabs[i])~"("*%~~%italic("H'")*")"),side=3)
Error: unexpected SPECIAL in
even though the * separator works in other parts of bquote. I can get the right spacing by including the approx. equal symbol directly
mtext(bquote("("*.(letters[i])*")"~.(ylabs[i])~"("*"≈"~italic("H'")*")"),side=3)
but I would like to know if there's a way to get * to work before the plotmath symbol?
I tried this with expression instead of bquote, but couldn't get it to combine the characters with the indexed objects.
The trick is to put the entire text into a subscript:
plot(1:10)
ylabs<-c("All","Native","Exotic")
i=1
b <- bquote(phantom(0)["("*.(letters[i])*")"~.(ylabs[i])~"(" %~~%italic("H'")*")"])
mtext(b, cex = 2, side=3)

Skip % in R when using sprintf

I am using "sprintf" function in R to generate some numbers as for a ggplot lables. The problem is that I want those numbers in percentage like the following:
sprintf("paste(round(%s*100, 2), '\\%', sep='')", data_plot[1])
As you can see I am using "\" so the sprintf function does not deal with it as a special character but I still receive the following error:
Error in sprintf("paste(round(%s*100, 2), '%', sep='')", names(data_plot [1]) : too few arguments
When I replace the "%" with for example "+" everything works fine. I found some posts regarding this and how I can write a separate function to take care of this, but I am wondering if there is an easier way of doing it.
Note: This line of code is a part of ggplot code so it has to be written like this.
Thanks
You need to use "%%" in sprintf to print a single "%".
You can use either paste or sprintf, you don't need both. So, something like
dat <- data.frame(x=seq(0.01, 1, len=10), y=runif(10))
ggplot(dat, aes(x, y)) +
geom_point() +
scale_x_continuous(breaks=dat$x, labels=sprintf("%.2f%%", 100*dat$x))

Using expression in variables

I'm trying to label a plot in R with superscript. For example, I have a variable called label:
>label <- colnames(tmp.df);
>label
[1] "ColumnA" "Volume 100mm3", "ColumnC", etc.
I would like to have "3" in the "Volume 100mm3" as superscript in my plot label. I cannot use something like:
label <- c("ColumnA", expression(paste('Volume 100mm'^'3')), "ColumnC");
since the ordering of the column names in tmp.df may change from run to run. So how can I get around this problem?
You could find the one with the mm by
ind <- grep("mm",label)
splt <- strsplit(label[ind], "mm")[[1]]
and then inject the expression via
label[ind] <- parse(text=sprintf("paste('%smm'^'%s')",splt[1],splt[2]))
If there are multiple strings that indicate the need for expressions, then it should be straightforward to adapt this.
You can use bquote for this. The * connects "Volume 100" to "mm^3" without a space. If you want a space there, you can use ~ instead of *.
plot(1:10, main = bquote(.("Volume 100") * mm^3))
how about just using the Unicode character for cubed 'SUPERSCRIPT THREE' (U+00B3)? in R, this would be escaped as '100mm\u00B3', or if this is coming from a data file, just use unicode characters directly in the file.

R - Plot: How to format in 10-base scientific notation and put it text, mtex, title etc functions?

I have numeric variable, say K=3.5e-5 (its values is calculated throughout my script). I want to write this value somewhere (title, as text in the plot, etc) in my plot as:
K_{root} = 3.5 10^{-5} cm /d
I have tried the functions bquote, substitute and no one worked.
Let's put the question in examples. I have tried the following:
1)
png("exp_1.png")
kroot = 3.5e-5
plot(1:10,1:10,
text(4,9,bquote(italic(K[root])~"="~.(kroot)~"cm/d")))
dev.off()
Try my favorite function, paste().
plot(1:10,1:10,
text(4,9,gsub("e",paste("K[root]=",format(k,scientific=TRUE),"cm/d",sep=" "),replacement=" 10^")))
You can replace the "e" here using the function gsub. I've edited my answer to include this.
The output:
> k=.0000035
> k
[1] 3.5e-06
> gsub("e",paste("K[root]=",format(k,scientific=TRUE),"} cm/d",sep=" "),replacement=" 10^{ ")
[1] "K[root]= 3.5 10^{ -06 } cm/d"
You can remove the extra spaces around { -06 } by using the function substr, if it's important, or simply leave out the curly brackets in the gsub statement.
I try to avoid using paste inside expressions. There is generally a cleaner way to approach this:
expon <- floor(log10(kroot)) # returns -5
mantis <- kroot*10^(-1*expon ) # returns 3.5
plot(1:10,1:10,
text(4,9,substitute( italic(K[root]) == mantis %.% pten* expon ~cm/d,
list(expon=expon, mantis=mantis, pten=" 10^")))

Resources