ggplot2 facets: Different annotation text for each plot

J_T*_*oat 2 r ggplot2

I have the following generated data frame called Raw_Data:

    Time Velocity Type
1    10        1    a
2    20        2    a
3    30        3    a
4    40        4    a
5    50        5    a
6    10        2    b
7    20        4    b
8    30        6    b
9    40        8    b
10   50        9    b
11   10        3    c
12   20        6    c
13   30        9    c
14   40       11    c
15   50       13    c
Run Code Online (Sandbox Code Playgroud)

I plotted this data with ggplot2:

ggplot(Raw_Data, aes(x=Time, y=Velocity))+geom_point() + facet_grid(Type ~.)
Run Code Online (Sandbox Code Playgroud)

I have the objects: Regression_a, Regression_b, Regression_c. These are the linear regression equations for each plot. Each plot should display the corresponding equation.

Using annotate displays the particular equation on each plot:

annotate("text", x = 1.78, y = 5, label = Regression_a, color="black", size = 5, parse=FALSE)
Run Code Online (Sandbox Code Playgroud)

I tried to overcome the issue with the following code:

Regression_a_eq <- data.frame(x = 1.78, y = 1,label = Regression_a,
                       Type = "a")
p <- x + geom_text(data = Raw_Data,label = Regression_a)
Run Code Online (Sandbox Code Playgroud)

This did not solve the problem. Each plot still showed Regression_a, rather than just plot a

Jaa*_*aap 5

You can put the expressions as character values in a new dataframe with the same unique Type's as in your data-dataframe and add them with geom_text:

regrDF <- data.frame(Type = c('a','b','c'), lbl = c('Regression_a', 'Regression_b', 'Regression_c'))

ggplot(Raw_Data, aes(x = Time, y = Velocity)) +
  geom_point() + 
  geom_text(data = regrDF, aes(x = 10, y = 10, label = lbl), hjust = 0) +
  facet_grid(Type ~.)
Run Code Online (Sandbox Code Playgroud)

which gives:

在此处输入图片说明

You can replace the text values in regrDF$lbl with the appropriate expressions.