在ggduo散点图矩阵中显示相关指数(带有颜色) [英] Show correlation index in ggduo scatterplot matrix (with coloring)
问题描述
基于
要在@aosmith的答案中对齐图像中的相关标签,
#,但已修改PointsWithCor<-函数(数据,映射,...,方法=皮尔逊"){df<-data.frame(x = eval(映射$ x,数据),y = eval(映射$ y,数据),c = eval(映射$ color,数据))xPos =分钟(df $ x)yPos = max(df $ y)sumdf<-df%&%;%group_by(c)%&%;%总结(实验室=圆(cor(x,y),3),x = xPos,y = yPos * min(as.numeric(c))/max(as.numeric(df $ c)))ggally_points(数据,映射,...)+ggplot2 :: geom_label(数据= sumdf,映射= ggplot2 :: aes(x = x,y = y,标签= lab,颜色= c),恰好= 0,恰好= 1,大小= 5,字体=粗体",Inherit.aes = FALSE#不从...继承任何东西)}
这是一种方法.我发现关键是要按组估算标签值和轴位置.我使用了来自 dplyr 的帮助程序功能进行分组和汇总.
否则,这与您所做的相似,即使用情节中的 mapping
.我将映射( x
, y
, colour
)存储在data.frame中,以便进行汇总.
您可能需要处理轴的位置.您会看到min x和max y对于所有这些都不真正起作用.您可能决定以其他方式计算它们.
这是我做的功能:
库(GGally)图书馆(dplyr)points_with_cor_color =函数(数据,映射,...,方法=皮尔逊"){dat = data.frame(x = data [,as.character(mapping $ x)],y = data [,as.character(mapping $ y)],color = data [,as.character(mapping $ colour)])sumdat = dat%&%;%group_by(颜色)%>%summarise(lab = round(cor(x,y,method = method),3),x = min(x,na.rm = TRUE),y = max(y,na.rm = TRUE))ggally_points(数据,映射,...)+ggplot2 :: geom_label(数据=求和映射= ggplot2 :: aes(x = x,y = y,标签=实验室,颜色=颜色),恰好= 0,恰好= 1,size = 5,fontface ="bold",#不继承...Inherit.aes = FALSE)}
这是带有 ggduo()
的绘图代码.
ggduo(df,columnsX = 1:2,columnY = 3:5,映射= ggplot2 :: aes(color = f),类型=列表(连续= points_with_cor_color))
Based on this post, now that I have
library (GGally)
# from help
PointsWithCor <- function(data, mapping, ..., method = "pearson") {
x <- eval(mapping$x, data)
y <- eval(mapping$y, data)
cor <- cor(x, y, method = method)
ggally_points(data, mapping, ...) +
ggplot2::geom_label(
data = data.frame(
x = min(x, na.rm = TRUE),
y = max(y, na.rm = TRUE),
lab = round(cor, digits = 3)
),
mapping = ggplot2::aes(x = x, y = y, label = lab),
hjust = 0, vjust = 1,
size = 5, fontface = "bold",
inherit.aes = FALSE # do not inherit anything from the ...
)
}
# data frame
df = data.frame(runif(100),
rnorm(100),
rgamma(100,1,2),
rt(100,1),
rf(100,1,2),
as.factor(round(runif(100,0,1))))
colnames(df) = c("a","b","c","d","e","f")
# points + cor, but only one cor index
ggduo(df,columnsX = 1:2, columnsY = 3:5,
mapping = aes(colour = f),
types = list(continuous = PointsWithCor))
but it produces a matrix of scatterplot with correlation in all x and all y. I'd like to show correlations colored by the same way to coloring the points in scatterplots.
I think it needs to modify the function to use the colour
attribute in mapping, but not sure how to do it. Could anyone please give me a suggestion?
Edit:
To align the correlation labels in the image in the answer from @aosmith,
# from help but modified
PointsWithCor <- function(data, mapping, ..., method = "pearson") {
df <- data.frame(x = eval(mapping$x, data), y = eval(mapping$y, data), c = eval(mapping$colour, data))
xPos = min(df$x)
yPos = max(df$y)
sumdf <- df %>%
group_by(c) %>%
summarise(
lab = round(cor(x, y),3),
x = xPos,
y = yPos*min(as.numeric(c))/max(as.numeric(df$c))
)
ggally_points(data, mapping, ...) +
ggplot2::geom_label(
data = sumdf,
mapping = ggplot2::aes(x = x, y = y, label = lab, color = c),
hjust = 0, vjust = 1,
size = 5, fontface = "bold",
inherit.aes = FALSE # do not inherit anything from the ...
)
}
Here is one approach. I found the key to be estimating the label values and the axis locations by group. I used helper functions from dplyr for the grouping and summarizing.
Otherwise this is similar to what you did, working with the mapping
from the plot. I store the mappings (x
, y
, colour
) in a data.frame so I can do the summarizing.
You'll likely want to work on the axis position placement. You'll see the min x and max y doesn't really work for all of these. You might decide to calculate them a different way.
Here is the function I made:
library(GGally)
library(dplyr)
points_with_cor_color = function(data, mapping, ..., method = "pearson") {
dat = data.frame(x = data[, as.character(mapping$x)],
y = data[, as.character(mapping$y)],
color = data[, as.character(mapping$colour)])
sumdat = dat %>%
group_by(color) %>%
summarise(lab = round(cor(x, y, method = method), 3),
x = min(x, na.rm = TRUE),
y = max(y, na.rm = TRUE) )
ggally_points(data, mapping, ...) +
ggplot2::geom_label(
data = sumdat,
mapping = ggplot2::aes(x = x, y = y, label = lab, color = color),
hjust = 0, vjust = 1,
size = 5, fontface = "bold", # do not inherit anything from the ...
inherit.aes = FALSE
)
}
And here is the plotting code with ggduo()
.
ggduo(df,columnsX = 1:2, columnsY = 3:5,
mapping = ggplot2::aes(color = f),
types = list(continuous = points_with_cor_color))
这篇关于在ggduo散点图矩阵中显示相关指数(带有颜色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!