【问题标题】:Order heatmap according to one specific row [duplicate]根据特定行排序热图[重复]
【发布时间】:2018-11-22 07:33:44
【问题描述】:

我正在尝试执行以下操作。考虑以下数据集

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

我正在尝试生成热图。到目前为止还不错。这相对容易。例如我可以使用

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

然后

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

我知道 x 轴上的标签是水平的,但我知道如何解决这个问题。我想要的是根据一个特定的行对 x 轴进行排序。例如,我希望热图与行“影响”(例如)值按升序排列。任何人都可以指出我正确的方向吗? 我应该将 x 转换为一个因子并更改那里的水平吗?

【问题讨论】:

    标签: r ggplot2 heatmap


    【解决方案1】:

    是的,您可以将其转换为因子并指定级别。因此,我们可以根据impact 行来更改它

    dt2$rowname <- factor(dt2$rowname, levels = df$trends[order(df$impact)])
    
    library(ggplot2)
    ggplot(dt2, aes(x = rowname, y = colname, fill = value)) +
        geom_tile()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-04-17
      • 2017-08-11
      • 2019-01-29
      • 1970-01-01
      • 2013-03-06
      • 2012-12-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多