2016-06-07 70 views
0

我新的R和在一些代碼,輸出散點圖矩陣工作。該數據幀是在以下格式:散點圖矩陣使用對()中的R

A B C D 
2 3 0 5 
8 9 5 4 
0 0 5 3 
7 0 0 0 

我的數據集可以碰上行100-1000s和列10-100s,具有值的大規模(因此日誌變換我的數據)。

的碼,該位使我在提高基本情節一些部分成功(見嵌入圖像):

panel.cor <- function(x, y, digits = 2, prefix = "", cex.cor, ...) 
{ 
    usr <- par("usr"); on.exit(par(usr)) 
    par(usr = c(0, 1, 0, 1), xlog = FALSE, ylog = FALSE) 
    r <- abs(cor(x, y)) 
    txt <- format(c(r, 0.123456789), digits = digits)[1] 
    txt <- paste(prefix, txt) 
    if(missing(cex.cor)) cex.cor <- 0.8/strwidth(txt) 
    text(0.5, 0.5, txt, cex = cex.cor * r) 
} 

# Add regression line to plots. 

my_line <- function(x,y,...){ 
    points(x,y,...) 
    LR <- lm(log(x) ~ log(y), data = SP) 
    abline(LR, col = "red", untf = TRUE) 
} 

# Plot scatter plot matrices. 

pairs(mydataframe, pch = 20, main = "test", 
     cex = 0.125, cex.labels = 1, 
     xlim = c(100, 1e9), 
     ylim = c(100, 1e9), 
     upper.panel = panel.cor, 
     lower.panel = my_line, 
     log = "xy")' 

example

問題1 - 而不是在上面板越來越R^2個值,我得到了新生代。我該如何解決這個問題?
問題2 - 我想刪除與相關性成比例地調整R^2值的文本大小的函數。我知道它在panel.cor中,但不知道哪個部分需要移除或調整。

提前感謝

編輯:2016年8月6日
我發現周圍的工作也簡化了代碼:

panel.cor <- function(x, y, digits = 2, cex.cor, ...) 
{ 
    usr <- par("usr"); on.exit(par(usr)) 
    par(usr = c(0, 1, 0, 1)) 
    # correlation coefficient 
    r <- cor(x, y) 
    txt <- format(c(r, 0.123456789), digits = digits)[1] 
    txt <- paste("r= ", txt, sep = "") 
    text(0.5, 0.6, txt) 
} 

# add regression line to plots. 

my_line <- function(x,y,...) 
{ 
    points(x,y,...) 
    LR <- lm(x ~ y, data = SP) 
    abline(LR, col = "red", untf = TRUE) 
} 

# Plot scatterplot matrices. 

pairs(SP, pch = 20, main = "test", 
     cex = 0.125, cex.labels = 1, 
     upper.panel = panel.cor, 
     lower.panel = my_line) 

example 2

問題似乎是缺少值,即0。我最初將它們更改爲NA,這樣我就可以使用日誌比例。這與對數變換的組合導致上面板中缺失R^2值。

理想情況下,我想有一個數刻度。有沒有一種方法可以做到這一點,而不會引入上述問題?

澄清 - 我想在散點圖一個日誌(XY)規模(下圖)和x軸的直方圖(對角線面板)。我今天一直在玩,但不能完全按照我的意願去玩。也許我從對中問得太多。任何幫助,將不勝感激。

編輯:2016年10月6日

成功....還有約99%的快樂。

我做了更改 - 添加了對角線面板的直方圖和上面的面板的p值(「對()」中的基本代碼,用於添加由於x軸上使用的對數刻度而需要調整的直方圖)。如果他們不準確或不正確,請隨時糾正我的描述:

library(lattice) 
DF <- read.csv("File location", header = TRUE) 
DF.1 <- DF+1 # Added small epsilon to data frame otherwise plot errors arise due to missing values. 

# Function to calculate R^2 & p-value for upper panels in pairs() - scatterplot matrices. 

panel.cor <- function(x, y, digits = 3, cex.cor, ...) 
{ 
    usr <- par("usr"); on.exit(par(usr)) 
    par(usr = c(0, 1, 0, 1), xlog = FALSE, ylog = FALSE) # xlog/ylog: ensures that R^2 and p-values display in upper panel. 
    # Calculate correlation coefficient and add to diagonal plot. 
    r <- cor(x, y) 
    txt <- format(c(r, 0.123456789), digits = digits)[1] 
    txt <- paste("r= ", txt, sep = "") 
    text(0.5, 0.7, txt, cex = 1.25) # First 2 arguments determine postion of R^2-value in upper panel cells. 

    # Calculate P-value and add to diagonal plot. 
    p <- cor.test(x, y)$p.value 
    txt2 <- format(c(p, 0.123456789), digits = digits)[1] 
    txt2 <- paste("p= ", txt2, sep = "") 
    if(p<0.01) txt2 <- paste("p= ", "<0.01", sep = "") 
    text(0.5, 0.3, txt2, cex = 1.25) # First 2 arguments determine postion of p-value in upper panel cells. 
} 

# Function to calculate frequency distribution and plot histogram in diagonal plot. 

panel.hist <- function(x, ...) 
{ 
    usr <- par("usr"); on.exit(par(usr)) 
    par(usr = c(0.5, 1.5, 0, 1.75), xlog = TRUE, ylog = FALSE) # xlog argument allows log x-axis when called in pairs. 
    h <- hist(log(x), plot = FALSE, breaks = 20) 
    breaks <- h$breaks; nB <- length(breaks) 
    y <- h$counts; y <- y/max(y) 
    rect(breaks[-nB], 0, breaks[-1], y, col = "cyan") 
} 

# add regression line to plots. 

my_line <- function(x,y, ...) 
{ 
    points(x,y,...) 
    LR <- lm(log(x) ~ log(y), data = DF.1) 
    abline(LR, col = "red", untf = TRUE) 
} 

# Plot scatterplot matrices. 

pairs(DF.1, pch = 20, main = "Chart Title", 
     cex = 0.75, cex.labels = 1.5, label.pos = 0.0001, 
     upper.panel = panel.cor, 
     lower.panel = my_line, 
     diag.panel = panel.hist, 
     log = ("xy"), 
     xlim = c(5, 1e9), 
     ylim = c(5, 1e9)) 

在美中不足:

1 - 對角線面板中的文本標籤只是部分出現。我在「pairs()」中使用了一個遞減的值作爲label.pos參數,它將標籤向下移動直到它們出現。但是,無論我減少多少價值,他們都不會再行動了。我試圖強制直方圖功能的位置,但這是行不通的。我希望有人能看到我錯過的東西。在此先感謝......我已經沒有任何反應,但:(

PS:我想第3圖像與我的成功情節聯繫起來,但我在這裏缺乏信譽的挫敗......呻吟。

編輯:13/06/2016

解決!我感覺有點愚蠢。在對角線面板中定位主標題的方法非常簡單,我花了很長時間嘗試更復雜的方法來完成此操作。成對的「label.pos」參數應該是負數!我使用了一個小的值-0.0675,它將它放置在包含直方圖的單元格的頂部附近。

我希望別人認爲這很有用。我會標記爲已解決,但我會很感激任何關於我的代碼評論的評論,或者如果有人看到讓代碼更有效的方法。謝謝亞歷克斯

+0

愚蠢的問題如何設置我的問題解決? –

回答

1

有時我覺得完全密集。回答我自己的問題......誰會想到...拍拍頭。請在我的帖子中查看修改後的修改內容。