传说使用ggpairs 2

时间:2015-10-04 00:28:01

标签: r ggplot2 ggally

尽管@ MikeWise对this question的答案我早些时候曾使用过示例数据,但看起来我的实际代码并不正常。

我的代码是:

data(iris)

col.index <- c(1,2,3)

p <- ggpairs(iris, columns = col.index, upper = "blank", legends=T, lower = list(continuous = "points"), diag = "blank",
             axisLabels = "show", 
             colour = "Species",
             columnLabels = c("", "", ""),
             title = "Example") +
  theme_bw() +
  theme(plot.title = element_text(size = 10), axis.title = element_text(size = 10), axis.text = element_text(size = 8), 
        legend.position = "top", legend.title = element_blank())


p1 <- ggally_text("SL") + 
  theme_bw() +
  theme(axis.text = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank())
p2 <- ggally_text("SW") +
  theme_bw() +
  theme(axis.text = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank())
p3 <- ggally_text("PL") +
  theme_bw() +
  theme(axis.text = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank())
p <- putPlot(p,p1,1,1)
p <- putPlot(p,p2,2,2)
p <- putPlot(p,p3,3,3)



GGally:::print_ggpairs_old(p)

colIdx <- c(1,2,3)

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
  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)){
      inner <- getPlot(p, i, j)
      inner <- inner + theme(legend.position=c(length(colIdx)-0.25,0.50)) 
      p <- putPlot(p, inner, i, j)
    }
    else{
      inner <- getPlot(p, i, j)
      inner <- inner + theme(legend.position="none")
      p <- putPlot(p, inner, i, j)
    }
  }
}

GGally:::print_ggpairs_old(p)

传说仍然不存在。此外,情节看起来不像我想要的。使用print()函数,布局成为我期待的。

关于该做什么的任何建议?

1 个答案:

答案 0 :(得分:1)

这有什么问题吗?注意我更改了diag参数。如果您确实需要空白图,则可以在之后编辑>调整图例。

library(GGally)
data(iris)

col.index <- c(1,2,3)

p <- ggpairs(iris, columns = col.index, 
             upper = "blank", legends=T, 
             lower = list(continuous = "points"), 
             diag = list(continuous = "density"),
             axisLabels = "show", 
             colour = "Species",

             columnLabels = c("", "", ""),
             title = "Example") +
  theme_bw() +
  theme(plot.title = element_text(size = 10), 
        axis.title = element_text(size = 10), 
        axis.text = element_text(size = 8), 
        legend.position = "top", 
        legend.title = element_blank())


# p1 <- ggally_text("SL") + 
#   theme_bw() +
#   theme(axis.text = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank())
# p2 <- ggally_text("SW") +
#   theme_bw() +
#   theme(axis.text = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank())
# p3 <- ggally_text("PL") +
#   theme_bw() +
#   theme(axis.text = element_blank(), panel.grid = element_blank(), axis.ticks = element_blank())
# p <- putPlot(p,p1,1,1)
# p <- putPlot(p,p2,2,2)
# p <- putPlot(p,p3,3,3)


###THE IT TURNS INTO

GGally:::print_ggpairs_old(p)

colIdx <- c(1,2,3)

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
  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)){
      inner <- getPlot(p, i, j)
      inner <- inner + 
               theme(legend.position=c(length(colIdx)-0.25,0.50)) 
      p <- putPlot(p, inner, i, j)
    }
    else{
      inner <- getPlot(p, i, j)
      inner <- inner + 
               theme(legend.position="none")
      p <- putPlot(p, inner, i, j)
    }
  }
}

GGally:::print_ggpairs_old(p)

enter image description here