No method for merging theme into element_rect

Hi Community !
I'm having an issue but I'm not sure what is the problem...
Here is my script (modified from an other old one) :

# 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)
??Rcpp_precious_remove
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
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
po.nopanel <-list(theme(panel.background=theme_void(),panel.grid.minor=theme_void(),panel.grid.major=theme_void()))
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

Everything works fine until this answer appears... :

> pa # check the first plot
Error : No method for merging theme into element_rect

I don't understand what is the probleme here.
I think it is because the script is old and the function (which one ?) probably doesn't exist anymore. So how should I edit my new script ?

Thanks a lot,
Cloé

Hi Cloé, do you mind sharing the data? Or a subset of it? Just to know what Dataf looks like.

I believe you have simply confused the use of theme_void with element_blank.

example of your issue:

library(tidyverse)

po.nopanel <-list(theme(panel.background=theme_void(),panel.grid.minor=theme_void(),panel.grid.major=theme_void()))

pa<-ggplot(iris, aes(Sepal.Length, Petal.Length)) +
  geom_tile(aes(fill=Species),colour="white") +
  scale_fill_brewer(palette = "RdYlGn",name="Correlation")  + 
  po.nopanel

pa

solution:

library(tidyverse)

po.nopanel <-list(theme(panel.background=element_blank(),panel.grid.minor=element_blank(),panel.grid.major=element_blank()))

pa<-ggplot(iris, aes(Sepal.Length, Petal.Length)) +
  geom_tile(aes(fill=Species),colour="white") +
  scale_fill_brewer(palette = "RdYlGn",name="Correlation")  + 
  po.nopanel

pa
1 Like

Here is my Dataf :

CRI DS Ascaris Coccidias Strongyles partners age
Berthe 1,270642202 13,83366893 0 0 0 16 17
Bulle 0,879781421 71,49250489 3,5 3 7 16 17
Georgika 1,064102564 14,0998227 0,666666667 5,666666667 6,333333333 16 12
Gindia 1,278431373 39,13119004 0 2 10,33333333 15 11
Hillary 1,296089385 3,242757601 9,25 1,5 1,75 13 10
Himalaya 1,19375 -2,087402026 0 0,5 3 5 10
Holliday 1,197115385 33,39066188 0 0,666666667 12,66666667 14 10
Iena 1,054621849 46,83488485 0,5 0,5 3,5 14 9
Inca 1,089171975 -5,118630703 0 3,5 16,5 13 9
Isidore 1,159322034 -0,756084512 0 0,5 24,5 16 10
Kakie 1,278688525 8,940206386 1,5 6,5 11 7 20
Orphee 1,180327869 14,05813226 0 2 5 8 4
Pamela 1,358552632 -10,7906219 0 0,5 14 7 2
Priska 1,313829787 -33,34665086 3 1,5 11 16 2
Quarida 1,48125 -30,31691381 2 0,5 5 13 1
Quercy 1,399038462 -16,35804279 17,33333333 4,333333333 19,33333333 6 1
Quinder 1,433823529 -44,79579312 5 0,333333333 8,666666667 16 1
Qwara 1,362831858 -35,19178819 14,5 1,5 8 13 2
Ricky 1,512676056 -33,54672267 0,333333333 1,666666667 2,333333333 15 1
Roquette 1,768844221 -60,70658978 1 2,333333333 1,666666667 15 0,5
Uhuru 1,025423729 89,49860116 0 2 7 17 3
Wenne 0,837150127 19,603705 7,333333333 1 1,666666667 10 9

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

Thank you so much !
Sorry about my posting, I'm a beginner on R AND on this kind of forums. I'll be more carefull !
Thanks again.

no worries @cloejoly , glad you found an answer

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.