1

I have encountered a strange problem with Markdown.

I attach hereunder the R code included in markdown for generating a correlation heatmap.

library(reshape2)
library(knitr)
library(ggplot2)
data("cars")

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

reorder_cormat <- function(cormat){
  # Use correlation between variables as distance
  dd <- as.dist((1-cormat)/2)
  hc <- hclust(dd)
  cormat <-cormat[hc$order, hc$order]
}

cormat <- round(cor(cars),2)

upper_tri <- get_upper_tri(cormat)

# Reorder the correlation matrix
cormat <- reorder_cormat(cormat)

upper_tri <- get_upper_tri(cormat)

# Melt the correlation matrix
melted_cormat <- melt(upper_tri, na.rm = TRUE)

# Create a ggheatmap
ggheatmap <- ggplot(melted_cormat, aes(Var2, Var1, fill = value))+
  geom_tile(color = "white")+
  scale_fill_gradient2(low = "blue", high = "red", mid = "white", 
                       midpoint = 0, limit = c(-1,1), space = "Lab", 
                       name="Pearson\nCorrelation") +
  theme_minimal()+ # minimal theme
  theme(axis.text.x = element_text(angle = 45, vjust = 1, 
                                   size = 12, hjust = 1))+
  coord_fixed()

ggheatmap + 
  geom_text(aes(Var2, Var1, label = value), color = "black", size = 4) +
  theme(
    axis.title.x = element_blank(),
    axis.title.y = element_blank(),
    panel.grid.major = element_blank(),
    panel.border = element_blank(),
    panel.background = element_blank(),
    axis.ticks = element_blank(),
    legend.justification = c(1, 0),
    legend.position = c(0.6, 0.7),
    legend.direction = "horizontal")+
  guides(fill = guide_colorbar(barwidth = 7, barheight = 1,
                               title.position = "top", title.hjust = 0.5))

The code runs perfectly in R console but when kitted with markdown it returns this error.

Error in FUN(X[[i]], ...) : object 'Var2' not found
Calls: <Anonymous> ... by_layer -> f -> <Anonymous> -> f -> lapply -> FUN -> FUN
Execution halted

The problem seems to be in the aes function (ggplot). For some reasons it's not able to find the "Var2" present in the melt_cormap object

Any advice?

Many thanks

2 Answers2

2

The library function does not take multiple library names as arguments although doesn't seem to throw an error. You probably have all the libraries loaded in the console, but when knitr runs it does so in a new environment and needs to load them all anew.

Try this at the top of your code:

library(reshape2) library(knitr) library(ggplot2)

If you want to load them all in a single line, there is already a SO post about that here:

Load multiple packages at once

Gary Weissman
  • 3,557
  • 1
  • 18
  • 23
  • 1
    I am using pacman as a manager. The code there reported is wrong but it just for reproducibility purposes. In any case, thanks for this, i will correct) – Alberto Stefanelli Feb 20 '18 at 23:50
1

I resolve this, the problem was pacman. For some reason, was not loading the packages properly, I switched back to the baseline command and the issues is resolved. Thanks to everyone