如何使用ggpairs()在不同方面设置相同的缩放比例 [英] How to set same scales across different facets with ggpairs()

查看:199
本文介绍了如何使用ggpairs()在不同方面设置相同的缩放比例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下数据集和代码来为数据帧中的每对变量构造2d密度轮廓图.我的问题是ggpairs()中是否有一种方法可以确保不同对变量的缩放比例相同,例如ggplot2中不同方面的缩放比例相同.例如,我希望每张图片的x标度和y标度都来自[-1,1].

I have the following dataset and codes to construct the 2d density contour plot for each pair of variables in the data frame. My question is whether there is a way in ggpairs() to make sure that the scales are the same for different pairs of variables like the same scale for different facets in ggplot2. For example, I would like the x scale and y scale are all from [-1, 1] for each of the picture.

提前谢谢!

情节看起来像

library(GGally)
ggpairs(df,upper = list(continuous = "density"),
     lower = list(combo = "facetdensity"))

#the dataset looks like 
print(df)
         x           y           z             w
1   0.49916998 -0.07439680  0.37731097  0.0927331640
2   0.25281542 -1.35130718  1.02680343  0.8462638556
3   0.50950876 -0.22157249 -0.71134553 -0.6137126948
4   0.28740609 -0.17460743 -0.62504812 -0.7658094835
5   0.28220492 -0.47080289 -0.33799637 -0.7032576540
6  -0.06108038 -0.49756810  0.49099505  0.5606988283
7   0.29427440 -1.14998030  0.89409384  0.5656682378
8  -0.37378096 -1.37798177  1.22424964  1.0976507702
9   0.24306941 -0.41519951  0.17502049 -0.1261603208
10  0.45686871 -0.08291032  0.75929106  0.7457002259
11 -0.16567173 -1.16855088  0.59439600  0.6410396945
12  0.22274809 -0.19632766  0.27193362  0.5532901113
13  1.25555629  0.24633499 -0.39836999 -0.5945792966
14  1.30440121  0.05595755  1.04363679  0.7379212885
15 -0.53739075 -0.01977930  0.22634275  0.4699563173
16  0.17740551 -0.56039760 -0.03278126 -0.0002523205
17  1.02873716  0.05929581 -0.74931661 -0.8830775310
18 -0.13417946 -0.60421101 -0.24532606 -0.1951831558
19  0.11552305 -0.14462104  0.28545703 -0.2527437818
20  0.71783902 -0.12285529  1.23488185  1.3224880574

推荐答案

我在ggpairs中找到了一种方法,该方法使用自定义函数来创建图

I found a way to do this within ggpairs which uses a custom function to create the graphs

df <- read.table("test.txt")

upperfun <- function(data,mapping){
  ggplot(data = data, mapping = mapping)+
    geom_density2d()+
    scale_x_continuous(limits = c(-1.5,1.5))+
    scale_y_continuous(limits = c(-1.5,1.5))
}   

lowerfun <- function(data,mapping){
  ggplot(data = data, mapping = mapping)+
    geom_point()+
    scale_x_continuous(limits = c(-1.5,1.5))+
    scale_y_continuous(limits = c(-1.5,1.5))
}  


ggpairs(df,upper = list(continuous = wrap(upperfun)),
        lower = list(continuous = wrap(lowerfun)))      # Note: lower = continuous

具有这种功能,它可以像任何ggplot一样自定义!

With this kind of function it is as customizable as any ggplot!

这篇关于如何使用ggpairs()在不同方面设置相同的缩放比例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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