如何在GGally中使用自己的密度函数创建低密度图 [英] How to create lower density plot using your own density function in GGally

查看:382
本文介绍了如何在GGally中使用自己的密度函数创建低密度图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下代码:

  library(GGally)
library(tidyverse)
library viridis)


dat < - iris%>>%select( - 物种)

my_fn< - function(data,mapping,...) {
#使用默认的ggplot密度函数

p < - ggplot(data = data,mapping = mapping)+
stat_density2d(aes(fill = .. density ..), geom =tile,contour = FALSE)+
scale_fill_gradientn(colors = viridis :: viridis(100,option =viridis))
p
}


ggpairs(dat,lower = list(continuous = my_fn))+
theme_void()

我可以创建这个图:



我的问题是我怎样才能用下面的方案来改变GGally密度较低的图:

  library(MASS)
#获取2维点的密度。
#@param x数字向量。
#@param y一个数字向量。
#@param n用n个网格创建一个正方形来计算密度。
#@return每个方块内的密度。
get_density < - 函数(x,y,n = 100){
dens <-MASS :: kde2d(x = x,y = y,n = n)
ix< ; - findInterval(x,dens $ x)
iy < - findInterval(y,dens $ y)
ii < - cbind(ix,iy)
return(dens $ z [ )
}



#数据处理方法2 ---------------------- ----------------------------
theme_set(theme_bw(base_size = 16))
tbl< - as .tibble(虹膜)%>%
select( - 物种)


#tbl
dens_wrapper< - 函数(tbl = NULL,var1 = NULL, var2 = NULL){
tbl_pair< - tbl%>%
select_(var1,var2)
x< - tbl_pair%>%pull(var1)
y< - tbl_pair%>%pull(var2)
tbl_pair $ density< - get_density(x,y)
tbl_pair
}

feature1 =Sepal.Length
feature2 =Petal.Length
tbl_pair1< - dens_wrapper(tbl = tbl,var1 = feature1,var2 = feature2)
ggplot(tbl_pair1)+
geom_point(aes_string (feature1,feature2,color ='de nsity'))+
scale_color_viridis()

产生这种结果的是:

解决方案

使用与


With the following code:

library(GGally)
library(tidyverse)
library(viridis)


dat <- iris %>% select(-Species)

my_fn <- function(data, mapping, ...){
      # Using default ggplot density function

      p <- ggplot(data = data, mapping = mapping) + 
        stat_density2d(aes(fill=..density..), geom="tile", contour = FALSE) +
        scale_fill_gradientn(colours=viridis::viridis(100, option="viridis"))
      p
}


ggpairs(dat, lower=list(continuous=my_fn)) +
  theme_void()

I can create this plot:

My question is how can I change the GGally lower density plot with the following scheme:

library(MASS)
# Get density of points in 2 dimensions.
# @param x A numeric vector.
# @param y A numeric vector.
# @param n Create a square n by n grid to compute density.
# @return The density within each square.
get_density <- function(x, y, n = 100) {
  dens <- MASS::kde2d(x = x, y = y, n = n)
  ix <- findInterval(x, dens$x)
  iy <- findInterval(y, dens$y)
  ii <- cbind(ix, iy)
  return(dens$z[ii])
}



# Data wrangling method2 --------------------------------------------------
theme_set(theme_bw(base_size = 16))
tbl <- as.tibble(iris) %>% 
        select(-Species)


# tbl
dens_wrapper <- function (tbl=NULL, var1=NULL, var2=NULL) {
  tbl_pair <- tbl %>%
                select_(var1, var2)
  x <- tbl_pair %>% pull(var1)
  y <- tbl_pair %>% pull(var2)
  tbl_pair$density <- get_density(x,y)
  tbl_pair
}

feature1 = "Sepal.Length"
feature2 = "Petal.Length"
tbl_pair1 <- dens_wrapper(tbl=tbl, var1=feature1, var2=feature2) 
ggplot(tbl_pair1) +
  geom_point(aes_string(feature1, feature2, color = 'density')) +
  scale_color_viridis()

Which produce this:

解决方案

Using a similar idea as from Change colors in ggpairs now that params is deprecated , you can just add the calculations in to your own defined function.

my_fn <- function(data, mapping, N=100, ...){

      get_density <- function(x, y, n ) {
                  dens <- MASS::kde2d(x = x, y = y, n = n)
                  ix <- findInterval(x, dens$x)
                  iy <- findInterval(y, dens$y)
                  ii <- cbind(ix, iy)
                  return(dens$z[ii])
                }

      X <- data[,as.character(mapping$x)]
      Y <- data[,as.character(mapping$y)]

      data$density <- get_density(x=X, y=Y, n=N)

      p <- ggplot(data, mapping) +
               geom_point(aes(colour=density), ...) +
               scale_color_viridis()      
      p
}

 ggpairs(dat, lower=list(continuous=my_fn)) +
 theme_bw()

Produces:

这篇关于如何在GGally中使用自己的密度函数创建低密度图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