0

I'm developing a R visual tool for correlation matrices and am stuck getting the entire plot to come in blue. I'm trying to get it to work no matter what dataset columns you throw at it. I've developed a few different ways to color code for various different plots in the matrix but I'm stuck on the plots for categorical variables above the axis (which are box plots, in this case). Does anyone have any ideas on how to get the dots on those graphs to come in blue?

I'm also bound by the libraries I can have, so I can't use gginnards: https://learn.microsoft.com/en-us/power-bi/connect-data/service-r-packages-support

Here is my code and the visual so far:

Correlation Matrix

ERRORCHECK = FALSE

options(warn=0)

library(ggplot2); library(GGally);

for (i in colnames(Values)){   Values[[i]] <- unlist(Values[[i]]) }

p <- ggpairs(Values, mapping = ggplot2::aes(fill = "cornflowerblue", alpha = .5))

for(i in 1:p$nrow) {   for(j in 1:p$ncol){
    p[i,j] <- p[i,j] + scale_fill_manual(values="cornflowerblue")   } }

f <- sapply(Values, is.factor)


middle <- p$ncol/2 middleround <- ceiling(middle) even <- FALSE

if (middle == middleround) {   even <- TRUE }


whereami <- function(i, j, ifprint) {   #TESTING WHERE WE ARE IN THE GRAPH
     axis = FALSE   TOP = FALSE   BOTTOM = FALSE
     if (even==TRUE) {
    
    if (i==j){
      
      axis = TRUE
      
    } else if (j>i) {
      
      TOP = TRUE
      
      
    } else if (i>j) {
      
      BOTTOM = TRUE
      
    } else {
      
      #ERROR
      print("You shoulnd't see this. EVEN = TRUE")
      
    }
       }  else if (even==FALSE) {
    
    if (i==j){
      
      axis = TRUE
      
    } else if (j>i) {
      
      TOP = TRUE
      
      
    } else if (i>j) {
      
      BOTTOM = TRUE
      
    } else {
      
      #ERROR
      print("You shoulnd't see this. EVEN = FALSE")
      
    }   } else {
    
    print("You shuound't see this. Even is neither TRUE not FALSE")
       }
     if (ifprint == TRUE){
    print("----")
    print("axis")
    print(axis)
    print("top")
    print(TOP)
    print("bottom")
    print(BOTTOM)
    print("row")
    print(i)
    print("column")
    print(j)
    
    rowname <- colnames(Values)[i]
    colname <- colnames(Values)[j]
    
    print("rowname")
    print(rowname)
    print("colname")
    print(colname)
       }
     returnlist <- list("bottom" = BOTTOM, "top" = TOP, "axis" = axis)
     return(returnlist)    }

for(i in 1:p$nrow) {   for(j in 1:p$ncol){
    
    wheream <- whereami(i,j, FALSE)
    
    BOTTOM <- wheream$bottom
    TOP <- wheream$top
    axis <- wheream$axis
    
    if (i==j) {
      #AXIS
      
      # DO NOTHING
      
    } else if (!(f[i])&!(f[j])) {
      #BOTH CONTINUOUS
      
      # ABOVE AXIS, NO CHANGE. BELOW AXIS, ADD GEOM POINT AND COLOUR MANUAL
      
      if (BOTTOM == TRUE) {
        
        p[i,j] <- p[i,j] + geom_point(aes(colour = "cornflowerblue")) + 
          scale_colour_manual(values = "cornflowerblue")
        
      } else if (TOP == TRUE) {
        
        # DO NOTHING
        
      } else {
        
        print("You shouldn't see this. Both are continuous, neither bottom nor top is true")
        
      }
      
    } else if ((!(f[i])&(f[j]))) {
      #ROW CONTINUOUS, COLUMN DISCRETE
      
      if (BOTTOM == TRUE) {
        
        # DO NOTHING
        
      } else if (TOP == TRUE) {
        
        # Discrete-continuous top. Still working on this.
        
      } else {
        
        print("You shouldn't see this. Row is continuous, column discrete, neither bottom nor top is true")
        
      }
      
    } else if ((f[i])&!(f[j])) {
      #ROW DISCRETE, COLUMN CONTINUOUS
      
      if (BOTTOM == TRUE) {
        
        # DO NOTHING
        
      } else if (TOP == TRUE) {
        
        # Discrete-continuous top. Still working on this.
        
      } else {
        
        print("You shouldn't see this. Row is discrete, column continuous, neither bottom nor top is true")
        
      }
      
    } else if ((f[i])&(f[j])) {
      #BOTH DISCRETE
      
      if (BOTTOM == TRUE) {
        
        # DO NOTHING
        
      } else if (TOP == TRUE) {
        
        # DO NOTHING
        
      } else {
        
        print("You shouldn't see this. Both discrete, neither bottom nor top is true")
        
      }
      
    }  else {
      #ERROR HANDLING
      
      print("You shouln't see this message. ")
      
    }
       } }

errorchecker <- function(nothing){
       #ERROR CHECKER
     print("BEGINNING ERROR CHECKING")
     for(i in 1:p$nrow) {
    for(j in 1:p$ncol){
      tryCatch(
        expr = {
          print(p[i,j])
        },
        error = function(e){
          print("ERROR AT:")
          wheream <- whereami(i,j, TRUE)
        }
      )
    }   }
     print("DONE ERROR CHECKING")    }

if (ERRORCHECK == TRUE){ errorchecker() }


p
  • Can you share your data? – Golem Jul 08 '23 at 10:54
  • @Golem I can't share the dataset I used to make the visual because it's proprietary. But the whole point of the visual is that it should work on any dataset. If you want to point me towards a public dataset I can run the visualization on that dataset and post the results. – Lukas Taylor Jul 08 '23 at 11:01

1 Answers1

0

You don't need to do all that iterating through plots. GGally has a built-in mechanism to allow you to specify the plots you want in each location and the (type-dependent) parameters for each plot type.

In your case the dots are black because the dots in a boxplot are coloured according to the parameter outlier.colour, which is set to black by default. You can change it by specifying the outlier colour in the arguments to lower and upper in ggpairs.

Here's a full reprex using the built-in iris data set.

library(GGally)

blu <- 'cornflowerblue'

prefs <- list(continuous = wrap('points', color = blu),
              combo = wrap('box', fill = blu, outlier.color = blu))
     
ggpairs(iris, lower = prefs, upper = prefs, 
        diag = list(continuous = wrap('densityDiag', fill = blu),
                    discrete = wrap('barDiag', fill = blu)))

enter image description here

If we change blu <- 'cornflowerblue' to blu <- 'red', the same code gives us:

enter image description here

Note that the outlier points in the box plots (the ones that are black in your example) are now colored appropriately.

Created on 2023-07-08 with reprex v2.0.2

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87