我是否可以为GGally包中的ggpairs函数提供一个参数,以便对某些(而不是所有)变量使用日志刻度?
发布于 2011-08-04 21:34:30
您不能提供这样的参数(原因是创建散点图的函数是没有缩放的预定义函数,请参见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)发布于 2016-01-07 03:41:02
这基本上与Jean-Robert的答案相同,但看起来要简单得多(可接近)。我不知道这是不是一个新特性,但看起来你不再需要使用getPlot或putPlot了。
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) }发布于 2019-03-26 21:50:02
在将相关系数提供给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))

https://stackoverflow.com/questions/6934698
复制相似问题