No method for merging theme into element_rect

Hi @cloejoly
Solution from @nirgrahamuk works like a charm. But running your code on your data, I saw a correction to make it work.

First comment though about your posting. If you post a question in the future, these are the main steps to make sure that people will be able to replicate the issue:

But I did this for you below and inserted my comments in your code:

  • Don’t rename the “value2” variable since you’re using it as argument in your plot
  • Replace po.nopanel by nirgrahamuk’ solution

If you run the whole block below, it should work.

If so, please flag @nirgrahamuk 's answer as the right solution.

nba <- data.frame(
  stringsAsFactors = FALSE,
  CRI = c(1.270642202,0.879781421,1.064102564,1.278431373,1.296089385,
          1.19375,1.197115385,1.054621849,1.089171975,1.159322034,
          1.278688525,1.180327869,1.358552632,1.313829787,
          1.48125,1.399038462,1.433823529,1.362831858,
          1.512676056,1.768844221,1.025423729,0.837150127),
  DS = c(13.83366893,71.49250489,14.0998227,
         39.13119004,3.242757601,-2.087402026,
         33.39066188,46.83488485,-5.118630703,
         -0.756084512,8.940206386,14.05813226,
         -10.7906219,-33.34665086,-30.31691381,
         -16.35804279,-44.79579312,-35.19178819,
         -33.54672267,-60.70658978,89.49860116,
         19.603705),
  Ascaris = c(0,3.5,0.666666667,0,9.25,0,0,0.5,0,
              0,1.5,0,0,3,2,17.33333333,5,14.5,
              0.333333333,1,0,7.333333333),
  Coccidias = c(0,3,5.666666667,2,1.5,0.5,0.666666667,
                0.5,3.5,0.5,6.5,2,0.5,1.5,0.5,
                4.333333333,0.333333333,1.5,
                1.666666667,2.333333333,2,1),
  Strongyles = c(0,7,6.333333333,10.33333333,1.75,3,
                 12.66666667,3.5,16.5,24.5,11,5,14,11,
                 5,19.33333333,8.666666667,8,
                 2.333333333,1.666666667,7,1.666666667),
  partners = c(16,16,16,15,13,5,14,14,13,16,7,8,
               7,16,13,6,16,13,15,15,17,10),
  age = c(17,17,12,11,10,10,10,9,9,10,20,4,
          2,2,1,1,1,2,1,0.5,3,9)
)


# Function to get the probability into a whole matrix not half, here is Spearman you can change it to Kendall or Pearson
cor.prob.all <- function (X, dfr = nrow(X) - 2) {
  R <- cor(X, use="pairwise.complete.obs",method="spearman")
  r2 <- R^2
  Fstat <- r2 * dfr/(1 - r2)
  R<- 1 - pf(Fstat, 1, dfr)
  R[row(R) == col(R)] <- NA
  R
}
# Change matrices to dataframes
#nba = Dataf
nbar<- as.data.frame(cor(nba[1:ncol(nba)]),method="spearman") # to a dataframe for r^2
nbap<- as.data.frame(cor.prob.all(nba[1:ncol(nba)])) # to a dataframe for p values
# Reset rownames
nbar <- data.frame(row=rownames(nbar),nbar) # create a column called "row" 
rownames(nbar) <- NULL
nbap <- data.frame(row=rownames(nbap),nbap) # create a column called "row" 
rownames(nbap) <- NULL
# Melt
library(reshape2)
nbar.m <- melt(nbar)
nbap.m <- melt(nbap)
# Classify (you can classify differently for nbar and for nbap also)         
nbar.m$value2<-cut(nbar.m$value,breaks=c(-1,-0.75,-0.5,-0.25,0,0.25,0.5,0.75,1),include.lowest=TRUE, label=c("(-0.75,-1)","(-0.5,-0.75)","(-0.25,-0.5)","(0,-0.25)","(0,0.25)","(0.25,0.5)","(0.5,0.75)","(0.75,1)")) # the label for the legend
nbap.m$value2<-cut(nbap.m$value,breaks=c(-Inf, 0.001, 0.01, 0.05),label=c("***", "** ", "*  ")) 
nbar.m<-cbind.data.frame(nbar.m,nbap.m$value,nbap.m$value2) # adding the p value and its cut to the first dataset of R coefficients

## --> [XV COMMENT]: you need to remove the two lines below, your ggplot fill argument mentions Value2, so let's keep it this way
#names(nbar.m)[4]<-paste("valuep") # change the column names of the dataframe 
#names(nbar.m)[4]<-paste("signif.")
nbar.m$row <- factor(nbar.m$row, levels=rev(unique(as.character(nbar.m$variable)))) # reorder the variable factor
# Plotting the matrix correlation heatmap
# Set options for a blank panel

## --> [XV COMMENT]: changed to nirgrahamuk's solution, works like a charm: 
po.nopanel <-list(theme(panel.background=element_blank(),panel.grid.minor=element_blank(),panel.grid.major=element_blank()))

pa<-ggplot(nbar.m, aes(row, variable)) +
  geom_tile(aes(fill=value2),colour="white") +
  scale_fill_brewer(palette = "RdYlGn",name="Correlation")+ # RColorBrewer package
  #theme(axis.text.x=theme_text(angle=-90))+
  po.nopanel
pa
1 Like