我想添加一个包含变量的公式作为我的 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))))
正如您所看到的,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))))
它会给你一个错误:
Error: Aesthetics must be either length 1 or the same as the data (1): label
我可以像这样解析 annotate() 中的粘贴调用:
p <- annotate("text",x= 0.75, y =0.25, label = "paste(y[H1]== intercept1+ slope1 %.%x)", parse = TRUE)
但是,这不会写入变量值,因为它用引号引起来。引号中的 Replace() 表达式根本不会被解析。
那么我该怎么做呢?
任何帮助表示赞赏,
提前致谢
朱利叶斯