我可以告诉 ggpairs 使用对数刻度吗?

发布于 2024-11-27 22:48:38 字数 60 浏览 0 评论 0原文

我可以向 GGally 包中的 ggpairs 函数提供一个参数,以便对某些而不是全部变量使用对数刻度吗?

Can I provide a parameter to the ggpairs function in the GGally package to use log scales for some, not all, variables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一念一轮回 2024-12-04 22:48:38

您无法提供这样的参数(原因是创建散点图的函数是在没有比例的情况下预定义的,请参阅ggally_points),但您可以随后使用getPlot更改比例代码>和putPlot。例如:

custom_scale <- ggpairs(data.frame(x=exp(rnorm(1000)), y=rnorm(1000)),
upper=list(continuous='points'), lower=list(continuous='points'))
subplot <- getPlot(custom_scale, 1, 2) # retrieve the top left chart
subplotNew <- subplot + scale_y_log10() # change the scale to log
subplotNew$type <- 'logcontinuous' # otherwise ggpairs comes back to a fixed scale
subplotNew$subType <- 'logpoints'
custom_scale <- putPlot(custom_fill, subplotNew, 1, 2)

You can't provide the parameter as such (a reason is that the function creating the scatter plots is predefined without scale, see ggally_points), but you can change the scale afterward using getPlot and putPlot. For instance:

custom_scale <- ggpairs(data.frame(x=exp(rnorm(1000)), y=rnorm(1000)),
upper=list(continuous='points'), lower=list(continuous='points'))
subplot <- getPlot(custom_scale, 1, 2) # retrieve the top left chart
subplotNew <- subplot + scale_y_log10() # change the scale to log
subplotNew$type <- 'logcontinuous' # otherwise ggpairs comes back to a fixed scale
subplotNew$subType <- 'logpoints'
custom_scale <- putPlot(custom_fill, subplotNew, 1, 2)
半﹌身腐败 2024-12-04 22:48:38

这本质上与 Jean-Robert 的答案相同,但看起来更简单(平易近人)。我不知道这是否是一个新功能,但看起来您不再需要使用 getPlotputPlot

custom_scale[1,2]<-custom_scale[1,2] + scale_y_log10() + scale_x_log10()

这是一个将其应用于大矩阵的函数。提供绘图中的行数和绘图名称。

scalelog2<-function(x=2,g){  #for below diagonal
 for (i in 2:x){ 
    for (j in 1:(i-1)) { 
      g[i,(j)]<-g[i,(j)] + scale_x_continuous(trans='log2') +
scale_y_continuous(trans='log2')
                        } } 
 for (i in 1:x){  #for the bottom row 
      g[(x+1),i]<-g[(x+1),i] + scale_y_continuous(trans='log2') 
                       } 
 for (i in 1:x){ #for the diagonal
      g[i,i]<-g[i,i]+ scale_x_continuous(trans='log2')  } 
  return(g) }

This is essentially the same answer as Jean-Robert but looks much more simple (approachable). I don't know if it is a new feature but it doesn't look like you need to use getPlot or putPlot anymore.

custom_scale[1,2]<-custom_scale[1,2] + scale_y_log10() + scale_x_log10()

Here is a function to apply it across a big matrix. Supply the number of rows in the plot and the name of the plot.

scalelog2<-function(x=2,g){  #for below diagonal
 for (i in 2:x){ 
    for (j in 1:(i-1)) { 
      g[i,(j)]<-g[i,(j)] + scale_x_continuous(trans='log2') +
scale_y_continuous(trans='log2')
                        } } 
 for (i in 1:x){  #for the bottom row 
      g[(x+1),i]<-g[(x+1),i] + scale_y_continuous(trans='log2') 
                       } 
 for (i in 1:x){ #for the diagonal
      g[i,i]<-g[i,i]+ scale_x_continuous(trans='log2')  } 
  return(g) }
数理化全能战士 2024-12-04 22:48:38

在将它们提供给 ggpairs 之前,最好适当使用线性标度和对数变换变量,因为这可以避免相关系数计算方式的歧义(在对数变换之前或之后)。
这可以很容易地实现,例如:

library(tidyverse)

log10_vars <- vars(ends_with(".Length"))             # define variables to be transformed

iris %>%                                             # use standard R example dataframe
  mutate_at(log10_vars, log10) %>%                   # log10 transform selected columns
  rename_at(log10_vars, sprintf, fmt="log10 %s") %>% # rename variables accordingly
  GGally::ggpairs(aes(color=Species))

示例 ggpairs 图与对数转换变量

It's probably better use a linear scale and log transform variables as appropriate before supplying them to ggpairs because this avoids ambiguity in how the correlation coefficients have been computed (before or after log-transform).
This can be easily achieved e.g. like this:

library(tidyverse)

log10_vars <- vars(ends_with(".Length"))             # define variables to be transformed

iris %>%                                             # use standard R example dataframe
  mutate_at(log10_vars, log10) %>%                   # log10 transform selected columns
  rename_at(log10_vars, sprintf, fmt="log10 %s") %>% # rename variables accordingly
  GGally::ggpairs(aes(color=Species))

example ggpairs plot with log transformed variables

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文