如何在R中绘制多个稀疏数据?

时间:2014-05-26 15:12:36

标签: r plot ggplot2

我的数据看起来像这样:

    Sample1       Sample 2     Sample 3       Sample 4
Reads    OTUs   Reads OTUs   Reads   OTUs   Reads   OTUs
100       26    90     17     80      47     95      43
500       35    250    23     290     52     315     59
700       40    490    29     350     60     460     62

我需要在X轴上绘制Reads,在Y轴上绘制OTUs,所有样本都有不同的颜色。任何想法如何在R或任何其他软件中绘制这个?

2 个答案:

答案 0 :(得分:1)

ggplot2解决方案:

# reading the data
data <- read.table(header=TRUE, text="Reads.Sample1    OTUs.Sample1   Reads.Sample2 OTUs.Sample2   Reads.Sample3   OTUs.Sample3   Reads.Sample4   OTUs.Sample4
100       26    90     17     80      47     95      43
500       35    250    23     290     52     315     59
700       40    490    29     350     60     460     62")

# reshaping the data into long format
require(reshape2)
melted <- cbind(
  melt(data, measure=c("Reads.Sample1","Reads.Sample2","Reads.Sample3","Reads.Sample4"),
       variable.name="Reads", value.name="rvalue"),
  melt(data, measure=c("OTUs.Sample1","OTUs.Sample2","OTUs.Sample3","OTUs.Sample4"),
       variable.name="OTUs", value.name="ovalue"))
melted <- melted[,c(5,6,11,12)]

# creating the plot
require(ggplot2)
ggplot(melted, aes(x=rvalue, y=ovalue, color=Reads)) +
  geom_point(shape=20, size=4) +
  scale_color_discrete("Sample", breaks=c("Reads.Sample1","Reads.Sample2","Reads.Sample3","Reads.Sample4"), 
                    labels=c(" 1"," 2"," 3"," 4")) +
  theme_bw()

给出: enter image description here

答案 1 :(得分:0)

DF <- read.table(text="    Sample1       Sample 2     Sample 3       Sample 4
Reads    OTUs   Reads OTUs   Reads   OTUs   Reads   OTUs
100       26    90     17     80      47     95      43
500       35    250    23     290     52     315     59
700       40    490    29     350     60     460     62", skip=1, header=TRUE)

matplot(DF[, seq(1, ncol(DF), by=2)], DF[, seq(2, ncol(DF), by=2)], pch=16)
legend("topright", legend=seq_len(ncol(DF)/2), col=seq_len(ncol(DF)/2), pch=16)