本文介绍了用ggplot绘制矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to display this matrix with ggplot in order to have lines :Example : in X the portion from 1 to 12, and in Y ther is 5 lines (categories) with different colors, and their corresponding values.Example first point x=1 and Y = 12.25 in red Second point x=2 and Y=0.9423 in green

  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值未排序?这是最健康的方法吗?

Why is my X value are not ordered ? and is it the healthiest way to do this ?

非常感谢.

推荐答案

尝试:

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

在这种情况下,无需为ggplot美观地添加reorder的任何内容.这将为您提供以下图形:

In this case there is no need to reorder anything in the aesthetic for ggplot. This will give you the following graph:

您可能想通过在第一步中重命名变量或为ggplot提供自定义标签来更改变量的名称.

You may want to change the names of the variables, either by renaming them in the first step, or by providing custom labels to ggplot.

这篇关于用ggplot绘制矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 20:38