I can't find a way how to write subscripts in the title or the subtitle in R.
How can I write v 1,2 with 1,2 as subscripts?
Thanks for your help!
expression is your friend:
plot(1,1, main=expression('title'^2)) #superscript
plot(1,1, main=expression('title'[2])) #subscript
If you are looking to have multiple subscripts in one text then use the star(*) to separate the sections:
plot(1:10, xlab=expression('hi'[5]*'there'[6]^8*'you'[2]))
See ?expression
plot(1:10,main=expression("This is a subscript "[2]))
A subscript and referring to a stored value...
a <- 10
plot(c(0,1), c(0,1), type = 'n', ann = FALSE, xaxt = 'n', yaxt = 'n')
text(0.2, 0.6, cex = 1.5, bquote(paste('S'['f']*' = ', .(a))))
Another example, expression works for negative superscripts without the need for quotes around the negative number:
title(xlab=expression("Nitrate Loading in kg ha"^-1*"yr"^-1))
and you only need the * to separate sections as mentioned above (when you write a superscript or subscript and need to add more text to the expression after).
As other users have pointed out, we use expression(). I'd like to answer the original question which involves a comma in the subscript:
How can I write v 1,2 with 1,2 as subscripts?
plot(1:10, 11:20 , main=expression(v["1,2"]))
Also, I'd like to add the reference for those looking to find the full expression syntax in R plotting: For more information see the ?plotmath help page. Running demo(plotmath) will showcase many expressions and relevant syntax.
Remember to use * to join different types of text within an expression.
Here is some of the sample output from demo(plotmath):
Related
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)
I am trying to rename the group names in a stripchart. I have tried using bquote and expression, but ran into the same problem. Everything within the brackets following expression is printed as a group label without formatting (so in the example below I am getting a label that reads "Delta ~ "Group1"".
I've tried combining expression and paste, but that gave me a label that read "paste(Delta, "Group1")".
stripchart(Df$value~Df$Treatment ,
vertical = TRUE,
group.names = c(expression(Delta ~ "Group1"), "Group2"),
Try setting your group.names=NA and then using the axis command to add the group.names afterwards.
I couldn't reproduce your example but if you do something like:
## define what you want your group.names to be using expression
group_names=c(expression(Delta==1),expression(Delta==2),expression(Delta==3),expression(Delta==4),expression(Delta==5))
## make your strip chart
stripchart(Temp~Month, data=airquality,group.names=NA,vertical=TRUE)
## add the lower axis labels, which equivalently adds the group.names
axis(1,at=1:5,labels=group_names)
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))
I'm trying to annotate a plot in ggplot with relevant data from a regression model.
I've followed the suggestions in this SO post and tried to modify the function to have a couple additional items in a newline in the plot.
This is my attempt at a new function:
lm_eqn = function(m){
eq <- substitute(italic(y) == a %.% italic(x)^b*","~~italic(r)^2~"="~r2*","~~italic(n)~"="~nn*","~~italic(p-value)~"="~pv,
list(a = format(exp(coef(m)[1]), digits = 3),
b = format(coef(m)[2], digits = 3),
r2 = format(summary(m)$r.squared, digits = 3),
nn=format(summary(m)$df[2]),
pv=format(summary(m)$coefficients[,4][2])))
as.character(as.expression(eq));
}
It produces the expected output: all in one line. But I'd like to split the text in two lines, the second one starting with italic(n)=. But if I introduce a \n, it throws an error when it finds \n. If I introduce the \n inside the quotes: "\n" then it seems to be ignored and the text remains in one line. I haven't found any reference as to how to introduce a newline in such an expression. Your kind help will be much appreciated.
Thanks.
EDIT: following a coment by #Tim I present a rewriten code and adjusted question.
\n cannot be used in plotmath expressions. You could perhaps break the expression in two parts, and use annotate to add the expressions where you want them. Or, use atop. Check out this post ->
Line break in expression()?
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.