2016-08-15 55 views
0

下面是數據集,df.test:遇到問題使用GGPlot繪製在同積多行[數據集包括]

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 

我知道GGPlot需要你包括所有的信息,我相信我在上面完成了相同的數據框。

這裏是我的出發點:

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

由於我的列名是不同的,我不知道要放什麼東西了「Y」。我一直在尋找所有的樣本數據框架,在這種情況下,我會空着。

請指教。

[編輯]我想想出一個情節,有兩個不同的顏色在同一個情節線。

+5

可能[複製](http://stackoverflow.com/questions/17150183/r-plot-multiple-lines-in-one-graph)。和[這篇文章](http://stackoverflow.com/questions/35586520/when-creating-a-multiple-line-plot-in-ggplot2-how-do-you-make-one-line-thicker)給出了信息關於'aes(x)' – cuttlefish44

+0

這個問題缺乏你試圖想出的東西的信息。你能否提供你想要在你的圖表中繪製的內容? –

+0

'ggplot(df.test,aes(x = MSLpredictions,y = BPLPredictions))+ geom_line()'會爲您發佈的所有數據提供一行。爲了得到兩條不同顏色的線條,你需要一個具有兩個不同值的分類列。例如,'df.test $ group = rep(c(「A」,「B」),10)',那麼ggplot(df.test,aes(x = MSLpredictions,y = BPLPredictions,color = group) )+ geom_line()' – eipi10

回答

1

ggplot希望輸入數據是所謂的「長」格式。在一個長數據集中,1列包含實際數據值(不管它們可能是什麼),而其他所有列告訴我們這些數據點的特徵,例如值的類型可能是什麼類型,它們是哪一組的,等你的數據的長版本可能看起來像:

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

然後你可以用得到你的意圖的情節:

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

有幾種方法可以轉換你的「寬」的數據轉換爲長格式,其中之一是:

library(dplyr) 
library(tidyr) 

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

Thnaks的_group_列中創建兩列,這肯定有幫助。現在,爲了讓事情變得更復雜一些,假設我有4個更多的列是兩組預測的置信區間的上限和下限,那麼這將如何工作。我知道如何使用ggplot創建置信區間功能區,但不知道如何將它們全部繪製在同一個繪圖上。 – madsthaks

+0

@ user3552144考慮詢問作爲單獨的問題 –

+0

我同意這可能是最好的單獨詢問。簡而言之,對於您想要使用的每種ggplot美學,您都需要一個專欄。因此,在這種情況下,您的數據將是「較長的」,並且包含錯​​誤術語的另一列。假設這個列被稱爲「錯誤」,你的'aes'看起來像'aes(x = index,y = value,ymin = value - error,ymax = value + error)'。 – jdobres