具有组直方图的scatterplotMatrix [英] scatterplotMatrix with group histograms
问题描述
很容易建立一个很好的巨大散点图矩阵,其对角线下方的直方图如下所示:
It's pretty easy to build a nice huge scatterplot matrix with histograms down the diagonal for multivariate data as follows:
scatterplotMatrix(somedata[1:points.count,],groups=somedata[1:points.count,class],
by.groups=TRUE,diagonal="histogram")
According to the documentation though, it doesn't seem possible to divide up the histogram by the group labels as is done in this question. How would you do that using scatterplotMatrix or a similar function?
推荐答案
这是您要记住的吗?
使用虹膜数据集:
library(ggplot2)
library(data.table)
library(reshape2) # for melt(...)
library(plyr) # for .(...)
xx <- with(iris, data.table(id=1:nrow(iris), group=Species,
Sepal.Length, Sepal.Width,Petal.Length, Petal.Width))
# reshape for facetting with ggplot
yy <- melt(xx,id=1:2, variable.name="H", value.name="xval")
yy <- data.table(yy,key="id,group")
ww <- yy[,list(V=H,yval=xval),key="id,group"]
zz <- yy[ww,allow.cartesian=T]
setkey(zz,H,V,group)
zz <- zz[,list(id, group, xval, yval, min.x=min(xval), min.y=min(yval),
range.x=diff(range(xval)),range.y=diff(range(yval))),by="H,V"]
# points colored by group (=species)
# density plots for each variable by group
d <- zz[H==V, list(x=density(xval)$x,
y=mean(min.y)+mean(range.y)*density(xval)$y/max(density(xval)$y)),
by="H,V,group"]
ggp = ggplot(zz)
ggp = ggp + geom_point(subset =.(H!=V),
aes(x=xval, y=yval, color=factor(group)),
size=3, alpha=0.5)
ggp = ggp + geom_line(subset = .(H==V), data=d, aes(x=x, y=y, color=factor(group)))
ggp = ggp + facet_grid(V~H, scales="free")
ggp = ggp + scale_color_discrete(name="Species")
ggp = ggp + labs(x="", y="")
ggp
我一直听说使用GGally软件包中的ggpairs(...)
可能会发生同样的事情.我希望看到一个实际的例子.该文档是难以理解的.另外,ggpairs(...)
(在我手中)非常慢,尤其是对于大型数据集.
I keep hearing that the same thing is possible using ggpairs(...)
in package GGally. I would love to see an actual example of it. The documentation is inscrutable. Also, ggpairs(...)
is extremely slow (in my hands), especially with large datasets.
这篇关于具有组直方图的scatterplotMatrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!