我有一个看起来像这样的数据框(df):
#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)
其中
Name
和VaporPressure
总是相互对应的。现在我想用线性回归绘制一个图,看看 log10(VaporPressure)
和 RE
之间是否存在 Aircleaner
的相关性。我想使用 stat_poly_eq
然后通过 label.y
手动确定方程的高度,如此处接受的答案中所述:https://stackoverflow.com/questions/51272809/how-to-position-r-squared -and-equation-in-facet-mode。但是,这不起作用,我不明白为什么。谁能帮帮我?
我已经尝试了以下(以及很多变体):
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")
根据指定的第一个 label.y 值将标签放置在每个方面的相同位置。
我也尝试过使用
label_repel
,但这仍然与我的一些数据重叠(在我的真实数据框中,有更多的数据,因此有很多点,这就是我想手动放置标签的原因)。