0

I am trying to mix annotate and expression statements in ggplot2. I'm getting a consistent error "Aesthetics must be either length 1 or the same as the data (1)". My first thought was that I had the wrong number of variables in aes. That might still be true, but I couldn't wrap my head around fixing it. So I searched and found errors and solutions that didn't seem to address the underlying problem. Here's my code:

r2.val <- .09
pl <- qplot(c(0,30))
pl+annotate(geom="text",x=0,y=28,label=(bquote(Value~is~sigma~R^{2}==.
(r2.val))))
SteveM49
  • 61
  • 1
  • 1
  • 5
  • Something like this `pl+annotate(geom="text",x=0,y=28,label=paste0("Value~is~Sigma~R^{2}==", r2.val), parse=TRUE)` – ahly Apr 21 '17 at 21:09

2 Answers2

1

I'm not familiar with bquote but it looks like you can achieve what you're trying to do by using paste0 and setting parse = TRUE in annotate:

pl + annotate(geom="text", x=10, y=1, 
              label = paste0("Value~is~sigma~R^2==", r2.val), parse = TRUE)

plot

Paolo
  • 3,825
  • 4
  • 25
  • 41
  • Thank you. I may use your suggestion. The post where I found the bquote function also mentioned that using paste or paste0 might be possible, but should be discouraged. Obviously, I don't understand bquote either, but if I leave the question open I might learn more. – SteveM49 Apr 21 '17 at 21:33
-1
pl <- qplot(c(0,30))
r2.val = 0.42
pl+annotate(geom="text",x=8,y=-.2,label=(paste("Value~is~sigma~R^{2}==",
                                               (r2.val))))

?

MLEN
  • 2,162
  • 2
  • 20
  • 36
  • 1
    Thank you, that corrects the missing value for r2.val, but I continue to see the Aesthetics error message. In my main session, that is the only error message I see. In a different, fresh session I also get "`stat_bin()` using `bins = 30`. Pick better value with `binwidth`." – SteveM49 Apr 21 '17 at 21:10
  • @SteveM49 That's just letting you know that it's using the default number of bins with 30 for the histogram. If you want to change that add `binwidth = ` inside `qplot`. – Paolo Apr 23 '17 at 17:39