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")