2016-01-21 42 views
3

我試圖定義一個函數來產生一對圖的對角線上的散點圖。訣竅是我想要在panel.splot函數中指定垂直軸。最終我認爲這裏使用的是在對角線上可視化時間序列。對於一個簡單的例子,雖然我想用虹膜數據集來嘗試,並使用Species作爲垂直軸變量。下面的陰謀產生一個漂亮的情節:函數繪製對()變量對對角線的公共垂直軸變量

pairs(iris[,c(1:4)]) 

所以現在我需要定義一個對角線面板的功能。這裏是我的努力迄今:

panel.splot <- function(x, yvar,...) 
{ 
    usr <- par("usr"); on.exit(par(usr)) 
    par(usr = c(0, 2, usr[3:4])) 
    plot(yvar,x, xlim=c(min(x),max(x))) 
} 

然而,當我嘗試運行它,我得到的是我不知道如何解釋錯誤消息。

pairs(iris[,c(1:4)],diag.panel=panel.splot(x=x, yvar="Species")) 

Error in plot.window(...) : invalid 'xlim' value 
In addition: Warning messages: 
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion 
2: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion 
3: In min(x) : no non-missing arguments to min; returning Inf 
4: In max(x) : no non-missing arguments to max; returning -Inf 

我一直沒能找到另外一個例子。許多其他的功能來創建不同類型的情節,但沒有任何事情做到這一點。

爲了清楚起見,這是情節我想象會沿着對角線pairs()通話產生的類型:

plot(iris$Species,iris$Petal.Width) 
+0

我沒用過'pairs',但有一次我在'GGally :: ggpairs'瞥了一眼;看看最後一個例子(「自定義例子」),在那裏將任意一個繪圖分配給子圖的網格中的任何一個座標似乎相當容易。我試圖用'custom_car [1,1] < - plot'在對角線上繪製一個圖。 – Henrik

+0

在'panel.splot'裏面,你試圖把'yvar'的'min'和'max'包含字符串'Species'。我不認爲你打算這麼做。這不會解決您的問題,但它似乎是一個問題。 – steveb

+0

什麼'x'在'雙(虹膜[,C(1:4)],diag.panel = panel.splot(X = X,yvar = 「物種」)) ' – mtoto

回答

1

儘管你的例子情節,我解釋這個稍有不同(錯誤地)由於使用物種作爲縱軸變量。休伯特給出了答案,但認爲它可能會增加一些東西。

使用pairs()

panel.splot <- function(Y, Adjust=0.2){ 
        function(x, y=Y, adjust=Adjust) 
        { 
        usr <- par("usr"); on.exit(par(usr)) 
        xs <- range(x) 
        ys <- if(is.factor(y)) c(1, nlevels(y)) else range(y) 
        par(usr = c(xs[1], xs[2], ys[1], ys[2]) + c(-adjust, adjust)) 
        points(x, y) 
        } 
       } 

pairs(iris[, 1:4], diag.panel=panel.splot(Y=iris$Species)) 

將會產生

enter image description here

要做到同樣的事情ggpairs()你也可以定義用戶功能

library(GGally) 
library(ggplot2)  

# Define diagonal function 
diag_fun <- function(data, mapping, ...){ 
     ggplot(data = data, mapping = mapping) + 
     geom_point(...) 
     } 

ggpairs(iris, columns=1:4, 
     diag = list(continuous = diag_fun, mapping=aes(y=Species))) 

其中給出

enter image description here


更新您的意見...

是,對角項,該變量爲x映射通過。您可以使用coord_flip()或反轉函數中的映射。下面的函數執行此操作,並使用annotate()(ps。)添加變量名稱。你應該能夠換出_point GEOM的發言權,_boxplot

diag_fun <- function(data, mapping, xnudge=0.95, ynudge=1, pts=lst(), ...){ 

    # create range for annotate 
    lbl <- as.character(mapping$x) 
    xmax <- max(data[, as.character(mapping$x)], na.rm=TRUE) 
    ymax <- mean(seq(nlevels(data[,as.character(mapping$y)]))) 

    # reverse mapping so no need for coord_flip() 
    tmp <- mapping$y 
    mapping$y <- mapping$x 
    mapping$x <- tmp 

    ggplot(data=data, mapping=mapping) + 
     do.call(geom_point, pts) + 
     annotate("text", x=ynudge*ymax, y=xnudge*xmax, label=lbl, ...) 
     } 

所以在ggpairs與默認

ggpairs(iris, columns=1:4, diag = list(continuous = diag_fun, mapping=aes(y=Species))) 

使用它,你可以使用wrap通過進一步論證

ggpairs(iris, columns=1:4, 
     diag = list(continuous = 
         wrap(diag_fun, 
          size=10, col="red", # make changes to the text label 
          pts=list(colour="blue")), # make changes to geom_point 
        mapping=aes(y=Species))) 
+1

我喜歡語法! – HubertL

+0

@ user20650你是對的。我打算把這個問題看成水平軸而不是垂直軸。我的錯。這似乎不是微不足道的,因爲當我將映射參數更改爲'mapping = aes(x = Year)'時,出現以下錯誤:錯誤:geom_point需要以下缺失美學:y'。我沒有在'diag_fun'中看到可以改變功能的地方。這裏的任何想法?繼此之後,我想要在每個對角線面板上顯示x軸標籤(水平)。有關我如何完成此任務的任何想法? – boshek

+1

i @ bosshek;我明天生病了 - 你想要點分情緒嗎?或者你想要盒狀圖嗎? (ps足夠在'diag_fn'中添加'+ coord_flip()' - 名稱位於右下方的面板中 – user20650

1

繼RAWR建議,我設法讓你的情節:

panel.splot <- function(y) 
{ 
     function(x,yv=y) 
     { 
       usr <- par("usr"); on.exit(par(usr)) 
       par(usr = c(0, 2, usr[3:4]), new = TRUE) 
       plot(yv,x) 
     } 
} 
pairs(iris[,1:4],diag.panel=panel.splot(iris[,5])) 

enter image description here

你仍然必須處理的文本大小

+0

這就是爲什麼我暗示像'圖(虹膜[,YV],X)'和直接在面板功能傳遞YV或者通過默認'panel.splot < - 函數(X,YV = 「Specis」)'或匿名'diag.panel = function(x)panel.splot(x,yv =「Species」)' – rawr