[R] variable in annotation, ggplot2

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Mon Dec 16 19:31:02 CET 2019


Another way:


expr <- substitute(V == x, list(x = as.list(V)))
ggplot(df, aes(x, y)) + geom_line() +
   annotate("text", x = 3, y = 20,
            label = deparse(expr), parse = TRUE)

Or this one (nothing to do with your use case, it's an example of plotmath):

v <- paste("atop(", paste0("'V ='*alpha[", V, "]", collapse = ","), ")")
ggplot(df, aes(x, y)) + geom_line() +
   annotate("text", x = 3, y = 20,
            label = v, parse = TRUE)



More idiomatic? At least it allows for the use of plotmath.


Hope this helps,

Rui Barradas

Às 17:27 de 16/12/19, Rui Barradas escreveu:
> Hello,
> 
> If you form the label with paste before the plot, it can display both 
> values. Something like
> 
> 
> lab <- paste("V = ", paste(V, collapse = ","))
> 
> ggplot(df,aes(x=x,y=y)) + geom_line() +
>    annotate("text", x = 3, y = 20, label = lab)
> 
> 
> Hope this helps,
> 
> Rui Barradas
> 
> Às 14:55 de 16/12/19, Troels Ring escreveu:
>> Hi friends - I have a simple problem of inserting values in label of a
>> ggplot2. I have a vector V with two values and want to show them in the
>> plot.
>>
>> Here is what I tried -  at most get the first entry "28".
>>
>> R version 3.6.1 (2019-07-05)
>>
>> Windows 10
>>
>>
>> BW
>> Troels
>>
>>
>> library(ggplot2)
>>
>> x <- 1:5
>>
>> y <- x^2
>>
>> V <- c(28,14)
>>
>> df <- data.frame(x=x,y=y)
>>
>> ggplot(df,aes(x=x,y=y))+geom_line()+
>>
>>      annotate("text",x=3,y=20,label=bquote(V ==.(as.vector(V))))
>>
>>
>>
>>
>> ggplot(df,aes(x=x,y=y))+geom_line()+
>>
>>      annotate("text",x=3,y=20,label=expression(paste("V is ",V)))
>>
>>
>>
>> ggplot(df,aes(x=x,y=y))+geom_line()+
>>
>>      annotate("text",x=3,y=20,label=bquote(V ==.(V)))
>>
>>
>> This email has been scanned by BullGuard antivirus protection.
>> For more info visit www.bullguard.com
>> <http://www.bullguard.com/tracking.aspx?affiliate=bullguard&buyaffiliate=smt 
>>
>> p&url=/>
>>
>>     [[alternative HTML version deleted]]
>>
>> ______________________________________________
>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide 
>> http://www.R-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
>>
> 
> ______________________________________________
> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide 
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list