I have a coefficient array bees created in the following way:
gfit = lm(y_data,x_data);
bees = coef(gfit);, where bees[1]=0.123, bees[2]=4.56
A plot plot(x_data,y_data) is created. I'd liket to add some text on this plot. The text should look like $b_0=0.123, b_1=4.55$ (how to add Latex symbols on StackOverflow?).
I tried the following command: text(3,15,expression(paste("b"[0],"="bees[1])));, which turns out to be $b_0=bees_1$, i.e. the variable bees[1] is not interpreted properly.
How can I display the value of a variable by typing its name?
R doesn't have a LaTeX interpreter. You need to use ?plotmath. Try using bquote to allow getting values of R-objects , and here assuming that (1,1) is in the range of your (undescribed) data. The .()-function will put values pulled from the working environment into expressions:
text(1,1, bquote( list( b[0] == .(bees[1]) , b[1] == .(bees[2]) ) ) )
See the examples in ?bquote.
Writing formulas is a horrible mess in R. Only regexp is more write-only.
bees=c(0.12, 4.56)
plot(rnorm(100))
text(30,0,bquote(bees[1]== .(bees[1])))
Related
I always desire to have my R code as flexible as possible; at present I have three (potentially more) curves to compare based on a parameter delta, but I don't want to hardcode the values of delta anywhere (or even how many values if I can avoid it).
I am trying to make a legend that involves both Greek and a variable substitution for the delta values, so each legend entry is of the form like 'delta = 0.01', where delta is Greek and 0.01 is determined by variable. Many different combinations of paste, substitute, bquote and expression have been tried, but always end up with some verbatim code leftover in the finished legend, OR fail to put 'delta' into symbolic form.
delta <- c(0.01,0.05,0.1)
plot(type="n", x=1:5, y=1:5) #the curves themselves are irrelevant
legend_text <- vector(length=length(delta)) #I don't think lists work either
for(i in 1:length(delta)){
legend_text[i] <- substitute(paste(delta,"=",D),list(D=delta[i]) )
}
legend(x="topleft", fill=rainbow(length(delta)), legend=legend_text)
Since legend=substitute(paste(delta,"=",D),list(D=delta[1]) works for a single entry, I've also tried doing a 'semi-hardcoded' version, fixing the length of delta:
legend(x="topleft", fill=rainbow(length(delta)),
legend=c(substitute(paste(delta,"=",A), list(A=delta[1])),
substitute(paste(delta,"=",B), list(B=delta[2])),
substitute(paste(delta,"=",C), list(C=delta[3])) )
)
but this has the same issues as before.
Is there a way I can do this, or do I need to change the code by hand with each update of delta?
Try using lapply() with as.expression() to generate your legend labels. Also use bquote to create your individual expressions
legend_text <- as.expression(lapply(delta, function(d) {
bquote(delta==.(d))
} ))
Note that with plotmath you need == to get an equals sign. Also no need for paste() since nothing is really a string here.
Following problem: I try to make a legend where I will have something like:
ɛ = 5 L / (mol cm). The number however is calculated, here a minimal example:
plot(rnorm(10,3),rnorm(10,3))
epsilon.calc <- mean(rnorm(10,3))
legend("topleft",bty="n",legend=paste("epsilon=",format(epsilon.calc,digits=5),"L/(molcm)"))
legend("bottom",bty="n",legend=expression(epsilon,paste(format(epsilon.calc,digits=5)),"L/(molcm)"))
If I use the first legend I can paste the number (epsilon.calc), if I use legend 2 I can express epsilon in the right manner.
Anyone an idea to combine how to combine those expression() and paste() in one legend?
This is what I think you want:
legend("bottom",bty="n",legend=c(bquote(epsilon ==.(format(epsilon.calc, digits=5))),
expression( L/(mol %.% cm) )
) )
It's better to learn to use plotmath with minimal use of paste(). bquote is the simplest mechanism for getting evaluations done but it can also be done with substitute. paste inside an expression call is actually a different function than paste "outside" one.
This is the one line version:
legend("bottom",bty="n",
legend=bquote(epsilon ==.(format(epsilon.calc, digits=5))~(L/(mol %.% cm))
) )
I have just become aware of the label function in the Hmisc package. Yet, as far as I can see, this function does not solve my problem as axis labels still need to be manually specified as xlab=label(var).
I don't see any solution for producing nice tables either (xtable). Are there any packages I am not aware of that provide this sort of functionality ? In sum, I would like to do the following:
attach labels & units to variables, columns: label(wght) <- weight; unit(var) <- 'kg'
print them in a nice way, such that: wght <- c(20, 30); printnice(mean(wght)) -> '25 kg'
make plots, xtable use these 'nice' labels automatically
There are several questions:
1 In an R function, "return" only can output one plot or value. But now, I want the function output every plot or vector I require, how could I achieve that. Which code should I use.
2 I have a series of variables : Game1~Game10 and I built up a do loop to analysis each of them where I represented their name as
"paste("Game",i, sep="")",
But it is characters, and I cannot do it like a variable like
"sort(eval(paste("Game",i, sep="")))"
is fail. How could I make R recognize the characters series as a variable name.
to return more than one value from a function, use a data structure, that can store more values and return it, e.g. a vector, list or a dataframe
...
vector_1 <- 1:10
vector_2 <- 11:20
return( list(vec_1=vector_1, vec_2=vector_2) )
to output more than one plot, simply use a loop within the function e.g.
for(i in 5:10) plot(1:i)
Your second question is not clear to me. What are you trying to do?
My problem is to strip my panels with lattice framework.
testData<-data.frame(star=rnorm(1200),frame=factor(rep(1:12,each=100))
,n=factor(rep(rep(c(4,10,50),each=100),4))
,var=factor(rep(c("h","i","h","i"),each=300))
,stat=factor(rep(c("c","r"),each=600))
)
levels(testData$frame)<-c(1,7,4,10,2,8,5,11,3,9,6,12)# order of my frames
histogram(~star|factor(frame), data=testData
,as.table=T
,layout=c(4,3),type="density",breaks=20
,panel=function(x,params,...){
panel.grid()
panel.histogram(x,...,col=1)
panel.curve(dnorm(x,0,1), type="l",col=2)
}
)
What I'm looking for, is:
You should not need to add the factor call around items in the conditioning section of the formula when they are already factors. If you want to make a cross between two factors the interaction function is the best approach. It even has a 'sep' argument which will accept a new line character. This is the closest I can produce:
h<-histogram(~star|interaction(stat, var, sep="\n") + n, data=testData ,
as.table=T ,layout=c(4,3), type="density", breaks=20 ,
panel=function(x,params,...){ panel.grid()
panel.histogram(x,...,col=1)
panel.curve(dnorm(x,0,1), type="l",col=2) } )
plot(h)
useOuterStrips(h,strip.left = strip.custom(horizontal = FALSE),
strip.lines=2, strip.left.lines=1)
I get an error when I try to put in three factors separately and then try to use useOuterStrips. It won't accept three separate conditioning factors. I've searched for postings in Rhelp, but the only perfectly on-point question got an untested suggestion and when I tried it failed miserably.