2015-03-25 64 views
0

我对R相当陌生,我试图将三块地块堆叠在一起研究每个地块彼此的多样性。作为R的新手,我仍然没有设法找到解决方案。我已经尝试了ggplot和多槽,但未能完成任务。也许我的文本文件中的标题是问题,或者我甚至没有看清问题!这将会有很大的帮助,如果任何人都可以在这方面告诉我..与R堆积地块

我的脚本如下:

defects <- read.table(file="C:/_____.txt",header=TRUE) 
squareX <- c()   
squareY <- c()   

distance <- c(0, 17.0, 17.5, 34.5, 35.0, 52.0, 52.5, 69.5, 70.0, 87.0, 
       87.5, 104.5, 105.0, 122.0, 122.5, 139.5) 
square_beginning <- distance[seq(1,length(distance),2)] 

for (i in 1:length(defects$x)){ 

    for (e in square_beginning){ 
    if (defects$x[i]>e & defects$x[i]<e+17.5) { 
     squareX[i] <- e/17.5+1 
    } 
    if (defects$y[i]>e & defects$y[i]<e+17.5) { 
     squareY[i] <- e/17.5+1 
    } 
    } 
} 

defects<- cbind(defects,squareX,squareY) 
#plot (defects) 

cont <- read.table(file="C:/____.txt",header=TRUE) 
squareX <- c()   
squareY <- c()   

distance <- c(0, 17.0, 17.5, 34.5, 35.0, 52.0, 52.5, 69.5, 70.0, 87.0, 
       87.5, 104.5, 105.0, 122.0, 122.5, 139.5) 
square_beginning <- distance[seq(1,length(distance),2)] 

for (i in 1:length(cont$x)){ 

    for (e in square_beginning){ 
    if (cont$x[i]>e & cont$x[i]<e+17.5) { 
     squareX[i] <- e/17.5+1 
    } 
    if (cont$y[i]>e & cont$y[i]<e+17.5) { 
     squareY[i] <- e/17.5+1 
    } 
    } 
} 
par(mo=c(1,2)) 
plot(defects, main="test 1") 
plot(cont, main="test 2") 
+0

欢迎来到SO!您提供的脚本很好,但不幸的是它不可重现(例如缺少缺陷)。请参阅[如何制作可重现的示例](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example)了解一些信息。如果您使其具有可重复性,那么您更有可能对您的问题得到快速,准确的答案。 – 2015-03-25 12:27:31

回答

0

可以叠加情节设置的par()mfrowmfcol参数。对于3行1列:

par(mfrow=c(3,1)) #mfrow will fill by rows 

par(mfcol=c(3,1)) #mfcol will fill by columns 

plot()语句之前执行此操作。无需使用ggplot2或任何其他库。

更新:

显然,我误解你的问题,如果你想在一个情节中添加三个数据集,你不应该需要使用par,做作为@Carl Witthoft建议和使用plot()其次通过lines()points()

以这种方式绘制将根据传递给plot()的数据创建一个轴限制图。您可以更改与xlimylim手动设置限制:

plot(1:10, (1:10)^2, xlim=c(0, 15), ylim=c(0, 150)) 
lines(1:15, seq(1, 150, length=15)) 
points(0:10, seq(1, 150, length=11)) 
+0

谢谢@Molx!这是精确和容易:)虽然该图不包含所有感兴趣的坐标。它看起来受到了限制..所以我在剧情路线中使用了xaxt =“n”,之后是轴(1,139.5,by = 17,las = 1)..但是似乎不工作..您能否建议解决这个问题也.. ..非常感谢.. – 2015-03-26 08:19:57

1

您是否希望多条曲线在一个页面上,为MOLX”解决方案提供,或者是否要覆盖的数据目前尚不清楚单一情节。

如果是后者,你有几个选择。尝试绘制一个数据集,然后par(new=TRUE),然后下一个plot命令将写入相同的图形。谨防重复的轴和缩放问题。

或者您可以绘制第一组图形,然后明确地调用lines(cont[,1],cont[,2],...)绘制更多数据集。

+1

对不起,我没有说清楚..我想叠加在一个单一的情节数据。所以我用par(mfrow = c(1,1)),这似乎工作!到目前为止,我已经设法在同一个图中获得两组不同的坐标。正如我之前提到的那样,问题在于情节不完整。似乎受到限制。例如,我想绘制直到140,140,但显示的绘图直到120,120,即使坐标超过该值。 – 2015-03-26 08:51:48

+0

@ Ram.V良好的解决方案 - 现在,默认情况下,绘图将范围设置为第一个的最小值/最大值数据集绘制。如果你知道你想要的范围,可以用plot(x,y,xlim = c(min,max),ylim = c(min,max) – 2015-03-26 11:56:12