2014-01-23 102 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

修正了,謝謝! :) –