1

Although @MikeWise´s answer to this question I asked earlier worked with example data, it looks like not working on my real code.

My code is:

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)

The legend is still not there. In addition, the plot does not look like what I want. Using the function print() the layout become what I am expecting for.

Any advice about what to do?

Community
  • 1
  • 1
  • Um, what do you think? – Mike Wise Oct 04 '15 at 17:34
  • Anything wrong with the answer? – Mike Wise Oct 05 '15 at 18:17
  • @Mike sorry. I could not go through it yet. But, trying what you suggest gives me a diag with density plots, and even if I try to get rid of it after, the legend desappear again. In addition, the legend becomes with lines, not with points, that is what I want. I do not know if it is possible or not what I want to. I am working in another code now, but I will work more on this one later to try solve this. – Ana Carolina Pessoa Oct 06 '15 at 19:51
  • It is easily possible. You just move a different legend. You do understand what this code is doing, right? – Mike Wise Oct 07 '15 at 08:34

1 Answers1

1

Anything wrong with this? Note I changed the diag parameter. If you really need the blank plots instead, you could edit them in after you adjust the legends.

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

Mike Wise
  • 22,131
  • 8
  • 81
  • 104