r bquote: remove the space before approximately equal plotmath symbol - r

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)

Related

Expression, bquote and inequality sign

I am trying to print a "greater or equal to" sign an an R base plot. I have tried both with expression() and bquote() and I simply do not understand what is going wrong.
There is no need for long code, according to plotmath documenmtation this
expression(>=)
should return a vector of type "expression" containing its arguments (unevaluated). Therefore, it should return expression(>=). As is the case for
> expression(phi)
expression(phi)
I keep getting
> expression(>=)
Error: unexpected '>=' in "expression(>="
irrespective of the surrounding code.
You can wrap >= in backquotes, which turns the symbol into a name, but it will be displayed as two symbols, not one. Use expression("" >= "") to get the symbol you probably want. For example,
plot(1, xlab = expression(`>=`), ylab = expression("" >= ""))
Created on 2022-11-25 with reprex v2.0.2

When I type the name of a character vector in R, how can I print without unnecessary lines?

I am working with some character vector, with the size of each elements ranging from 4 to 855.
Let's assume the name of such vector is y. When I type "y", R prints out the whole vector, and the problem is that each element takes the same number of lines. Thus, if it takes 8 lines to print the elements with 855 character, then R also give 8 lines to the elements with only 4 character, and this way shows a lot of unnecessary lines
I want to remove this unnecessary lines. For example, I want
[1] "a"
[2]"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
to be printed as
[1] "a"
[2]"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
when I type the name of this vector.
How can I change this setting?
Note that the behavior you describe with extra lines being added to the printed output is not universal. For instance, my R Console on Mac (3.1.2) does not print the extra lines you described, but I get the extra lines when running the exact same version of R on the terminal.
You can do this with cat, looping through the vector and constructing the output as you wish:
> for (i in seq(y)) cat(paste0("[", i, "] \"", y[i], "\"\n"))
[1] "aaaa"
[2] "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
[3] "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
[4] "aa"
Here is a reproducible example:
y<-c("aaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", "aa")
y
You will notice that the top 2 lines have gaps underneath them when returned in the console.
The help from ?format details what is going on:
"Justification for character vectors (and objects converted to character vectors by their methods) is done on display width (see nchar), taking double-width characters and the rendering of special characters (as escape sequences, including escaping backslash but not double quote: see print.default) into account. Thus the width is as displayed by print(quote = FALSE) and not as displayed by cat. Character strings are padded with blanks to the display width of the widest. (If na.encode = FALSE missing character strings are not included in the width computations and are not encoded.)"
I think the solution here is to use cat for the printing.
An easy way to see how each element is padded with blank characters is to do the following:
format(y, trim=T)

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

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=",")))))

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