Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Label or annotation with subscript and variable source

Tags:

r

ggplot2

I have an R routine which creates a number of plots from a large set of data. Each plot is labeled with a titles describing the details of the set of points plotted. Unfortunately, I have not been able to use subscripts in the text if I am using paste to combine a complex label. The result is ugly. This is a simplified version of the code using data from R. The title shows the technique I am currently using, without subscripts. The attempt at an improved version is placed either on the x axis or on the plot.

library(ggplot2)
x1 = 1
x2 = 2
list <- c(1:4)
tle <- paste("VGM = ", as.character(list[1]),
         "V, VDM = ", as.character(list[2]),
         "V, VGF = ", as.character(list[3]),
         "V, VDF = ", as.character(list[4]),
         "V", sep="")
p <- ggplot(mtcars, aes(x=wt, y=mpg)) +
  labs(title=tle) +
  geom_point()
p
p + xlab(expression(V[DM]))  #works fine
p + xlab(expression(paste(V[DM], "= 3"))) # works fine

# now we would like to use a variable to provide the number

p + xlab(expression(paste(V[DM], "=", x1))) # Just displays "x1", not value of x1
p + xlab(expression(paste(V[DM], "=", 
                      as.character(x1)))) # NO
p + xlab(expression(paste(V[DM], "=", 
                      as.character(as.number(x1))))) # NO
my.xlab1 <- bquote(V[DM] == .(x1)) 
p + xlab(my.xlab1) # We can see the success here

# A single variable at the end of the expression works
# What if you wanted to display two variables?

my.xlab2 <- bquote(V[GM] == .(x2))
my.xlab3 <- paste(my.xlab1, my.xlab2) 
p + xlab(my.xlab3) # doesn't work

# Apparently the expressions cannot be pasted together. Try another approach.
# Place the two expressions separately on the plot. They no longer need to be
# pasted together. It would look better, anyway. Would it work?

p + annotate("text", x=4, y=30, label="Annotate_text", parse=TRUE) 
# This is the idea
# p + annotate("text", x=4, y=30, label=bquote(V[DM] == .(x1)), parse=TRUE) 
# This is a disaster
# RStudio stops the process with a question mark placed on the console. Appears that
# more input is being requested?
p + geom_text(x=4, y=30, label="Geom_text") # works
p + geom_text(x=4, y=30, label=my.xlab1) # does not accept variables.

I have included comments which describe the problems raised by each attempt. Ideally, the information should probably be placed as an annotation on the plot rather than as a title, but I cannot find a way to do this. Using a subscript turns a character into an expression, and it seems that there is a long list of functions which handle characters but not expressions.

like image 706
Paul M Avatar asked Sep 19 '25 01:09

Paul M


1 Answers

If you want to "paste" two expressions together, you need to have some "operator" join them. There really isn't a paste method for expressions, but there are ways to put them together. First, obviously you could use one bquote() to put both variables together. Either

my.xlab3 <- bquote(V[DM] == .(x1)~ V[GM] == .(x2))
my.xlab3 <- bquote(list(V[DM] == .(x1), V[GM] == .(x2)))

would work. The first puts a space between them, the second puts a comma between them. But if you want to build them separately, you can combine them with another round of bquote. So the equivalent building method for the two above expressions is

my.xlab3 <- bquote(.(my.xlab1) ~ .(my.xlab2))
my.xlab3 <- bquote(list(.(my.xlab1), .(my.xlab2)))

All of those should work to set your xlab() value.

Now, if you also want to get annotate to work, you can "un-parse" your expression and then have R "re-parse" it for you and you should be all set. Observe

p + annotate("text", x=4, y=30, label=deparse(my.xlab3), parse=TRUE) 
like image 179
MrFlick Avatar answered Sep 20 '25 14:09

MrFlick