2014-01-23 104 views
6

有没有方法用ggplot2以优雅的方式绘制复数? plot {graphics}为我的snowflake向量值做了它,但我宁愿在ggplot2。我遇到的ggplot2教程没有提及复杂的这个词。使用ggplot2绘制R中的复数

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

UPDATE

不幸的是,它会出现下面的建议不符合我的更复杂的数字工作 - 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

+3

geom_path可能 – baptiste

回答

9

只是ReIm,然后提取的实部和虚部传递给他们GGPLOT2 X和Y:

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

+0

佩顿,感谢您的建议!不幸的是,它不适合我的更复杂的例子 - 请参阅后更新。 –

+1

你说得对,它看起来像'geom_line'不是一个很好的替代品。我已经更新了我的示例以使用'geom_path',正如baptiste在评论中所建议的那样。 'geom_path'将按照您提供的顺序连接点。 – Peyton

+0

修正了,谢谢! :) –