如何将外部图例添加到ggpairs()?

时间:2014-04-08 18:48:06

标签: r plot ggally

我正在使用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)

我得到以下情节:

enter image description here

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

2 个答案:

答案 0 :(得分:11)

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

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

    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)
        }
      }
    }
    

答案 1 :(得分:7)

希望有人能说明ggpairs(...)如何做到这一点。我想亲眼看看。在此之前,这是一个不使用ggpairs(...)的解决方案,而是使用facet的普通香草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(...) df ggplot xx yy的正确格式,制作两份副本(ww和{{1} }}并运行基于idgroup的笛卡尔联接(此处,id只是一个行号,groupRegion变量),创建zz。我们需要在外部计算和缩放密度(在数据表d中)。尽管如此,它仍然比ggpairs(...)跑得快。