1

First of all, I'm beginner R user. I wanna add R-square and p-value in my chart. But, in a line just separate by a comma. Attached is the code I'm trying to use.

ggplot(data, aes(x,y))+
geom_point(shape=1,size=4)+
stat_smooth(method="lm",size=0.6,se=FALSE,colour="black")+
annotate("text",x=30,y=0.45,label=c("italic(r^{2}==0.151)","p==0.226"),parse=TRUE)+
theme_bw(base_size = 12)

However, I receive this error:

Error: Aesthetics must be either length 1 or the same as the data (1): label

Thanks!

G5W
  • 36,531
  • 10
  • 47
  • 80
Pedr Nton
  • 79
  • 7

3 Answers3

4

When you want to parse multiple expressions as a single expression, you can use paste to take the expressions and put them on a single line. As explained in the other answer, right now you are giving annotate two distinct expressions but only one set of x, y coordinates and this is causing the error.

Without a comma, the single expression could look like

paste("italic(r^{2}) ==", 0.151, "~p==.226")

This is what you would put as your label in annotate. The additional tilde makes a space between the first and second expressions.

To include the comma, you need list in plotmath. In plotmath, list means a comma separated lists (see ?plotmath for all the available features). Essentially this means wrapping your entire expression in list inside paste.

paste("list(italic(r^{2}) ==", 0.151, ", p==.226)")

And so your annotate code would be

+ annotate("text", x=30, y=0.45, label=paste("list(italic(r^{2}) ==", 0.151, ", p==.226)"), parse=TRUE)
aosmith
  • 34,856
  • 9
  • 84
  • 118
  • Nice. Why couldn't I get that to work. ggplot kept silently failing when I added the comma. – Mike Wise Jun 16 '16 at 13:59
  • Ah, because of the list instead of a character array? Why does that make a difference? Odd. – Mike Wise Jun 16 '16 at 14:00
  • @MikeWise Yeah, `list` in plotmath is what allows comma separated lists. Without that you either get an error or don't get a comma. – aosmith Jun 16 '16 at 14:05
  • Well that is probably the more useful answer. I think you should get the check. Although I explain the error message which is probably useful too, so I will let it sit. – Mike Wise Jun 16 '16 at 14:09
0

This works, but it doesn't get you a comma, because a comma confuses the formula parser.

library(ggplot2)
set.seed(123)
n <- 100
data <- data.frame(x=(40+10*rnorm(n)),y=-0.2+0.5*rnorm(n))
ggplot(data)+
  geom_point( aes(x,y),shape=1,size=4)+
  stat_smooth(method="lm",size=0.6,se=FALSE,colour="black")+
  annotate("text",x=c(30,37),y=c(0.45,0.43),label=c("italic(r^{2}==0.151)","p==0.226"),
                                                                           parse=TRUE) +
  theme_bw(base_size = 12)

Here is the graph.

enter image description here

Note your error is coming about because you are giving it a vector of length 2 in your annotate command, but the x and y parameter only have length of 1. I adjusted them, but it would be better as one line I think.

To get the comma you can break it up into two annotate layers like thi:

library(ggplot2)
set.seed(123)
n <- 100
data <- data.frame(x=(40+10*rnorm(n)),y=-0.2+0.5*rnorm(n))
ggplot(data)+
  geom_point( aes(x,y),shape=1,size=4)+
  stat_smooth(method="lm",size=0.6,se=FALSE,colour="black")+
  annotate("text",x=30,y=0.45,label="italic(r^{2}==0.151)", parse=T) +
  annotate("text",x=36,y=0.43,label=", p==0.226",parse=F) +
  theme_bw(base_size = 12)

Yielding this:

enter image description here

But I personally think it is too much bother. Go without the comma. Although there might be a way to get it in the formula with an escape sequence or something.

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
0

In addition to the solution by aosmith, simply try the following one:

library(ggplot2)
set.seed(123)
n <- 100
data <- data.frame(x=(40+10*rnorm(n)),y=-0.2+0.5*rnorm(n))
ggplot(data, aes(x,y))+
  geom_point(shape=1,size=4)+
  stat_smooth(method="lm",size=0.6,se=FALSE,colour="black") +
  annotate(geom = "text",
           x = 30, y = 0.45,
           parse = TRUE,
           label = "italic(r)^{2}==0.151*','~italic(p)==0.226")

enter image description here

Jinsen
  • 1