12

我正在使用ggpairs. 我正在使用以下代码:

# Load required packages
require(GGally)

# Load datasets
data(state)
df <- data.frame(state.x77,
                 State = state.name,
                 Abbrev = state.abb,
                 Region = state.region,
                 Division = state.division
) 
# Create scatterplot matrix
p <- ggpairs(df, 
             # Columns to include in the matrix
             columns = c(3,5,6,7),

             # What to include above diagonal
             # list(continuous = "points") to mirror
             # "blank" to turn off
             upper = "blank",
             legends=T,

             # What to include below diagonal
             lower = list(continuous = "points"),

             # What to include in the diagonal
             diag = list(continuous = "density"),

             # How to label inner plots
             # internal, none, show
             axisLabels = "none",

             # Other aes() parameters
             colour = "Region",
             title = "State Scatterplot Matrix"
) 

# Show the plot
print(p)

我得到以下情节:

在此处输入图像描述

现在,可以很容易地看到我正在为矩阵中的每个图获取图例。我希望整个情节只有一个普遍的传说。我怎么做?任何帮助将非常感激。

4

2 回答 2

12

我正在做类似的事情,这是我会采取的方法,

  1. ggpairs确保在函数调用中将图例设置为“TRUE”
  2. 现在遍历绘图矩阵中的子图并删除每个图例并仅保留其中一个,因为密度都绘制在同一列上。

    colIdx <- c(3,5,6,7)
    
    for (i in 1:length(colIdx)) {
    
      # Address only the diagonal elements
      # Get plot out of matrix
      inner <- getPlot(p, i, i);
    
      # Add any ggplot2 settings you want (blank grid here)
      inner <- inner + theme(panel.grid = element_blank()) +
        theme(axis.text.x = element_blank())
    
      # Put it back into the matrix
      p <- putPlot(p, inner, i, i)
    
      for (j in 1:length(colIdx)){
        if((i==1 & j==1)){
    
          # Move legend right
          inner <- getPlot(p, i, j)
          inner <- inner + theme(legend.position=c(length(colIdx)-0.25,0.50)) 
          p <- putPlot(p, inner, i, j)
        }
        else{
    
          # Delete legend
          inner <- getPlot(p, i, j)
          inner <- inner + theme(legend.position="none")
          p <- putPlot(p, inner, i, j)
        }
      }
    }
    
于 2014-04-12T00:21:56.540 回答
8

希望有人会展示如何使用ggpairs(...). 我想亲眼看看。在那之前,这里有一个不使用的解决方案,而是使用带有刻面的ggpairs(...)普通香草ggplot

library(ggplot2)
library(reshape2)   # for melt(...)
library(plyr)       # for .(...)
library(data.table)

xx <- with(df, data.table(id=1:nrow(df), group=Region, df[,c(3,5,6,7)]))
yy <- melt(xx,id=1:2, variable.name="H", value.name="xval")
setkey(yy,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"]
d  <- zz[H==V,list(x=density(xval)$x,
                   y=min.y+range.y*density(xval)$y/max(density(xval)$y)),
         by="H,V,group"]
ggplot(zz)+
  geom_point(subset= .(xtfrm(H)<xtfrm(V)), 
             aes(x=xval, y=yval, color=factor(group)), 
             size=3, alpha=0.5)+
  geom_line(subset= .(H==V), data=d, aes(x=x, y=y, color=factor(group)))+
  facet_grid(V~H, scales="free")+
  scale_color_discrete(name="Region")+
  labs(x="", y="")

基本思想是为( ) 设置正确melt(...)的格式,制作两个副本 ( and ) 并基于 and 运行笛卡尔连接(这里只是一个行号并且是变量),以创建. 我们确实需要在外部计算和缩放密度(在数据表中)。尽管如此,它的运行速度仍然比.dfggplotxxyywwidgroupidgroupRegionzzdggpairs(...)

于 2014-04-08T20:07:30.867 回答