Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在R中绘制多个稀疏数据?_R_Plot_Ggplot2 - Fatal编程技术网

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

如何在R中绘制多个稀疏数据?,r,plot,ggplot2,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

我有一些数据如下所示:

    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轴上绘制
读数
,在Y轴上绘制
OTU
,所有样本使用不同的颜色。知道如何在R或任何其他软件中绘制此图吗?

DFA
ggplot2
解决方案:

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)
# 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()
#读取数据
数据