我有一个刻面的ggplot2散点图,并且想要打印关于每个方面的线性回归的摘要统计,就像这里和这里所做的那样.与我使用的那些示例scales="free"不同,每个方面的数据范围完全不同,但我希望摘要统计信息显示在每个方面的相同相对位置(例如右上角或其他).如何指定geom_text或annotate标签应出现在相对于面板的相同位置?
我现在在哪里:
# Fake data
set.seed(2112)
x <- c(1:10, 6:15)
y <- x + c(runif(10), runif(10)*10)
l <- gl(2, 10)
d <- data.frame(x=x, y=y, l=l)
# Calculate a summary statistic (here, the r-squared) in a separate data frame
r_df <- ddply(d, .(l), summarise, rsq=round(summary(lm(y~x))$r.squared, 2))
# Use geom_text and a separate data frame to print the summary statistic
ggplot(d, aes(x=x, y=y)) +
geom_text(data=r_df, aes(x=8, y=8, label=paste("rsq=", rsq)))+
geom_point() +
facet_wrap(~l, scales="free")
Run Code Online (Sandbox Code Playgroud)

相反,我希望将ggplot文本自动定位在每个方面的相同相对位置.
Bri*_*ggs 16
如果你想相对的角落放置它们,你可以通过指定一个实现这一目标x或y位置Inf或-Inf:
ggplot(d, aes(x=x, y=y)) +
geom_text(data=r_df, aes(label=paste("rsq=", rsq)),
x=-Inf, y=Inf, hjust=-0.2, vjust=1.2)+
geom_point() +
facet_wrap(~l, scales="free")
Run Code Online (Sandbox Code Playgroud)

我也进行了调整hjust,vjust所以标签不是在图表的确切角落,而是稍微推开它.
| 归档时间: |
|
| 查看次数: |
2673 次 |
| 最近记录: |