Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6158293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:55:59+00:00 2026-05-23T20:55:59+00:00

Is this possible to reproduce this lattice plot with ggplot2? library(latticeExtra) data(mtcars) x <-

  • 0

Is this possible to reproduce this lattice plot with ggplot2?

library(latticeExtra)
data(mtcars)
x  <- t(as.matrix(scale(mtcars)))
dd.row <- as.dendrogram(hclust(dist(x)))
row.ord <- order.dendrogram(dd.row)

dd.col <- as.dendrogram(hclust(dist(t(x))))
col.ord <- order.dendrogram(dd.col)

library(lattice)

levelplot(x[row.ord, col.ord],
      aspect = "fill",
      scales = list(x = list(rot = 90)),
      colorkey = list(space = "left"),
      legend =
      list(right =
           list(fun = dendrogramGrob,
                args =
                list(x = dd.col, ord = col.ord,
                     side = "right",
                     size = 10)),
           top =
           list(fun = dendrogramGrob,
                args =
                list(x = dd.row,
                     side = "top",
                     size = 10))))

enter image description here

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T20:55:59+00:00Added an answer on May 23, 2026 at 8:55 pm

    EDIT

    From 8 August 2011 the ggdendro package is available on CRAN
    Note also that the dendrogram extraction function is now called dendro_data instead of cluster_data


    Yes, it is. But for the time being you will have to jump through a few hoops:

    1. Install the ggdendro package (available from CRAN). This package will extract the cluster information from several types of cluster methods (including Hclust and dendrogram) with the express purpose of plotting in ggplot.
    2. Use grid graphics to create viewports and align three different plots.

    enter image description here

    The code:

    First load the libraries and set up the data for ggplot:

    library(ggplot2)
    library(reshape2)
    library(ggdendro)
    
    data(mtcars)
    x <- as.matrix(scale(mtcars))
    dd.col <- as.dendrogram(hclust(dist(x)))
    col.ord <- order.dendrogram(dd.col)
    
    dd.row <- as.dendrogram(hclust(dist(t(x))))
    row.ord <- order.dendrogram(dd.row)
    
    xx <- scale(mtcars)[col.ord, row.ord]
    xx_names <- attr(xx, "dimnames")
    df <- as.data.frame(xx)
    colnames(df) <- xx_names[[2]]
    df$car <- xx_names[[1]]
    df$car <- with(df, factor(car, levels=car, ordered=TRUE))
    
    mdf <- melt(df, id.vars="car")
    

    Extract dendrogram data and create the plots

    ddata_x <- dendro_data(dd.row)
    ddata_y <- dendro_data(dd.col)
    
    ### Set up a blank theme
    theme_none <- theme(
      panel.grid.major = element_blank(),
      panel.grid.minor = element_blank(),
      panel.background = element_blank(),
      axis.title.x = element_text(colour=NA),
      axis.title.y = element_blank(),
      axis.text.x = element_blank(),
      axis.text.y = element_blank(),
      axis.line = element_blank()
      #axis.ticks.length = element_blank()
    )
    
    ### Create plot components ###    
    # Heatmap
    p1 <- ggplot(mdf, aes(x=variable, y=car)) + 
      geom_tile(aes(fill=value)) + scale_fill_gradient2()
    
    # Dendrogram 1
    p2 <- ggplot(segment(ddata_x)) + 
      geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) + 
      theme_none + theme(axis.title.x=element_blank())
    
    # Dendrogram 2
    p3 <- ggplot(segment(ddata_y)) + 
      geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) + 
      coord_flip() + theme_none
    

    Use grid graphics and some manual alignment to position the three plots on the page

    ### Draw graphic ###
    
    grid.newpage()
    print(p1, vp=viewport(0.8, 0.8, x=0.4, y=0.4))
    print(p2, vp=viewport(0.52, 0.2, x=0.45, y=0.9))
    print(p3, vp=viewport(0.2, 0.8, x=0.9, y=0.4))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

anyone know if is possible to reproduce this effect background-image: -moz-linear-gradient(top, #666666, #000000); background-image:
Is this possible to setup Steps To Reproduce field so it's required? It's always
Is this possible? I want to have the To:, Body, and an Attachment all
Is this possible? I am looking forward to a tutorial which explains the steps
Is this possible? I had troubles with SVN clients not being able to access
Is this possible? I can't find it anywhere.
Is this possible? Or would I miss something? (Fixtures,..?) Because: When I use autotest
Is this possible? I am creating a single base factory function to drive factories
Is this possible? I've followed the instructions from here ( http://www.jetbrains.net/confluence/display/TCD4/Setting+up+an+External+Database#SettingupanExternalDatabase-MicrosoftSQLServer2005 ) but I
Is this possible: myList = [] myList[12] = 'a' myList[22] = 'b' myList[32] =

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.