r - How to add an external legend to ggpairs()? -
i plotting scatterplot matrix using ggpairs
. using following code:
# 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 include in matrix columns = c(3,5,6,7), # include above diagonal # list(continuous = "points") mirror # "blank" turn off upper = "blank", legends=t, # include below diagonal lower = list(continuous = "points"), # include in diagonal diag = list(continuous = "density"), # how label inner plots # internal, none, show axislabels = "none", # other aes() parameters colour = "region", title = "state scatterplot matrix" ) # show plot print(p)
and following plot:
now, 1 can see getting legends every plot in matrix. have 1 universal legend whole plot. how do that? appreciated.
i working on similar, approach take,
- ensure legends set 'true' in
ggpairs
function call now iterate on subplots in plot matrix , remove legends each of them , retain 1 of them since densities plotted on same column.
colidx <- c(3,5,6,7) (i in 1:length(colidx)) { # address diagonal elements # plot out of matrix inner <- getplot(p, i, i); # add ggplot2 settings want (blank grid here) inner <- inner + theme(panel.grid = element_blank()) + theme(axis.text.x = element_blank()) # put matrix p <- putplot(p, inner, i, i) (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) } } }
Comments
Post a Comment