2016-05-03 6 views
-1

私はこのようなデータフレームを持っています。R ggplotを使って複数の曲線をプロットしてグループ化する方法

 ID  read1  read2  read3  read4 class 
1  5820350 0.3791915 0.3747022 0.3729779 0.3724259  1 
2  5820364 0.3758676 0.3711775 0.3695976 0.3693112  2 
3  5820378 0.3885081 0.3823900 0.3804273 0.3797707  2 
4  5820392 0.3779945 0.3729582 0.3714910 0.3709072  1 
5  5820425 0.2954782 0.2971604 0.2973882 0.2973216  3 
6  5820426 0.3376101 0.3368173 0.3360203 0.3359517  3 

各行は4つの値を持つ1つのサンプルを表し、最後の列はこのサンプルの分類です。私は各サンプルカーブを視覚化し、そのクラスを色として設定したいと思います。 データフレームの形状を変更しようとしましたが、必要なクラス機能が失われました。 Rでこれを行う方法を教えてください。

ありがとうございます。

答えて

1

あなたのデータを整理したいと思うでしょう(下にtidyr::gatherと示されています)。その後、あなたはプロット、あなたはgroup = IDcolor = factor(class)(離散色の)を設定することになるでしょうとき:

library(tidyr) 
library(ggplot2) 

df <- structure(list(ID = c(5820350L, 5820364L, 5820378L, 5820392L, 5820425L, 5820426L), 
       read1 = c(0.3791915, 0.3758676, 0.3885081, 0.3779945, 0.2954782, 0.3376101), 
       read2 = c(0.3747022, 0.3711775, 0.38239, 0.3729582, 0.2971604, 0.3368173), 
       read3 = c(0.3729779, 0.3695976, 0.3804273, 0.371491, 0.2973882, 0.3360203), 
       read4 = c(0.3724259, 0.3693112, 0.3797707, 0.3709072, 0.2973216, 0.3359517), 
       class = c(1L, 2L, 2L, 1L, 3L, 3L)), 
      .Names = c("ID", "read1", "read2", "read3", "read4", "class"), 
      class = "data.frame", row.names = c("1", "2", "3", "4", "5", "6")) 

df <- gather(df, reading, value, -c(ID, class)) 

ggplot(df, aes(x = reading, y = value, color = factor(class))) + 
    geom_line(aes(group = ID)) 

enter image description here

1

ここであなたがやりたいことがあり機能です。

PlotMultiCurve = function(x, classes, cols = NULL, colSet = "Set1", ...) { 

    if(!is.factor(classes)) classes = as.factor(classes) 
    nClasses = length(levels(classes)) 

    if(is.null(cols)) cols = brewer.pal(nClasses, colSet) 

    plot(1:ncol(x), x[1,], col = cols[classes[1]], type = "l", 
     ylim = range(x), xaxt = "n", ...) 
    axis(1, 1:ncol(x), 1:ncol(x)) 
    for(i in 2:nrow(x)) { 
    par(new = T) 
    plot(1:ncol(x), x[i,], col = cols[classes[i]], type = "l", 
     ylim = range(x), axes = F, xlab = "", ylab = "") 

    } 
} 

色を指定しない限り、RColorBrewerパッケージから自動的に色が選択されます。私は、直接テキストファイルにデータをコピーして、次を実行しました:

# Prepare data 
require(RColorBrewer) 
myData = read.table("Data.2016-05-03.txt") 
x = myData[,2:5] 
classes = as.factor(myData$class) 

# Plot into PNG file[![enter image description here][1]][1] 
png("Plot.2016-05-03.png", width = 1000, height = 1000, res = 300) 
par(cex = 0.8) 
PlotMultiCurve(x = x, classes = classes, xlab = "Read", ylab = "Response") 
dev.off() 

Example

関連する問題