Skip to content

Instantly share code, notes, and snippets.

@dsparks
Last active August 19, 2022 06:54

Revisions

  1. dsparks revised this gist Oct 16, 2014. 1 changed file with 4 additions and 7 deletions.
    11 changes: 4 additions & 7 deletions Heatmap.R
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,3 @@
    # Simple ggplot2 heatmap
    # with colorBrewer "spectral" palette

    doInstall <- TRUE # Change to FALSE if you don't want packages installed.
    toInstall <- c("ggplot2", "reshape2", "RColorBrewer")
    if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
    @@ -26,18 +23,18 @@ head(longData, 20)

    # Optionally, reorder both the row and column variables in any order
    # Here, they are sorted by mean value
    longData$X1 <- factor(longData$X1, names(sort(with(longData, by(value, X1, mean)))))
    longData$X2 <- factor(longData$X2, names(sort(with(longData, by(value, X2, mean)))))
    longData$Var1 <- factor(longData$Var1, names(sort(with(longData, by(value, Var1, mean)))))
    longData$Var2 <- factor(longData$Var2, names(sort(with(longData, by(value, Var2, mean)))))

    # Define palette
    myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")), space="Lab")

    zp1 <- ggplot(longData,
    aes(x = X2, y = X1, fill = value))
    aes(x = Var2, y = Var1, fill = value))
    zp1 <- zp1 + geom_tile()
    zp1 <- zp1 + scale_fill_gradientn(colours = myPalette(100))
    zp1 <- zp1 + scale_x_discrete(expand = c(0, 0))
    zp1 <- zp1 + scale_y_discrete(expand = c(0, 0))
    zp1 <- zp1 + coord_equal()
    zp1 <- zp1 + theme_bw()
    print(zp1) # Your plot will look different, depending on the seed
    print(zp1)
  2. dsparks revised this gist Apr 24, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Heatmap.R
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ longData$X1 <- factor(longData$X1, names(sort(with(longData, by(value, X1, mean)
    longData$X2 <- factor(longData$X2, names(sort(with(longData, by(value, X2, mean)))))

    # Define palette
    myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
    myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")), space="Lab")

    zp1 <- ggplot(longData,
    aes(x = X2, y = X1, fill = value))
  3. dsparks revised this gist Sep 14, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion Heatmap.R
    Original file line number Diff line number Diff line change
    @@ -2,7 +2,7 @@
    # with colorBrewer "spectral" palette

    doInstall <- TRUE # Change to FALSE if you don't want packages installed.
    toInstall <- c("ggplot2", "reshape", "RColorBrewer")
    toInstall <- c("ggplot2", "reshape2", "RColorBrewer")
    if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
    lapply(toInstall, library, character.only = TRUE)

  4. dsparks revised this gist Sep 13, 2012. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions Heatmap.R
    Original file line number Diff line number Diff line change
    @@ -24,6 +24,11 @@ for(ii in 2:nrow(myData)){ myData[ii, ] <- myData[ii-1, ] + rnorm(ncol(myData))
    longData <- melt(myData)
    head(longData, 20)

    # Optionally, reorder both the row and column variables in any order
    # Here, they are sorted by mean value
    longData$X1 <- factor(longData$X1, names(sort(with(longData, by(value, X1, mean)))))
    longData$X2 <- factor(longData$X2, names(sort(with(longData, by(value, X2, mean)))))

    # Define palette
    myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))

  5. dsparks revised this gist Sep 13, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Heatmap.R
    Original file line number Diff line number Diff line change
    @@ -34,4 +34,5 @@ zp1 <- zp1 + scale_fill_gradientn(colours = myPalette(100))
    zp1 <- zp1 + scale_x_discrete(expand = c(0, 0))
    zp1 <- zp1 + scale_y_discrete(expand = c(0, 0))
    zp1 <- zp1 + coord_equal()
    zp1 <- zp1 + theme_bw()
    print(zp1) # Your plot will look different, depending on the seed
  6. dsparks revised this gist Sep 12, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions Heatmap.R
    Original file line number Diff line number Diff line change
    @@ -20,6 +20,7 @@ colnames(myData) <- LETTERS[1:nCol]
    for(ii in 2:ncol(myData)){ myData[, ii] <- myData[, ii-1] + rnorm(nrow(myData)) }
    for(ii in 2:nrow(myData)){ myData[ii, ] <- myData[ii-1, ] + rnorm(ncol(myData)) }

    # For melt() to work seamlessly, myData has to be a matrix.
    longData <- melt(myData)
    head(longData, 20)

  7. dsparks created this gist Sep 12, 2012.
    36 changes: 36 additions & 0 deletions Heatmap.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    # Simple ggplot2 heatmap
    # with colorBrewer "spectral" palette

    doInstall <- TRUE # Change to FALSE if you don't want packages installed.
    toInstall <- c("ggplot2", "reshape", "RColorBrewer")
    if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
    lapply(toInstall, library, character.only = TRUE)

    # Generate a random matrix
    # This can be any type of numeric matrix,
    # though we often see heatmaps of square correlation matrices.
    nRow <- 9
    nCol <- 16

    myData <- matrix(rnorm(nRow * nCol), ncol = nCol)
    rownames(myData) <- letters[1:nRow]
    colnames(myData) <- LETTERS[1:nCol]

    # Replace with numbers that actually have a relationship:
    for(ii in 2:ncol(myData)){ myData[, ii] <- myData[, ii-1] + rnorm(nrow(myData)) }
    for(ii in 2:nrow(myData)){ myData[ii, ] <- myData[ii-1, ] + rnorm(ncol(myData)) }

    longData <- melt(myData)
    head(longData, 20)

    # Define palette
    myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))

    zp1 <- ggplot(longData,
    aes(x = X2, y = X1, fill = value))
    zp1 <- zp1 + geom_tile()
    zp1 <- zp1 + scale_fill_gradientn(colours = myPalette(100))
    zp1 <- zp1 + scale_x_discrete(expand = c(0, 0))
    zp1 <- zp1 + scale_y_discrete(expand = c(0, 0))
    zp1 <- zp1 + coord_equal()
    print(zp1) # Your plot will look different, depending on the seed