ropensci / iheatmapr

Complex, interactive heatmaps in R
https://docs.ropensci.org/iheatmapr
Other
267 stars 35 forks source link

How to change the row order of the heatmap from Alabama to Wyoming ? [Question] #91

Closed JauntyJJS closed 2 years ago

JauntyJJS commented 2 years ago

Hi.

Looking at the gif provided in https://raw.githubusercontent.com/ropensci/iheatmapr/master/vaccine.gif

The Wyoming is labeled on the top of the heatmap and Alabama is at the bottom.

Is there a way to make Alabama on the top and Wyoming at the bottom instead ?

Thank you.

mschilli87 commented 2 years ago

I can't give you the excact solution without knowing the code you used to generate this, but I guess a single call of forcats::fct_rev before passing the data to the heatmap function would give you the desired results.

JauntyJJS commented 2 years ago

Hi,

The code to generate is from the Readme.md file in https://github.com/ropensci/iheatmapr

library(iheatmapr)
data(measles, package = "iheatmapr")

main_heatmap(measles, name = "Measles<br>Cases", x_categorical = FALSE,
             layout = list(font = list(size = 8))) %>%
  add_col_groups(ifelse(1930:2001 < 1961,"No","Yes"),
                  side = "bottom", name = "Vaccine<br>Introduced?",
                  title = "Vaccine?",
                  colors = c("lightgray","blue")) %>%
  add_col_labels(ticktext = seq(1930,2000,10),font = list(size = 8)) %>%
  add_row_labels(size = 0.3,font = list(size = 6)) %>% 
  add_col_summary(layout = list(title = "Average<br>across<br>states"),
                  yname = "summary")  %>%                 
  add_col_title("Measles Cases from 1930 to 2001", side= "top") %>%
  add_row_summary(groups = TRUE, 
                  type = "bar",
                  layout = list(title = "Average<br>per<br>year",
                                font = list(size = 8)))

Unfortunately, I do not know how to use forcats::fct_rev to get what is required due to my limited understanding of this function. Could you help me in this ?

mschilli87 commented 2 years ago

Looking at this I assume simply reversing the row order in the input matrix should do.

Try adding

 measles <- measles[nrow(measles):1, ]

in between your data(...) and main_heatmap(...) lines.

JauntyJJS commented 2 years ago

Hi,

Thank you for the suggestion. I have tried measles <- measles[nrow(measles):1,] and it works.

library(iheatmapr)

data(measles, package = "iheatmapr")
measles <- measles[nrow(measles):1,]

main_heatmap(measles, name = "Measles<br>Cases", x_categorical = FALSE,
             layout = list(font = list(size = 8))) %>%
  add_col_groups(ifelse(1930:2001 < 1961,"No","Yes"),
                 side = "bottom", name = "Vaccine<br>Introduced?",
                 title = "Vaccine?",
                 colors = c("lightgray","blue")) %>%
  add_col_labels(ticktext = seq(1930,2000,10),font = list(size = 8)) %>%
  add_row_labels(size = 0.3,font = list(size = 6)) %>% 
  add_col_summary(layout = list(title = "Average<br>across<br>states"),
                  yname = "summary")  %>%                 
  add_col_title("Measles Cases from 1930 to 2001", side= "top") %>%
  add_row_summary(groups = TRUE, 
                  type = "bar",
                  layout = list(title = "Average<br>per<br>year",
                                font = list(size = 8)))

Created on 2022-02-10 by the reprex package (v2.0.1)