Order heatmap according to one specific row [duplicate]











up vote
2
down vote

favorite













This question already has an answer here:




  • Order data inside a geom_tile

    2 answers




I am trying to do the following. Consider the following dataset



trends <- c('Virtual Assistant', 'Citizen DS', 'Deep Learning', 'Speech Recognition', 
'Handwritten Recognition', 'Machine Translation', 'Chatbots',
'NLP')

impact <- sample(5,8, replace = TRUE)
maturity <- sample(5,8, replace = TRUE)
strategy <- sample(5,8, replace = TRUE)
h <- sample(5,8, replace = TRUE)

df <- data.frame(trends, impact, maturity, strategy, h)
rownames(df) <- df$trends


I am trying to generate a heatmap. So far is good. That is relatively easy. For example I can use



dftemp = df[,c("impact", "maturity", "strategy", "h")]
dt2 <- dftemp %>%
rownames_to_column() %>%
gather(colname, value, -rowname)


and then



ggplot(dt2, aes(x = rowname, y = colname, fill = value)) +
geom_tile()


I know the labels on the x-axis are horizontal, but I know how to fix that. What I would like to have is to order the x-axis based on one specific rows. For example I would like to have the heatmap with the row "impact" (for example) values in ascending order. Anyone can point me in the right direction?
Shoudl I convert the x in a factor and change the levels there?










share|improve this question















