如何使用R ggplot绘制多条曲线并将它们作为组着色

时间:2016-05-03 19:40:26

标签: r ggplot2

我有这样的数据框。

      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

每行代表一个包含四个值的样本,最后一列是此样本的分类。我想要显示每个样本曲线并将类设置为颜色。 我试图重塑数据框,但后来我失去了我需要的类功能。 你能否给我一些提示或告诉我如何在R中做到这一点?

提前致谢。

2 个答案:

答案 0 :(得分: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 :(得分: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