Legend using ggpairs

一曲冷凌霜 提交于 2019-12-03 21:47:35
Mike Wise

There should be a standard way to to it, but I could not find one. The old print function is still there and accessible but as in internal function. Try this instead of the print:

  GGally:::print_ggpairs_old(p)

And if you add this (thanks to this post answer Legend using ggpairs )

colidx <- c(3,5,6,7)
for (i in 1:length(colidx)) {

  # Address only the diagonal elements
  # Get plot out of plot-matrix
  inner <- getPlot(p, i, i);

  # Add ggplot2 settings (here we remove gridlines)
  inner <- inner + theme(panel.grid = element_blank()) +
    theme(axis.text.x = element_blank())

  # Put it back into the plot-matrix
  p <- putPlot(p, inner, i, i)

  for (j in 1:length(colidx)){
    if((i==1 & j==1)){

      # Move the upper-left legend to the far right of the plot
      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 the other legends
      inner <- getPlot(p, i, j)
      inner <- inner + theme(legend.position="none")
      p <- putPlot(p, inner, i, j)
    }
  }
}

You get something much better:

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!