不同点大小的相关散点矩阵图(R 中)

发布于 2024-08-28 04:28:46 字数 1497 浏览 7 评论 0原文

我刚刚遇到了这个漂亮的代码,制作了这个散点矩阵图:

alt text
(来源:free.fr

并且想要通过使点的大小/颜色(在下三角形中)根据出现的该类型选项的数量而变化(就像抖动可能给我带来的效果),将其实现为 likret 比例变量(1 到 5 的整数)。

关于如何在基本绘图机制上做到这一点有什么想法吗?

更新:

我做了以下功能,但不知道如何让点的比例始终“好”,你觉得怎么样?

panel.smooth2 <- function (x, y, col = par("col"), bg = NA, pch = par("pch"), 
                    cex = 1, col.smooth = "red", span = 2/3, iter = 3, ...) 
{
    require(reshape)
    z <- merge(data.frame(x,y), melt(table(x ,y)),sort =F)$value
    z <- z/ (4*max(z)) 

    symbols( x, y,  circles = z,#rep(0.1, length(x)), #sample(1:2, length(x), replace = T) ,
            inches=F, bg="blue", fg = bg, add = T)

    # points(x, y, pch = pch, col = col, bg = bg, cex = cex)
    ok <- is.finite(x) & is.finite(y)
    if (any(ok)) 
        lines(stats::lowess(x[ok], y[ok], f = span, iter = iter), 
            col = col.smooth, ...)
}



a1 <- sample(1:5, 100, replace = T)
a2 <- sample(1:5, 100, replace = T)
a3 <- sample(1:5, 100, replace = T)
aa <- data.frame(a1,a2,a3)


pairs(aa , lower.panel=panel.smooth2)

I just came a cross this nice code that makes this scatter matrix plot:

alt text
(source: free.fr)

And wanted to implement it to a likret scale variables (integers of 1 to 5) by making the dot's sizes/colors (in the lower triangle) differ according to how many options of that type occurs (like the effect the jitter might have given me).

Any idea on how to do this on the base plotting mechanism ?

Update:

I made the following function, but don't know how to have the scale of the dots always be "good", what do you think ?

panel.smooth2 <- function (x, y, col = par("col"), bg = NA, pch = par("pch"), 
                    cex = 1, col.smooth = "red", span = 2/3, iter = 3, ...) 
{
    require(reshape)
    z <- merge(data.frame(x,y), melt(table(x ,y)),sort =F)$value
    z <- z/ (4*max(z)) 

    symbols( x, y,  circles = z,#rep(0.1, length(x)), #sample(1:2, length(x), replace = T) ,
            inches=F, bg="blue", fg = bg, add = T)

    # points(x, y, pch = pch, col = col, bg = bg, cex = cex)
    ok <- is.finite(x) & is.finite(y)
    if (any(ok)) 
        lines(stats::lowess(x[ok], y[ok], f = span, iter = iter), 
            col = col.smooth, ...)
}



a1 <- sample(1:5, 100, replace = T)
a2 <- sample(1:5, 100, replace = T)
a3 <- sample(1:5, 100, replace = T)
aa <- data.frame(a1,a2,a3)


pairs(aa , lower.panel=panel.smooth2)

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

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

发布评论

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

评论(2

萌化 2024-09-04 04:28:46

您可以使用“符号”(类似于“lines”、“abline”等方法)。

此方法可以让您在一行代码中对符号大小和颜色进行细粒度控制。

使用“符号”,您可以设置符号大小、颜色和形状。形状和大小是通过传入每个符号大小的向量并将其绑定到“圆形”、“正方形”、“矩形”或“星形”来设置的,例如,“星形”= c(4, 3, 5, 1).颜色用“bg”和/或“fg”设置。

symbols( x, y, circles = circle_radii, inches=1/3, bg="blue", fg=NULL) 

如果我理解你问题的第二部分,你想要合理地确定你用来缩放图中符号的函数是以有意义的方式实现的。 'symbols' 函数根据 'z' 变量(或 data.frame 列等)中的值缩放(例如)圆的半径。在下面的行中,我设置了最大符号大小(半径)为 1/3 英寸——除最大符号外,每个符号的半径都小一些,按该数据点的值与最大值的比率进行缩放。比那个比例 这是一个好的选择吗?我不知道——在我看来,直径或特别是周长可能更好。无论如何,这都是一个微不足道的改变。总之,传入“圆”的“符号”将与“z”坐标成比例地缩放符号的半径——可能最适合连续变量。我会使用颜色('bg')来表示离散变量/因子。

使用“符号”的一种方法是调用绘图函数并传入 type='n',这会创建绘图对象,但禁止绘制符号,以便您接下来可以使用“符号”函数绘制它们。

我不会推荐“cex”用于此目的。 “cex”是文本大小和符号大小的缩放因子,但它影响这两个绘图元素中的哪一个取决于您将其传入的时间 - 如果您通过“par”设置它,那么它会作用于出现的大部分文本在情节上;如果您在“绘图”函数中设置它,那么它会影响符号大小。

You can use 'symbols' (analogous to the methods 'lines', 'abline' et al.)

This method will give you fine-grained control over both symbols size and color in a single line of code.

Using 'symbols' you can set the symbol size, color, and shape. Shape and size are set by passing in a vector for the size of each symbol and binding it to either 'circles', 'squares', 'rectangles', or 'stars', e.g., 'stars' = c(4, 3, 5, 1). Color is set with 'bg' and/or 'fg'.

symbols( x, y, circles = circle_radii, inches=1/3, bg="blue", fg=NULL) 

If i understand the second part of your question, you want to be reasonably sure that the function you use to scale the symbols in your plot does so in a meaningful way. The 'symbols' function scales (for instance) the radii of circles based on values in a 'z' variable (or data.frame column, etc.) In the line below, I set the max symbol size (radius) as 1/3 inches--every symbol except for the largest has a radius some fraction smaller, scaled by the ratio of the value of that dat point over the largest value. than that one in proportion to Is this a good choice? I don't know--it seems to me that diameter or particularly circumference might be better. In any event, that's a trivial change. In sum, 'symbols' with 'circles' passed in will scale the radii of the symbols in proportion to the 'z' coordinate--probably best suited for continuous variables. I would use color ('bg') for discrete variables/factors.

One way to use 'symbols' is to call your plot function and pass in type='n' which creates the plot object but suppresses drawing the symbols so that you can draw them with the 'symbols' function next.

I would not recommend 'cex' for this purpose. 'cex' is a scaling factor for both text size and symbols size, but which of those two plot elements it affects depends on when you pass it in--if you set it via 'par' then it acts on most of the text appearing on the plot; if you set it within the 'plot' function then it affects symbols size.

扛刀软妹 2024-09-04 04:28:46

当然,只需使用cex

set.seed(42)
DF <- data.frame(x=1:10, y=rnorm(10)*10, z=runif(10)*3) 
with(DF, plot(x, y, cex=z))

它就会给你不同的圆圈大小。颜色可以简单地是第四维。

Sure, just use cex:

set.seed(42)
DF <- data.frame(x=1:10, y=rnorm(10)*10, z=runif(10)*3) 
with(DF, plot(x, y, cex=z))

which gives you varying circle sizes. Color can simply be a fourth dimension.

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