2016-03-01 159 views
-1

我想用ggplot显示这个矩阵以获得线条: 例如:在X中从1到12的部分,并且在其中是具有不同颜色的5个线(类别),并且它们的相应的值。 实例第一点x = 1和Y = 12.25在绿色用ggplot绘制矩阵

DF <- read.table(text = " 
       Portion   1   2  3   4   5 
    1     1 12.250000 0.9423077 33.92308 0.0000000 1.8846154 
    2     2 6.236364 1.7818182 38.30909 0.8909091 1.7818182 
    3     3 9.333333 1.8666667 28.00000 0.0000000 2.8000000 
    4     4 9.454545 2.8363636 34.03636 4.7272727 0.9454545 
    5     5 27.818182 0.0000000 19.47273 2.7818182 0.9272727 
    6     6 19.771930 2.5789474 19.77193 0.8596491 6.0175439 
    7     7 22.350877 1.7192982 22.35088 0.8596491 1.7192982 
    8     8 17.769231 4.0384615 15.34615 0.8076923 4.0384615 
    9     9 16.925373 8.8656716 23.37313 2.4179104 2.4179104 
    10    10 10.036364 8.3636364 25.09091 0.8363636 1.6727273 
    11    11 8.937500 8.9375000 8.12500 0.0000000 0.0000000 
    12    12 12.157895 5.2105263 14.76316 0.8684211 0.0000000", header = TRUE) 
     newResults <- as.data.frame(DF) 

    library(reshape2) 
    R = data.frame(Portion = c('1','2','3','4','5','6','7','8','9','10','11','12'), newResults[,1], newResults[,2], newResults[,3], newResults[,4], newResults[,5]) 
    meltR = melt(R, id = "Portion") 
    ggplot(meltR, aes(reorder(Portion, -value), y = value, group = variable, colour = variable)) + geom_line(). 

红色 二点x = 2,Y = 0.9423为什么我的X值是没有顺序?这是否是最健康的方式?

非常感谢。

+1

下面的答案当然是一个'更健康'的方式来做到这一点..但在你的具体问题。值得注意的是,c('1','2','3','4','5','6','7','8','9','10 ','11','12')'不是你所期望的,因为它不是'数字',而是'字符',因此以字母数字顺序排列。 –

回答

2

尝试:

meltR = melt(DF, id = "Portion") 
ggplot(meltR, aes(x=Portion, y = value, group = variable, colour = variable)) + geom_line() 

在这种情况下,没有必要在审美reorder何事ggplot。这会给你如下图:

enter image description here

你可能想,无论是在第一步中重命名他们改变变量的名称,或提供自定义标签来ggplot