0

I am a newbie for stack Overflow and language.

Here is my problem. I now have a with one variable called Type and other 14 variables whose correlation matrix needed to be calculated.

origin dataset

I already have an overall using , and the theme is default theme_grey but fine for me to view. The code is :

m<- melt(get_lower_tri(round(cor(xrf[3:16], method = 'pearson', use = 'pairwise.complete.obs'), 2)),na.rm = TRUE)
ggplot(m, aes(Var1, Var2, fill = value)) + 
  geom_tile() + 
  scale_fill_gradient2(low = 'skyblue4', 
                       high = 'coral2', 
                       mid = 'white', 
                       midpoint = 0,
                       limit = c(-1, 1), 
                       space = "Lab", 
                       name = 'Person\nCorrelation') + 
  theme_grey()+
  coord_fixed() +
  theme(axis.title = element_blank())

The result is fine and the background looks good to view.

overall Correlation Matrix

But when I managed to generate a grouped correlation matrix heatmap, I found that no matter how hard I tried (using theme(panel.background = element_rect()) or theme(panel.background = element_blank())), the subplot backgrounds won’t change and remain this ugly grey which is even different from the overall one.

Facet correlation matrix Here is my code:


Type = rep(c('(a)', '(b)', '(c)','(d)', '(e)', '(f)', '(g)', '(h)', '(i)', '(j)'), each = 14^2)

# Get lower triangle of the correlation matrix
get_lower_tri<-function(x){
  x[upper.tri(x)] <- NA
  return(x)
}

df2 <- do.call(rbind, lapply(split(xrf, xrf$Type), 
                             function(x) melt(get_lower_tri(round(cor(x[3:16], method = 'pearson', use = 'pairwise.complete.obs'), 2)),na.rm = FALSE)))

my_cors <- cbind(Type,df2)

my_cors %>% 
  ggplot(aes(Var1, Var2, fill = value)) + 
  geom_tile() + 
  scale_fill_gradient2(low = 'skyblue4', 
                       high = 'coral2', 
                       mid = 'white', 
                       midpoint = 0,
                       limit = c(-1, 1), 
                       space = "Lab", 
                       name = 'Person\nCorrelation') + 
  theme_grey()+
  coord_fixed() +
  theme(axis.title = element_blank(),
        panel.background = element_rect(fill = 'grey90',colour = NA))+
  facet_wrap("Type",ncol = 5, nrow = 2)

Isn’t the facet subplot backgrounds the same as the overall one if using the same theme? And how can I change it?

Update:sorry! It’s my first time to raise a question and it’s not a good one!

xrf is my original dataset...But now I have figured out why thanks to Tjebo and those who comment my faulty questions.It’s very instructive to me!!

scale_fill_gredient2(...,na.value = 'transparent') will solve it.The default value of this parameter is "grey50" which I took as the background color.

I am truly sorry for asking such a silly question, and I really really appreciate you guys’s nice comment for a rookie! Thank you guys!

James
  • 1
  • 2
  • Hi James. Welcome to StackOverflow! Please read the info about [how to ask a good question](https://stackoverflow.com/help/how-to-ask) and how to give a [minimale reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). The idea behind a **minimal** reproducible example is to provide as little code/text as possible while still replicating the problem. That way you can help others to help you! – dario Feb 23 '20 at 11:24
  • When I try using the code you supplied I get `Error in split(xrf, xrf$Type) : object 'xrf' not found` on the line `df2 <- do.call(r`. Please make sure the MRE you show does actually work. – dario Feb 23 '20 at 12:00
  • Welcome to SO. Your question seems generally nice, but is not yet quite reproducible. What is the object `xrf` ? The best is to use the `reprex` package in order to create fully reproducible examples. - it is integrated in RStudio if you should use this. Have a look at the `ggplotcorr` package - it may save you from calculating all the correlations yourself – tjebo Feb 23 '20 at 12:01
  • it's possible that by faceting you are introducing NA values that geom_tile is drawing not transparently. possibly related https://stackoverflow.com/questions/6906661/ggplot2-make-missing-value-in-geom-tile-not-blank – tjebo Feb 23 '20 at 12:08

0 Answers0