gpt4 book ai didi

r - 使用 ggplot2 在 R 中绘制复数

转载 作者:行者123 更新时间:2023-12-04 11:24:58 24 4
gpt4 key购买 nike

有没有办法用 ggplot2 以优雅的方式绘制复数?
plot {graphics}
为我做 snowflake值向量,但我更愿意将它放在 ggplot2 中. ggplot2我遇到的教程没有提到一个复杂的词。

snowflake <- c(complex(real=0, imaginary=0),
complex(real=1/3, imaginary=0),
complex(real=1/2, imaginary=(3^(1/2))/6),
complex(real=2/3, imaginary=0),
complex(real=1, imaginary=0))
plot(snowflake, type="l")

enter image description here

更新

不幸的是,下面的建议似乎不适用于我更复杂的数字 - plot {graphics}按照值向量给定的顺序排列点,而 qplot .请参见以下示例:
my.snowflake <- c( 0.0000000+0.0000000i, 0.1111111+0.0000000i,
0.1666667+0.0962250i, 0.2222222+0.0000000i,
0.3333333+0.0000000i, 0.3888889+0.0962250i,
0.3333333+0.1924501i, 0.4444444+0.1924501i,
0.5000000+0.2886751i, 0.5555556+0.1924501i,
0.6666667+0.1924501i, 0.6111111+0.0962250i,
0.6666667+0.0000000i, 0.7777778+0.0000000i,
0.8333333+0.0962250i, 0.8888889+0.0000000i,
1.0000000+0.0000000i)

结果:
plot(my.snowflake, type = "l")

enter image description here
qplot(Re(my.snowflake), Im(my.snowflake), geom="line")

enter image description here

最佳答案

只需使用 Re 提取数字的实部和虚部即可和 Im ,然后将它们作为 x 和 y 传递给 ggplot2:

qplot(Re(snowflake), Im(snowflake), geom="path")

The OP's plot in ggplot2

在您的更新中,您指出 geom_line不适用于非平凡的情况,因此您需要使用 geom_path相反,它按照提供的顺序连接点。这是您更复杂的示例,使用 geom_path :

The OP's updated plot in ggplot2, with geom_path

关于r - 使用 ggplot2 在 R 中绘制复数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21320412/

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