简体   繁体   English

使用GGPlot在同一图上绘制多条线时遇到麻烦[包含数据集]

[英]Having trouble using GGPlot to plot multiple lines on the same plot [Data Set Included]

Here is the data set, df.test: 这是数据集df.test:

   MLSpredictions BPLPredictions
1        1.392213      0.8326201
2        1.392213      0.8662049
3        1.448370      0.9011444
4        1.448370      1.0146486
5        1.448370      0.9374932
6        1.448370      0.9374932
7        1.448370      0.9011444
8        1.448370      1.0981538
9        1.448370      1.0555757
10       1.506792      1.0555757
11       1.506792      1.1424492
12       1.506792      1.0555757
13       1.567570      1.0981538
14       1.567570      1.0981538
15       1.567570      1.1424492
16       1.567570      1.1424492
17       1.567570      1.1885314
18       1.567570      1.1424492
19       1.567570      1.1885314
20       1.630800      1.2364723

I know that GGPlot requires you to include all of the information in the same data frame which I believe I have done above. 我知道GGPlot要求您将所有信息都包含在同一数据帧中,我相信我已经在上面做了。

Here is my starting point: 这是我的出发点:

ggplot(df.test, aes(x = 1:20, y = , color = ))

Since my column names are different, I'm not sure what to put for "y". 由于我的列名不同,因此我不确定要为“ y”输入什么。 I've been looking all over for sample data frames that would be used in this instance but I'm coming up empty. 我一直在寻找在这种情况下将使用的示例数据帧,但是我空了。

Please advise. 请指教。

[EDIT] I would like to come up with a plot that has two lines with two different colors in the same plot. [编辑]我想提出一个绘图,该绘图在同一绘图中具有两条具有两种不同颜色的线。

ggplot expects the input data to be in so-called "long" format. ggplot期望输入数据为所谓的“长”格式。 In a long data set, 1 column contains the actual data values (whatever they may be), and all other columns tell us characteristics of those data points, such as what type of measure the values might be, what group they're a part of, etc. A long version of your data might look like: 在长数据集中,一列包含实际数据值(无论它们可能是什么),其他所有列则告诉我们这些数据点的特征,例如值可能属于哪种测量方式,它们属于哪个组等等。您的数据的长版可能如下所示:

index         variable      value
    1   MLSpredictions   1.392213
    2   MLSpredictions   1.392213
  ...              ...        ...
    1   BPLPredictions  0.8326201
    2   BPLPredictions  0.8662049
  ...              ...        ...

And you could then get your intended plot with: 然后,您可以使用以下方法获得预期的图:

my.plot <- ggplot(data = long.data, aes(x = index, y = value, color = variable)) +
           geom_line()

There are a few ways to convert your "wide" data into long format, one of which would be: 有几种方法可以将“宽”数据转换为长格式,其中一种是:

library(dplyr)
library(tidyr)

df$index <- 1:20
long.data <- gather(df, variable, value, -index)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM