使用ggpair创建这个图

时间:2021-11-08 01:37:13

I have some code in a Shiny app that produces the first plot below. As you can see the font size varies with the size of the correlation coefficient. I would like to produce something similar with ggpairs (GGally) or ggplot2. The second image below was produced with the following code:

我在一个闪亮的应用程序中有一些代码,它产生了下面的第一个图。可以看到字体大小随相关系数的大小而变化。我想生成类似于GGally (GGally)或ggplot2的东西。下面的第二幅图是用下面的代码制作的:

library(GGally)
ggpairs(df, 
  upper = list(params = c(size = 10)),
  lower = list(continuous = "smooth", params = c(method = "loess", fill = "blue"))
)

As you can see the size of the correlation font is adjustable using size but when I set a vector of sizes only the first value is used. I would also like to remove 'Corr:' and add an indicator of significance. Using colors for the sign of the correlation coefficient would also be nice. In lower, method and fill are not linked to smooth. Any suggestions on how to get the 2nd plot to capture more features of the 1st would be great.

正如您所看到的,使用size可以调整相关字体的大小,但是当我设置一个大小向量时,只使用第一个值。我还想删除Corr:,并添加一个有意义的指示器。用颜色表示相关系数的符号也不错。在较低的位置,方法和填充不与光滑连接。任何关于如何获得第2个剧情的建议都很好。

Anscombe's data:

安斯科姆的数据:

df <- structure(list(y1 = c(8.04, 6.95, 7.58, 8.81, 8.33, 9.96, 7.24, 
4.26, 10.84, 4.82, 5.68), x1 = c(10L, 8L, 13L, 9L, 11L, 14L, 
6L, 4L, 12L, 7L, 5L), y2 = c(9.14, 8.14, 8.74, 8.77, 9.26, 8.1, 
6.13, 3.1, 9.13, 7.26, 4.74), x2 = c(10L, 8L, 13L, 9L, 11L, 14L, 
6L, 4L, 12L, 7L, 5L), y3 = c(7.46, 6.77, 12.74, 7.11, 7.81, 8.84, 
6.08, 5.39, 8.15, 6.42, 5.73), x3 = c(10L, 8L, 13L, 9L, 11L, 
14L, 6L, 4L, 12L, 7L, 5L)), .Names = c("y1", "x1", "y2", "x2", 
"y3", "x3"), class = "data.frame", row.names = c(NA, -11L))

使用ggpair创建这个图

# based mostly on http://gallery.r-enthusiasts.com/RGraphGallery.php?graph=137
panel.plot <- function(x, y) {
    usr <- par("usr"); on.exit(par(usr))
    par(usr = c(0, 1, 0, 1))
    ct <- cor.test(x,y)
    sig <- symnum(ct$p.value, corr = FALSE, na = FALSE,
                  cutpoints = c(0, 0.001, 0.01, 0.05, 0.1, 1),
                  symbols = c("***", "**", "*", ".", " "))
    r <- ct$estimate
    rt <- format(r, digits=2)[1]
    cex <- 0.5/strwidth(rt)

    text(.5, .5, rt, cex=cex * abs(r))
    text(.8, .8, sig, cex=cex, col='blue')
}
panel.smooth <- function (x, y) {
      points(x, y)
      abline(lm(y~x), col="red")
      lines(stats::lowess(y~x), col="blue")
}
pairs(df, lower.panel=panel.smooth, upper.panel=panel.plot)

使用ggpair创建这个图

1 个解决方案

#1


12  

Edit for GGally 1.0.1

Since params is now deprecated, use wrap like so:

由于params现已被弃用,请使用包装如下:

ggpairs(df[, 1:2], 
        upper = list(continuous = wrap("cor", size = 10)), 
        lower = list(continuous = "smooth"))

使用ggpair创建这个图

Original answer

Customization of complicated plots is not always available through parameter list. That's natural: there are way too many parameters to keep in mind. So the only reliable option is to modify the source. This is especially pleasant when the project is hosted on github.

通过参数列表并不总是可以定制复杂的情节。这很自然:要记住的参数太多了。所以唯一可靠的选择就是修改源文件。当项目在github上托管时,这一点尤其令人愉快。

Here's a simple modification to start with, made in a forked repo. The easiest way to update the code and produce the plot below is to copy and paste the function ggally_cor to your global environment, then override the same function in the GGally namespace:

这里有一个简单的修改,以一个分叉的回购。更新代码并生成下图的最简单方法是将函数ggally_cor复制粘贴到全局环境中,然后在GGally名称空间中覆盖相同的函数:

# ggally_cor <- <...>
assignInNamespace("ggally_cor", ggally_cor, "GGally")
ggpairs(df[, 1:2], 
        upper = list(params = c(size = 10)), 
        lower = list(continuous = "smooth"))

使用ggpair创建这个图

I removed the text label and added significance indicators. Modifying colour and size is not that easy, though, since these are mapped earlier. I'm still thinking on it, but you get the idea and may move on with your further customizations.

我去掉了文本标签,添加了显著性指标。但是,修改颜色和大小并不容易,因为这些都是早期映射的。我还在考虑这个问题,但是您已经有了想法,可能会继续进行进一步的定制。

Edit: I've updated the code, see my latest commit. It now maps size of the label to the absolute value of the correlation. You can do similar thing if you want different colour, though I think this is probably a not very good idea.

编辑:我已经更新了代码,请查看我的最新提交。它现在将标签的大小映射到相关性的绝对值。如果你想要不同的颜色,你可以做类似的事情,尽管我认为这可能不是一个好主意。

使用ggpair创建这个图

#1


12  

Edit for GGally 1.0.1

Since params is now deprecated, use wrap like so:

由于params现已被弃用,请使用包装如下:

ggpairs(df[, 1:2], 
        upper = list(continuous = wrap("cor", size = 10)), 
        lower = list(continuous = "smooth"))

使用ggpair创建这个图

Original answer

Customization of complicated plots is not always available through parameter list. That's natural: there are way too many parameters to keep in mind. So the only reliable option is to modify the source. This is especially pleasant when the project is hosted on github.

通过参数列表并不总是可以定制复杂的情节。这很自然:要记住的参数太多了。所以唯一可靠的选择就是修改源文件。当项目在github上托管时,这一点尤其令人愉快。

Here's a simple modification to start with, made in a forked repo. The easiest way to update the code and produce the plot below is to copy and paste the function ggally_cor to your global environment, then override the same function in the GGally namespace:

这里有一个简单的修改,以一个分叉的回购。更新代码并生成下图的最简单方法是将函数ggally_cor复制粘贴到全局环境中,然后在GGally名称空间中覆盖相同的函数:

# ggally_cor <- <...>
assignInNamespace("ggally_cor", ggally_cor, "GGally")
ggpairs(df[, 1:2], 
        upper = list(params = c(size = 10)), 
        lower = list(continuous = "smooth"))

使用ggpair创建这个图

I removed the text label and added significance indicators. Modifying colour and size is not that easy, though, since these are mapped earlier. I'm still thinking on it, but you get the idea and may move on with your further customizations.

我去掉了文本标签,添加了显著性指标。但是,修改颜色和大小并不容易,因为这些都是早期映射的。我还在考虑这个问题,但是您已经有了想法,可能会继续进行进一步的定制。

Edit: I've updated the code, see my latest commit. It now maps size of the label to the absolute value of the correlation. You can do similar thing if you want different colour, though I think this is probably a not very good idea.

编辑:我已经更新了代码,请查看我的最新提交。它现在将标签的大小映射到相关性的绝对值。如果你想要不同的颜色,你可以做类似的事情,尽管我认为这可能不是一个好主意。

使用ggpair创建这个图