gpt4 book ai didi

r - 在 ggplot2 中使用反向轴在绘图上定位文本

转载 作者:行者123 更新时间:2023-12-01 05:11:45 26 4
gpt4 key购买 nike

我正在尝试使用 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")

感谢您的任何建议。

最佳答案

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

labels <- data.frame(xval = 10, yval = 30, labeltext = paste("italic(R^2)==",R2))

ggplot(df, aes(x = x, y = y)) +
geom_point(shape = 1) +
geom_text(data = labels, aes(x = xval, y = yval, label=labeltext), parse = T) +
scale_x_continuous(limits = c(14, 6), trans="reverse")

关于r - 在 ggplot2 中使用反向轴在绘图上定位文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24281845/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com