2012-05-11 101 views
1

我正在寫在R程序y中另一列,按一定的索引中的列我有一個這樣的數據集:情節爲X與R中

category x-value y-value 
     1  2  5 
     1  3  1 
     1  4  10 
     1  5  23 
     2  2  12 
     2  3  15 
     2  4  21 
     2  5  29 
     3  2  34 
     3  3  45 
     3  4  7 
     3  5  9 

而且我想找到一個簡單的按「類別」對數據進行分組,並將這3組數據繪製在單個xyplot上。

謝謝!

回答

2

使用ggplot2? 這樣的事情?

df = read.table(text = " 
category x-value y-value 
    1  2  5 
    1  3  1 
    1  4  10 
    1  5  23 
    2  2  12 
    2  3  15 
    2  4  21 
    2  5  29 
    3  2  34 
    3  3  45 
    3  4  7 
    3  5  9", header = TRUE, sep = "") 

library(ggplot2) 
ggplot(df, aes(x.value, y.value, colour = factor(category))) + geom_point() + 
    geom_path() 

enter image description here

或那樣嗎?

ggplot(df, aes(x.value, y.value, shape = factor(category), 
colour = factor(category))) + geom_point(size = 5) 

enter image description here

+0

謝謝!這是非常整潔:)。最初我嘗試使用subset()來分割數據集,但這並不像您提供的那樣整齊。還在學習如何用ggplot繪製美麗的圖。 – cchuang

+0

@ sprite728你可能喜歡閱讀[wiki.stdout.org/rcookbook/Graphs/](http://wiki.stdout.org/rcookbook/Graphs/) –