marked as duplicate by shadow r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 22 at 7:55


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.



















    up vote
    2
    down vote

    favorite













    This question already has an answer here:




    • Order data inside a geom_tile

      2 answers




    I am trying to do the following. Consider the following dataset



    trends <- c('Virtual Assistant', 'Citizen DS', 'Deep Learning', 'Speech Recognition', 
    'Handwritten Recognition', 'Machine Translation', 'Chatbots',
    'NLP')

    impact <- sample(5,8, replace = TRUE)
    maturity <- sample(5,8, replace = TRUE)
    strategy <- sample(5,8, replace = TRUE)
    h <- sample(5,8, replace = TRUE)

    df <- data.frame(trends, impact, maturity, strategy, h)
    rownames(df) <- df$trends


    I am trying to generate a heatmap. So far is good. That is relatively easy. For example I can use



    dftemp = df[,c("impact", "maturity", "strategy", "h")]
    dt2 <- dftemp %>%
    rownames_to_column() %>%
    gather(colname, value, -rowname)


    and then



    ggplot(dt2, aes(x = rowname, y = colname, fill = value)) +
    geom_tile()


    I know the labels on the x-axis are horizontal, but I know how to fix that. What I would like to have is to order the x-axis based on one specific rows. For example I would like to have the heatmap with the row "impact" (for example) values in ascending order. Anyone can point me in the right direction?
    Shoudl I convert the x in a factor and change the levels there?










    share|improve this question















    marked as duplicate by shadow r
    Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 22 at 7:55


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite












      This question already has an answer here:




      • Order data inside a geom_tile

        2 answers




      I am trying to do the following. Consider the following dataset



      trends <- c('Virtual Assistant', 'Citizen DS', 'Deep Learning', 'Speech Recognition', 
      'Handwritten Recognition', 'Machine Translation', 'Chatbots',
      'NLP')

      impact <- sample(5,8, replace = TRUE)
      maturity <- sample(5,8, replace = TRUE)
      strategy <- sample(5,8, replace = TRUE)
      h <- sample(5,8, replace = TRUE)

      df <- data.frame(trends, impact, maturity, strategy, h)
      rownames(df) <- df$trends


      I am trying to generate a heatmap. So far is good. That is relatively easy. For example I can use



      dftemp = df[,c("impact", "maturity", "strategy", "h")]
      dt2 <- dftemp %>%
      rownames_to_column() %>%
      gather(colname, value, -rowname)


      and then



      ggplot(dt2, aes(x = rowname, y = colname, fill = value)) +
      geom_tile()


      I know the labels on the x-axis are horizontal, but I know how to fix that. What I would like to have is to order the x-axis based on one specific rows. For example I would like to have the heatmap with the row "impact" (for example) values in ascending order. Anyone can point me in the right direction?
      Shoudl I convert the x in a factor and change the levels there?










      share|improve this question
















      This question already has an answer here:




      • Order data inside a geom_tile

        2 answers




      I am trying to do the following. Consider the following dataset



      trends <- c('Virtual Assistant', 'Citizen DS', 'Deep Learning', 'Speech Recognition', 
      'Handwritten Recognition', 'Machine Translation', 'Chatbots',
      'NLP')

      impact <- sample(5,8, replace = TRUE)
      maturity <- sample(5,8, replace = TRUE)
      strategy <- sample(5,8, replace = TRUE)
      h <- sample(5,8, replace = TRUE)

      df <- data.frame(trends, impact, maturity, strategy, h)
      rownames(df) <- df$trends


      I am trying to generate a heatmap. So far is good. That is relatively easy. For example I can use



      dftemp = df[,c("impact", "maturity", "strategy", "h")]
      dt2 <- dftemp %>%
      rownames_to_column() %>%
      gather(colname, value, -rowname)


      and then



      ggplot(dt2, aes(x = rowname, y = colname, fill = value)) +
      geom_tile()


      I know the labels on the x-axis are horizontal, but I know how to fix that. What I would like to have is to order the x-axis based on one specific rows. For example I would like to have the heatmap with the row "impact" (for example) values in ascending order. Anyone can point me in the right direction?
      Shoudl I convert the x in a factor and change the levels there?





      This question already has an answer here:




      • Order data inside a geom_tile

        2 answers








      r ggplot2 heatmap






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 22 at 7:53









      Ronak Shah

      29.9k103753




      29.9k103753










      asked Nov 22 at 7:33









      Umberto

      9281819




      9281819




      marked as duplicate by shadow r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 22 at 7:55


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






      marked as duplicate by shadow r
      Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 22 at 7:55


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote













          Yes, you could convert it into factors and specify the levels. So to change it based on impact row we can do



          dt2$rowname <- factor(dt2$rowname, levels = df$trends[order(df$impact)])

          library(ggplot2)
          ggplot(dt2, aes(x = rowname, y = colname, fill = value)) +
          geom_tile()


          enter image description here






          share|improve this answer




























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote













            Yes, you could convert it into factors and specify the levels. So to change it based on impact row we can do



            dt2$rowname <- factor(dt2$rowname, levels = df$trends[order(df$impact)])

            library(ggplot2)
            ggplot(dt2, aes(x = rowname, y = colname, fill = value)) +
            geom_tile()


            enter image description here






            share|improve this answer

























              up vote
              3
              down vote













              Yes, you could convert it into factors and specify the levels. So to change it based on impact row we can do



              dt2$rowname <- factor(dt2$rowname, levels = df$trends[order(df$impact)])

              library(ggplot2)
              ggplot(dt2, aes(x = rowname, y = colname, fill = value)) +
              geom_tile()


              enter image description here






              share|improve this answer























                up vote
                3
                down vote










                up vote
                3
                down vote









                Yes, you could convert it into factors and specify the levels. So to change it based on impact row we can do



                dt2$rowname <- factor(dt2$rowname, levels = df$trends[order(df$impact)])

                library(ggplot2)
                ggplot(dt2, aes(x = rowname, y = colname, fill = value)) +
                geom_tile()


                enter image description here






                share|improve this answer












                Yes, you could convert it into factors and specify the levels. So to change it based on impact row we can do



                dt2$rowname <- factor(dt2$rowname, levels = df$trends[order(df$impact)])

                library(ggplot2)
                ggplot(dt2, aes(x = rowname, y = colname, fill = value)) +
                geom_tile()


                enter image description here







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 at 7:51









                Ronak Shah

                29.9k103753




                29.9k103753