将文本放在ggplot2中具有反转轴的图上

时间:2014-06-18 09:26:06

标签: r ggplot2

我正在尝试使用geom_text()在散点图上显示一些统计信息,但是当我反转x轴时,文本会消失。

x = rnorm(20, mean = 10)
y = rnorm(20, mean =30)
R2 = cor(x, y)^2
R2 = signif(R2, 2)
df = data.frame(x, y)

#  It works on a normal axis.

library(ggplot2)
ggplot(df, aes(x = x, y = y)) + geom_point(shape = 1) +
  geom_text(label=paste("italic(R^2)==",R2), x = 10, y = 30, parse = T) +
  scale_x_continuous(limits = c(6, 14)) 

#  But the text disappears when I reverse the x-axis:

ggplot(df, aes(x = x, y = y)) + geom_point(shape = 1) +
  geom_text(label=paste("italic(R^2)==",R2), x = 10, y = 30, parse = T) +
  scale_x_continuous(limits = c(14, 6), trans="reverse") 

感谢您的任何建议。

1 个答案:

答案 0 :(得分:1)

以下内容也只会写一次文本,并会显示您提供的第一个示例中的值:

func setUp() {
    super.setUp();
    XCUIApplication().launch
}