用ggplot绘制矩阵

ran*_*ell 1 r ggplot2

我想用ggplot显示此矩阵以显示行:示例:在X中,从1到12的部分,在Y中,是5条线(类别),它们具有不同的颜色及其对应的值。示例第一个点x = 1,Y = 12.25(红色)第二个点x = 2,Y = 0.9423,绿色

  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().
Run Code Online (Sandbox Code Playgroud)

为什么我的X值未排序?这是最健康的方式吗?

非常感谢。

cof*_*nky 5

尝试:

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

在这种情况下,不需要reorder任何美感ggplot。这将为您提供以下图形:

在此处输入图片说明

您可能想要更改变量的名称,方法是在第一步中重命名变量,或者通过向提供自定义标签ggplot