【问题标题】:Function to plot pairs() variables against common vertical axis variable in the diagonal绘制pairs()变量与对角线中公共垂直轴变量的函数
【发布时间】:2016-01-21 19:37:56
【问题描述】:

我正在尝试定义一个函数以在成对图的对角线上生成散点图。诀窍是我想在panel.splot 函数中指定垂直轴。最终,我认为这里的用途是在对角线上可视化时间序列。举一个简单的例子,虽然我想尝试使用 iris 数据集并使用 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)

【问题讨论】:

  • 我没用过pairs,但是我曾经看过GGally::ggpairs;看一下最后一个示例(“自定义示例”),其中似乎很容易将任何图分配给子图网格中的任何坐标。我只是尝试使用custom_car[1,1] &lt;- plot 在对角线上绘制一个图。
  • panel.splot 中,您正试图获取yvarminmax,其中包含字符串Species。我不认为你是故意的。这不会解决您的问题,但它似乎是一个问题。
  • x 中的 pairs(iris[,c(1:4)],diag.panel=panel.splot(x=x, yvar="Species")) 是什么
  • 如果我在没有x=x 的情况下运行它,我会收到此错误:Error in xy.coords(x, y, xlabel, ylabel, log) : argument "x" is missing, with no default。 @steveb:你是对的。编辑了问题。
  • 你想plot('character string', numeric_vector),改成plot(iris[, 'Species'], x) 并把new = TRUE 加到par,这是你想要的吗?

标签: r plot


【解决方案1】:

尽管有您的示例图,但由于使用 Species 作为纵轴变量,我对此的解释略有不同(不正确)。休伯特给出了答案,但认为它可能会增加一些东西。

使用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))

哪个产生

要对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)))

这给了


为您的 cmets 更新...

是的,对于对角线条目,变量作为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))) 

【讨论】:

  • @user20650 你是对的。我打算将问题阅读为水平轴而不是垂直轴。我的错。这似乎不是微不足道的,因为当我将映射参数更改为mapping=aes(x=Year) 时,我收到以下错误Error: geom_point requires the following missing aesthetics: y。我在diag_fun 中看不到我可以更改功能的地方。这里有什么想法吗?在此之后,我希望在每个对角面板上显示 x 轴标签(水平)。关于我如何实现这一点的任何想法?
  • i@bosshek;明天我会稍微看看 - 那么你想要绘制点还是想要箱线图? (ps 将+ coord_flip() 添加到diag_fn 是否足够 - 名称在右下面板中
  • @user20650 coord_flip() 确实有效。我仍然有兴趣了解为什么geom_point 似乎需要y 美学而不是x 美学。 x 美学是由ggpairs 提供的吗?事实上,我想要这个应用程序的积分,但我想保持它足够灵活以改变它。
  • @user20650 同样对于名称注释 - pairs 示例将变量名称直接嵌入到相应的对角图中。我想也许showStrips=TRUE 可以做到这一点,但没有。我可以想象通过geom_text 做到这一点,但我无法思考如何指定坐标的xy
【解决方案2】:

按照更原始的建议,我设法得到了你的情节:

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]))

您仍然需要处理文本大小

【讨论】:

  • 这就是为什么我建议类似plot(iris[, yv], x) 并在面板函数中直接传递yv 默认panel.splot &lt;- function(x, yv = "Specis") 或匿名diag.panel=function(x) panel.splot(x, yv = "Species")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-25
  • 2010-09-20
  • 2021-01-09
  • 1970-01-01
  • 2012-10-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多