2

I have a dataframe (df) which looks something like this:

#Creating sample data
RE <- c(0.95, 0.8, 0.7, 0.85, 0.5, 0.3, 1, 0.8, 0.2, 0.95, 0.4, 0)
TimeSinceStart <- c(10, 20, 30, 10, 20, 30, 10, 20, 30, 10, 20, 30)
Name <- c("A","A","A","B","B","B", "A","A","A","B","B","B")
VaporPressure <- c(1.1, 1.1, 1.1, 0.002, 0.002, 0.002, 1.1, 1.1, 1.1, 0.002, 0.002, 0.002)
Aircleaner <- c("P1","P1","P1","P1","P1","P1","P2","P2","P2","P2","P2","P2")
 
#Creating the dataframe
df <- data.frame(RE = RE, TimeSinceStart = TimeSinceStart, Name = Name, VaporPressure = VaporPressure, Aircleaner = Aircleaner,
                  stringsAsFactors = FALSE)

#Defining my formula
my.formula <- y ~ x

where Name and VaporPressure always correspond to each other. Now I want to make a plot with a linear regression to see if there's a correlation between log10(VaporPressure) and RE for the Aircleaner's. I wanted to use stat_poly_eq and then manually decide the height of the equation through label.y as described in the accepted answer here. However, this does not work, and I can't figure out why. Can anyone help me?

I've tried the following (and a lot of variations of this):

ggplot(data = df, mapping = aes(log10(VaporPressure), RE, color=TimeSinceStart))+
  geom_point()+
  geom_smooth(method = "lm", se=FALSE, formula = my.formula, size=.7, color="black") +
  stat_poly_eq(geom = "label_npc",
               formula = my.formula, parse = TRUE,
               aes(label =  paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~~")),
               color="black", size = 3,
               label.y = c(0.3, 0.5))+
  facet_wrap(Aircleaner~., ncol = 2)+
  scale_color_continuous(name = "Operating time (h)")+
  labs(x = bquote(log[10](p[v])), y = "RE (%)", title = NULL)+
  theme(legend.position = "top")

which just places the labels at the same position in each facet based on the first label.y value specified.

I've also tried using label_repel, but this still overlaps with some of my data (in my real dataframe there's much more data and hence many points which is why I want to place the label manually).

UPDATE WITH SOLUTION

As commented by @Z.Lin, adding group = Aircleaner to my aes() did the trick. Thus the code below works perfectly:

ggplot(data = df, mapping = aes(log10(VaporPressure), RE, group = Aircleaner, color=TimeSinceStart))+
  geom_point()+
  geom_smooth(method = "lm", se=FALSE, formula = my.formula, size=.7, color="black") +
  stat_poly_eq(geom = "label_npc",
               formula = my.formula, parse = TRUE,
               aes(label =  paste(..eq.label.., ..rr.label.., sep = "*plain(\",\")~~")),
               color="black", size = 3,
               label.y = c(0.3, 0.5))+
  facet_wrap(Aircleaner~., ncol = 2)+
  scale_color_continuous(name = "Operating time (h)")+
  labs(x = bquote(log[10](p[v])), y = "RE (%)", title = NULL)+
  theme(legend.position = "top")
  • Hi, What is your `my.formula`? – Quinten Mar 29 '23 at 14:43
  • Sorry, I completely forgot to put that. It's `my.formula <- y ~ x`. I've now edited my question to include this. – Sara Bjerre Sørensen Mar 30 '23 at 07:14
  • Does adding `group = Aircleaner` inside `aes()` help your case? – Z.Lin Mar 30 '23 at 08:45
  • No unfortunately not. That just gives me `Error: Aesthetics must be either length 1 or the same as the data (2408): x` – Sara Bjerre Sørensen Mar 30 '23 at 09:49
  • Hmm, that can't be replicated from the example you gave, so I'm shooting in the dark. But the general intuition is that the different `label.y` (or `label.x`) values can be assigned to different equation positions when each equation corresponds to a different group of data points. In your toy example, that implicit grouping is based on the facet variable. In the answer you linked, it's the discrete colour variable. I can't tell what that would be, for your actual use case. – Z.Lin Mar 30 '23 at 10:01
  • You are completely right. This solved the problem! I don't know how to accept your answer when it's just a comment, but I'll edit my question to contain a solution with the answer you gave. Thank you so much! – Sara Bjerre Sørensen Mar 30 '23 at 11:16
  • @SaraBjerreSørensen Good question! Did not see it before, but you found the solution already. As you are new here, welcome! It may seem surprising at first, but it is in a case like this not only allowed but in fact recommended, because it helps those later searching for answer, to write yourself the solution to your own question as an answer. As both your question and answer are good and helpful, I would upvote also the answer if you write it as such. :-) – Pedro J. Aphalo Apr 05 '23 at 13:56

0 Answers0