用ggplot2绘制R中的复数

时间:2014-01-23 22:12:16

标签: r ggplot2 complex-numbers

有没有办法用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

1 个答案:

答案 0 :(得分:9)

只需使用ReIm提取数字的实部和虚部,然后将它们作为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