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.
Related
Basically, I want to write a program to transform R code into Latex formulas. For example, I want to build some sort of converter where, when I have, as input, mean(x) it will return $\frac{1}{n}\sum_{i=1}^{n} x_{i}$ - latex code for the formula. I would to this for a group of formulas I have to basically make my job easier.
Although this might be some work, I would still like to do it. Basically I would tell the program to return $\frac{1}{n}\sum_{i=1}^{n} everytime he finds the word mean; to, by default, transform x into x_{i}, this type of thing.
What language should I use to build such a program? Or does this already exist? I've looked online and found nothing of the sort... Would you say this is extremely difficult to do?
Thanks everyone!
Having come from Matlab I am struggling to work out why the following does not work:
plot(x=rand(10),y=rand(10))
Produces a graph correctly.
x=rand(10)
y=rand(10)
plot(x,y)
produces error:
ERROR: plot has no method matching plot(::Array(Float64,1),::Array(Float64,1))
I would be very grateful if someone coould explain to me why embeding the code within the plot line produces a result, but defining the variables beforehand results in an error. Logic says they should produce the same result.
I am using Julia v 0.3.1 and have loaded Gadfly as charting tool.
In the first case, you are using keyword argument syntax, not assigning to variables x and y (the meaning of = inside function calls is special). To get the same effect in the second case, you should use
x=rand(10)
y=rand(10)
plot(x=x,y=y)
which passes the value in the variable x in the keyword argument x to plot, and the value in the variable y in the keyword argument y.
In case you didn't. Write this before your code:
using plots
plyplot()
i am new to programming / R and i have a question that might be very easy.
my function is:
par(mfrow=c(2,2))
plot_QQ=function(x) {for(i in 2:x)
plot(c(data_raw[,Group1[i]]),c(data_raw[,Group1[1]]), xlab=paste("replicate",i), ylab="replicate 1")
abline(lm(c(data_raw[,Group1[i]])c(data_raw[,Group1[1]]))}
group1 is an vector c("","","") to grap specific the data. This function is working, but R does not draw the abline() in all plots. (only in the "last" plot c(data_raw[,Group1[i=x]]),c(data_raw[,Group1[1]]) the line is drawn.
sorry for such an easy question and thx for helping
greetz
In future you should supply some simulated data so that people can run your code, it's unclear what exactly you're trying to do. You don't need the c() functions, and your lm call isn't proper. Also you don't have curly braces around your for loop. Try this.
par(mfrow=c(2,2))
plot_QQ=function(x) {for(i in 2:x){
plot(data_raw[,Group1[i]],data_raw[,Group1[1]], xlab=paste("replicate",i), ylab="replicate 1")
abline(lm(data_raw[,Group1[i]]~data_raw[,Group1[1]])}}
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.
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.