2017-01-29 22 views
0

我需要爲我的變量製作一個直方圖,它是'旅行時間'。而在這裏面,我需要繪製迴歸(相關)數據,即我觀察到的數據與預測的數據。而且我需要在一天和一週的不同時間重複它(用簡單的話來說,用par函數來製作這樣一個數字的矩陣)。現在,我可以繪製直方圖並以矩陣形式進行排列,但是我在內部繪圖中遇到問題(將x和y數據與y = x線一起繪製,並將它們排列在矩陣的連續直方圖繪圖中)。我如何做到這一點,如下圖所示。任何幫助,將不勝感激。謝謝!要做到這一點R工作室:在直方圖內製作小圖並根據需要重複多次,使用par函數

enter image description here

+1

請閱讀[如何提問](http://stackoverflow.com/help/how-to-ask),以及製作[可重現的問題](http://stackoverflow.com/問題/ 5963269 /如何對做 - 一個偉大-R重現-例子)。這包括顯示*樣本*代表性數據和*企圖*代碼。通常,它可以顯着簡化問題,可能使用標準數據集,而不必花費大量時間解釋所有變量的含義和/或包含無關變量。 – r2evans

+0

@ r2evans:謝謝你的建議。 – santosh

回答

0

一種方法是遍歷數據,並在每個迭代上創建所希望的描繪。這是一個不是很好的例子,但它顯示瞭如何繪製一個較大的情節小圖可以完成的邏輯。你將不得不調整代碼才能以你需要的方式工作,但它不應該那麼困難。

# create some sample dataset (your x values) 
a <- c(rnorm(100,0,1)) 
b <- c(rnorm(100,2,1)) 
# create their "y" values counterparts 
x <- a + 3 
y <- b + 4 
# bind the data into two dataframes (explanatory variables in one, explained in the other) 
data1 <- cbind(a,b) 
data2 <- cbind(x,y) 

# set dimensions of the plot matrix 
par(mfrow = c(2,1)) 
# for each of the explanatory - explained pair 
for (i in 1:ncol(data2)) 
{ 
     # set positioning of the histogram 
     par("plt" = c(0.1,0.95,0.15,0.9)) 
     # plot the histogram 
     hist(data1[, i]) 
     # set positioning of the small plot 
     par("plt" = c(0.7, 0.95, 0.7, 0.95)) 
     # plot the small plot over the histogram 
     par(new = TRUE) 
     plot(data1[, i], data2[, i]) 
     # add some line into the small plot 
     lines(data1[, i], data1[, i]) 
} 
+0

看起來好像很有效。謝謝! – santosh

+0

當然。如果它解決了你的問題,隨時接受答案作爲解決方案:) – ira