gpt4 book ai didi

r - 在ggplot的注释中显示固定小数

转载 作者:行者123 更新时间:2023-12-01 08:23:44 26 4
gpt4 key购买 nike

我需要在 ggplot 的注释中包含所有浮点数在小数点分隔符后显示 3 位数字。但我面临这个问题:

require(ggplot2)
data(iris)
a <- 1.8
b <- 0.9

ggplot(iris, aes(Sepal.Length, Petal.Length))+
geom_point()+
annotate("text", 7, 3,
label = paste0("y == ", format(a, digits = 3, nsmall = 3), " %*%z^",
format(b, digits = 3, nsmall = 3)), parse = TRUE)
ggplot(iris, aes(Sepal.Length, Petal.Length))+
geom_point()+
annotate("text", 7, 3,
label = sprintf("y == %0.3f %%*%%z^ %0.3f", a,b), parse = TRUE)

两者都产生只有一位小数的图。很明显,如果我改成 parse = FALSE ,那么该图带有正确的小数位数,但其格式(很明显)与预期的格式相差甚远。

除了硬性介绍文本之外,还有哪些其他选择可以实现这一目标?

最佳答案

如果您为数字添加引号,它会起作用。

ggplot(iris, aes(Sepal.Length, Petal.Length))+
geom_point()+
annotate("text", 7, 3, label = sprintf("y == '%0.3f' %%*%%z^ '%0.3f'", a,b), parse = TRUE)

这里,我改了 %0.3f'%0.3f'导致字符串
[1] "y == '1.800' %*%z^ '0.900'"

因此,数字不再被解释为数值。虽然 1.800代表数字 1.8, '1.800'表示字符串 1.800 .

enter image description here

关于r - 在ggplot的注释中显示固定小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46056293/

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