1

I am plotting a large number of Spearman's correlation analyses in R. I want to include the R value label in the plot, but I don't want to include the p value. The sample sizes for my actual plots are all large, so all the p values are small and their inclusion doesn't add anything useful to the plot. After creating the individual plots I am using gridExtra::grid.arrange to combine plots and the extra label length is getting in the way (cleaner to include the information outside of the plot itself).

I've seen ways to alter the label size and position, but haven't found anything that limits or adjusts the output of the label contents. Is it possible to drop the p value from the plot in stat_cor but keep the R? Is there another package that would allow more customization?

testplot

Here's the basic code I'm using to produce the plot.

df %>% 
ggscatter(x = "Diameter", y = "Depth", 
            add = "reg.line", conf.int = TRUE,
            title = "test",
            xlab = "Diameter (m)", ylab = "Depth (m)",
            shape = 21, fill = "lightgray", color = "black", size = 3) +
  stat_cor(method = "spearman", label.x = 0.45, label.y = 1.2) +
  coord_cartesian(ylim = c(0,1.25), xlim = c(0,0.7)) +
  theme_minimal()

Here is basically the same setup using the iris data set.

enter image description here

ggscatter(data = iris,x = "Sepal.Length", y = "Sepal.Width", 
                     add = "reg.line", conf.int = TRUE,
                     title = "Iris test",
                     xlab = "Length", ylab = "Width",
                     shape = 21, fill = "lightgray", color = "black", size = 3) +
  stat_cor(method = "spearman") +
  theme_minimal()
Corey
  • 405
  • 2
  • 6
  • 18

1 Answers1

2

Found some hints from this github issue: https://github.com/kassambara/ggpubr/issues/32 And then playing around with the ggplot object to see all the different attributes being built.

ggscatter(data = iris,x = "Sepal.Length", y = "Sepal.Width", 
          add = "reg.line", conf.int = TRUE,
          title = "Iris test",
          xlab = "Length", ylab = "Width",
          shape = 21, fill = "lightgray", color = "black", size = 3)+
  stat_cor(r.digits = 2, method = "spearman",
           aes(label = paste("'R = '", ..r.., sep = " "))
  )

enter image description here

JD Caddell
  • 383
  • 2
  • 10
  • Trying to figure out if this results in Spearman's rho or Pearson's r. Don't see where the correlation method is specified. – Corey May 04 '20 at 05:40
  • I adjusted the post to call that method. You do it the same way as you had before, inside the stat_cor function call. – JD Caddell May 04 '20 at 10:56
  • 1
    Getting this in my actual data set. Trying to figure out what the issue is. `Error in parse(text = text[[i]]) : :1:8: unexpected numeric constant 1: 'R = ' 0.32` – Corey May 08 '20 at 10:07
  • Tough to guess on this end, I’m guessing a small typo somewhere. Maybe try different sets of quotes? This thread has some suggestions on similar errors. https://stackoverflow.com/questions/25889234/error-unexpected-symbol-input-string-constant-numeric-constant-special-in-my-co – JD Caddell May 09 '20 at 11:28