Used normal text in combination with normal text in plot - r

UPDATE: I actually found the solution myself, see below.
In R I want to add a label to a plot containing both subscript and normal text. To be more precise, I would like to use mtext() (or any other method that does the trick) to add a text below a plot. The text should look like this:
This can easily done in latex with $B\pm t_{a/2}SE(B)$
In R I come as far as mtext(expression(B%+-%t[a/2])), which does print
But the difficulty is in geting the SE(B) part after it, because of expression treating SE(B) as a function. I've tried several combinations with paste, but to no avail. I'm sure there must be a simple solution to this, but I wasn't able to find one after quite a long search.
UPDATE:
Wow, found the solution myself. As I said I have tried combinations of expression and paste and was sure I tried this before, but apparently, I did not. The solution is this:
mtext(expression(paste(B%+-%t[a/2],"SE(B)")))

I see you have solved this, but your final solution is much more nicely and succinctly handled by dropping the use of paste() and using the ~ operator to add spacing:
expression(B %+-% t[a/2] ~ SE(B))
e.g.:
plot(1:10, xlab = expression(B %+-% t[a/2] ~ SE(B)))
which gives
You can add extra spacing by using multiple ~: ~~~ for example. If you just want to juxtapose two parts of an equation, using the * operator, as in:
plot(1:10, xlab = expression(B %+-% t[a/2] * SE(B)))
which gives:
It isn't immediately clear from your Q which one is preferable.

Related

Math Symbols in corrplot

In my opinion this is one of the R stuff which is annoying. Some stuff is not logic or at least intuitively doable.
Now in this post, the usage of greek symbols in corrplot was nicely explained. However what I can't get running is the other way around with first a greek letter and than normal text. E.g.
colnames(data_matrix)[2] <- c(":Delta")
or
colnames(data_matrix)[2] <- c("=S1P[Delta]")
are working fine.
However putting the Delta BEFORE the text is not working for me. Any suggestion on this?
After playing around I found this solution:
colnames(data_mat)[2] <- c(":Delta [S1P]") so its logical and simple after all.
Now, one remaining problem is an empty space. I can write colnames(data_mat)[2] <- c(":Delta [S1P]") but not colnames(data_mat)[2] <- c(":Delta [S 1P]")
Any suggestions on this particular problem?

Count Values comma separated non-numeric (Google Sheets)

I'm trying to figure out how to get a count of answers inside a cell which are comma-separated in this format: Anna, peter, Hans, Otto (here it should be 4)
Need this for an assignment and nothing seems to work and my programming are very limited so I hope someone might help me out here :/
I have tried it in excel first with this formula:
=LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1),",",""))+1
..which didn't work (the brackets around the first A1 and after substitute turned red - whats that telling us anyway? My search only show me entries about negative values..)
Then I tried this formula here in google spreadsheet:
=COUNTA(SPLIT(A1; ","))
..which also didn't work (here I simply get an error).
I guess it's about the values being non numeric? Any ideas?
It is possible that you need:
=LEN(TRIM(A1))-LEN(SUBSTITUTE(TRIM(A1);",";""))+1
If your Regional Settings require it. (see Scott's comment)
This should do the trick
=LEN(A1)-LEN(SUBSTITUTE(A1,",",""))+1
It just counts the commas and adds 1
Update
I just realized that's pretty much the same as what you had - just without using TRIM which isn't necessary. Your formula should work too.
In Excel, use:
=LEN(A1)-LEN(SUBSTITUTE(A1,",","")) + 1
Unless there is a chance for A having no value, then you need to expand it farther:
=IF(LEN(A1)>0,LEN(A1)-LEN(SUBSTITUTE(A1,",","")) + 1,0)
Since you also tagged Google Spreadsheets, there use:
=COUNTA( SPLIT(A1, ",", TRUE))
Same applies for the possiblity of an empty field in Google Sheets.

writing $^*$ in R's matplot

I'm trying to represent $r^*$ in R matplot:
expression(r^*(bold(X)))
but the interpreter seems to get caught at the '^*' part.
Any solutions?
Try this:
plot.new()
text(0.5,0.5,expression(r^"*"*(bold(X))))
A raw * is used to separate (juxtapose) the plotted expressions, see ?plotmath. Here, you wish to print an asterisk as plain text.

Variable part in an expression( ... ) with R [duplicate]

This question already has answers here:
Use a variable within a plotmath expression
(3 answers)
Closed 9 years ago.
I would like to display physical units in an R plot. In order to have a better typography, I use the expression function this way:
plot(rnorm(10),rnorm(10),main=expression(µg.L^-1))
Suppose now that the unit is not statically known, and is given by a variable [unit]:
unit = 'µg.L^-1'
plot(rnorm(10),rnorm(10),main=expression(unit))
This of course does not work because [unit] is not substituted by its value. Is there some means to achieve that anyway?
Edit:
I should stress that the main difficulty here is that the unit to be displayed is sent as a string to my plot function. So the contents of unit should be interpreted as an expression at some point (that is transformed from a string to an expression object), and this is where the answer by texb comes handy. So please unmark this question as duplicate, since the use of parse is fundamental here and is not even mentionned in the post you suggest.
How about:
unit = 'µg.L^-1'
plot(rnorm(10),rnorm(10),main=parse(text=unit))
The bquote function gives you flexibility in creating expressions while inserting values from variables. Here is one example:
unit <- as.name('mu')
plot(rnorm(10), main=bquote( .(unit)*.L^-1 ) )
I think both answers are helpful but would like to suggest a more complete use of the plotmath syntax. The answer you accepted at the moment doesn't really parse the Greek mu separately and Greg Snow's answer doesn't illustrate how expressions can be used as values (but it does show how to substitute within expressions). So this is another alternative that also shows using a plotmath cdot operator as the separating "dot" which I think better addresses your interest in typography.
plot(1,1, main=expression(mu*g %.% L^-1) )
It's also possible to create fully formed expression and save by name:
micgmperL = expression(mu*g %.% L^-1)
plot(1,1, main=micgmperL)

Latex and variables in plot label in R?

How do I use variables in Latex expressions in R?
For example:
plot(X, Y, main=expression(R^2))
Will put R with a nice superscripted 2 as main title.
But let's say I want it to say 'R^2: 0.5', with 0.5 coming from a R variable. How do I do that?
The hack of Owen is pretty cool, but not really the common way to do that. If you use bquote, this is actually pretty easy. bquote will do the same as quote, with the exception that everything between .() will be evaluated in a specified environment (or the global, if nothing is specified).
A trivial example :
X <- 1:10
Y <- 1:10
a <- 0.8
plot(X,Y,main=bquote(R^2 : .(a)))
Gives :
See also ?bquote and ?plotmath for more examples and possibilities
Well this works...
call(':', quote(R^2), a)
though it feels a little hacky to me since it's using R's : operator, whereas you just want to stick some text on the end. Maybe there's a better way?
tikz and psfrag allow you to use actual LaTeX code and output instead of plotmath's, resulting in better typographic consistency.
See this question for details. Getting LaTeX into R Plots
Another variation on #Joris' theme is substitute(). You give substitute() an expression and an environment or list within which to evaluate the expression. Passing a list is usually easiest, especially for jobs such as the one posed here.
plot(X,Y, main = substitute(R^2 : a, list(a = a)))
We can see why this works, by looking solely at the substitute() call:
> substitute(R^2 : a, list(a = a))
R^2:0.8
The a in the expression is replace with the value of a in the list.

Resources