1

I have two questions about plotting with ggpairs in r:

(1) I have some unavoidably long variable names that are not shown in full in the default output of ggpairs. How can I adjust ggpairs so that the whole name is visible (e.g. can labels be split over multiple lines, or displayed at 45 degrees, etc.)?

and (2), How do I set a custom range for axis limits for individual variables?

For example, the following code gives us the plot below:

library(GGally)

set.seed(99)

really_long_variable_name_1 <- round(runif(50, 0, 1), 2)
really_long_variable_name_2 <- round(runif(50, 0, 0.8), 2)
really_long_variable_name_3 <- round(runif(50, 0, 0.6), 2)
really_long_variable_name_4 <- round(runif(50, 0, 100), 2)

df <- data.frame(really_long_variable_name_1, 
                    really_long_variable_name_2,
                    really_long_variable_name_3,
                    really_long_variable_name_4)

ggpairs(df) 

example_ggpairs_image

(1) How do I adjust the plot so that full variable names are visible (in this case, the labels on the Y axis)?

and (2) How would I set the axes limits at 0 to 1 for the first three variables, and 0 to 100 for the fourth?

I can set all axes limits to the same values using a function like the one below:

custom_range <- function(data, mapping, ...) { 
  ggplot(data = data, mapping = mapping, ...) + 
    geom_point(...) + 
    scale_x_continuous(limits = c(0, 1)) +
    scale_y_continuous(limits = c(0, 1)) 
}

ggpairs(df, 
        lower = list(continuous = custom_range))

but how would I set axis limits for the fourth variable, really_long_variable_name_4, so that X ranges from 0 to 100?

Many thanks.

monkeytennis
  • 848
  • 7
  • 18

1 Answers1

1

First you can modify your column names to recognize _ as separation points:

cleanname = function(x,lab="\n"){
  sapply(x, function(c){ 
    paste(unlist(strsplit(as.character(c) , split="_")),collapse=lab)
  })
  }

colnames(df) = cleanname(colnames(df))

#Using your function
custom_range <- function(data, mapping, ...) { 
  ggplot(data = data, mapping = mapping, ...) + 
    geom_point(...) + 
    scale_x_continuous(limits = c(0, 1)) +
    scale_y_continuous(limits = c(0, 1)) 
}

Add theme element to specify if you'd like text at an angle, size, color, etc. Note that this is just modifying your X names, but the Y names also have new lines at each _

myplot = ggpairs(df,
        lower = list(continuous = custom_range)) +
 theme(strip.text.x = element_text(size = 6, angle = 45)) 

Next, index each plot and overwrite the existing scale. I'm sure there is a cleaner way of coding this

myplot[4,1]= myplot[4,1] + scale_y_continuous(limits  = c(0,100))
myplot[4,2]= myplot[4,2] + scale_y_continuous(limits  = c(0,100))
myplot[4,3]= myplot[4,3] + scale_y_continuous(limits  = c(0,100))


myplot
Jonni
  • 804
  • 5
  • 16