我想添加一个包含变量的公式作为我的 ggplot 上的注释。
regline1 <- 0.00
slope1 <- 1.00
dat <- as.data.frame(c(0,1))
dat[2] <- c(0,1)
names(dat) <- c("foo","bar")
p <-
ggplot(dat, aes(foo, bar)) + coord_fixed(ratio = 1) + geom_point() +
geom_abline(slope = slope1, intercept = intercept1, linetype = "dashed") +
labs(x = substitute(y[H1]==i+s%*%x,list(i=format(intercept1, digits = 1), s= format(slope1, digits = 1))))
Run Code Online (Sandbox Code Playgroud)
正如您所看到的,ggplot 计算 labs(x =...) 的公式是没有问题的,但是如果您尝试添加注释:
p + annotate("text",x=0.75, y = 0.25, label = substitute(y[H1]==i+s%*%x,list(i=format(intercept1, digits = 1), s= format(slope1, digits = 1))))
Run Code Online (Sandbox Code Playgroud)
它会给你一个错误:
Error: Aesthetics must be either length …Run Code Online (Sandbox Code Playgroud)