r - Label or annotation with subscript and variable source -
i have r routine creates number of plots big set of data. each plot labeled titles describing details of set of points plotted. unfortunately, have not been able utilize subscripts in text if using paste combine complex label. result ugly. simplified version of code using info r. title shows technique using, without subscripts. effort @ improved version placed either on x axis or on 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 # utilize variable provide number p + xlab(expression(paste(v[dm], "=", x1))) # 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) # can see success here # single variable @ end of look works # if wanted display 2 variables? my.xlab2 <- bquote(v[gm] == .(x2)) my.xlab3 <- paste(my.xlab1, my.xlab2) p + xlab(my.xlab3) # doesn't work # apparently expressions cannot pasted together. seek approach. # place 2 expressions separately on plot. no longer need # pasted together. better, anyway. work? p + annotate("text", x=4, y=30, label="annotate_text", parse=true) # thought # p + annotate("text", x=4, y=30, label=bquote(v[dm] == .(x1)), parse=true) # disaster # rstudio stops process question mark placed on console. appears # more input beingness requested? p + geom_text(x=4, y=30, label="geom_text") # works p + geom_text(x=4, y=30, label=my.xlab1) # not take variables.
i have included comments describe problems raised each attempt. ideally, info should placed annotation on plot rather title, cannot find way this. using subscript turns character expression, , seems there long list of functions handle characters not expressions.
if want "paste" 2 expressions together, need have "operator" bring together them. there isn't paste method expressions, there ways set them together. first, utilize 1 bquote()
set 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. first puts space between them, sec puts comma between them. if want build them separately, can combine them round of bquote
. equivalent building method 2 above expressions is
my.xlab3 <- bquote(.(my.xlab1) ~ .(my.xlab2)) my.xlab3 <- bquote(list(.(my.xlab1), .(my.xlab2)))
all of should work set xlab()
value.
now, if want annotate work, can "un-parse" look , have r "re-parse" , should set. observe
p + annotate("text", x=4, y=30, label=deparse(my.xlab3), parse=true)
r ggplot2
No comments:
Post a Comment