Home > Mobile >  ggplot2 order by rownames with heatmap
ggplot2 order by rownames with heatmap

Time:01-27

Question:

Below can find the data, codes, and output. How can I get the plot sorted according to the original order in the row names, instead of the alphabetic order?

Note: I understand there are many existing questions and answers saying we can use as. character() and specify the orders in factor levels. However, the codes for the heatmap are a result of StackOverflow help, and I don't know how to follow those guidances to modify the codes.

Data:

data <- structure(c(1.56415665339664, 1.88622232430646, 2.7670527911374, 
                    1.92703163268293, 1.92815958820144, 1.53777698134781, 1.85785080937573, 
                    2.73057928043023, 1.91265151742415, 1.91392443413493, 1.51188863842245, 
                    1.82978143286307, 2.69398442644669, 1.89813137541848, 1.89955330864784, 
                    1.48650974337831, 1.80204149133308, 2.65731564638771, 1.88348256348151, 
                    1.8850572392635, 1.46165563934645, 1.77465595548961, 2.62061921610674, 
                    1.86871647260771, 1.87044727645794, 1.4373390243787, 1.74764749626653, 
                    2.58394008473444, 1.85384450032525, 1.85573446770023, 1.41357009067807, 
                    1.72103652543546, 2.5473217079521, 1.83887802369739, 1.84092983214118, 
                    1.39035667008188, 1.69484124901252, 2.51080590056645, 1.82382837308096, 
                    1.82604433604935, 1.36770438358188, 1.66907773176055, 2.47443270873259, 
                    1.80870680674341, 1.81108886908555, 1.34561679288903, 1.64375997112343, 
                    2.4382403018842, 1.79352448643103, 1.79607422149822, 1.3240955522757, 
                    1.61889997899129, 2.40226488416451, 1.77829245397141, 1.78101106231344, 
                    1.30314055915218, 1.59450786977524, 2.36654062491116, 1.76302160898326, 
                    1.7659099185843, 1.28275010205015, 1.57059195336362, 2.33109960753632, 
                    1.74772268775752, 1.7507811557552, 1.26292100489292, 1.54715883163515, 
                    2.29597179596148, 1.73240624336288, 1.73563495918782, 1.24364876662612, 
                    1.52421349731482, 2.26118501761437, 1.71708262702006, 1.72048131688628, 
                    1.22492769546226, 1.50175943407199, 2.22676496187464, 1.70176197077873, 
                    1.70533000345043, 1.20675103715734, 1.47979871687523, 2.19273519276265, 
                    1.68645417152199, 1.69019056527759, 1.18911109688617, 1.45833211173251, 
                    2.15911717460239, 1.67116887631423, 1.67507230702478, 1.17199935441569, 
                    1.43735917405612, 2.12593030935165, 1.65591546909929, 1.65998427933569, 
                    1.15540657239252, 1.41687834499844, 2.09319198427861, 1.64070305874773, 
                    1.6449352678292, 1.13932289766278, 1.39688704520578, 2.06091762867183, 
                    1.6255404684443, 1.62993378333913, 1.12373795562949, 1.37738176553192, 
                    2.02912077829616, 1.6104362263992, 1.61498805338851, 1.10864093772681, 
                    1.35835815434096, 1.99781314634913, 1.5953985578604, 1.60010601487567, 
                    1.09402068215196, 1.33981110110915, 1.96700469972745, 1.58043537839775, 
                    1.58529530794391, 1.07986574804581, 1.32173481610856, 1.93670373947838, 
                    1.56555428842454, 1.57056327100153, 1.06616448335306, 1.30412290602036, 
                    1.90691698438448, 1.55076256891683, 1.55591693685494, 1.05290508662397, 
                    1.28696844538424, 1.87764965670914, 1.53606717828693, 1.54136302991323, 
                    1.04007566304237, 1.27026404384093, 1.84890556921326, 1.52147475036339, 
                    1.52690796441975, 1.02766427498045, 1.25400190916953, 1.82068721263795, 
                    1.50699159342693, 1.51255784366333, 1.01565898739092, 1.23817390615927, 
                    1.79299584293262, 1.49262369024903, 1.49831846011925, 1.0040479083513, 
                    1.22277161138752, 1.76583156759166, 1.47837669907795, 1.48419529646884, 
                    0.992819225075914, 1.2077863640027, 1.73919343054343, 1.46425595551545, 
                    1.47019352744474, 0.98196123570733, 1.19320931263264, 1.71307949511363, 
                    1.45026647522637, 1.45631802244833, 0.9714623771931, 1.17903145855638, 
                    1.68748692465827, 1.43641295742272, 1.4425733488853, 0.96131124954466, 
                    1.16524369529089, 1.66241206053094, 1.42269978906386, 1.42896377616518, 
                    0.951496636765001, 1.15183684475424, 1.63785049711299, 1.40913104971445, 
                    1.41549328031103, 0.942007524719625, 1.13880169017355, 1.61379715369429, 
                    1.39571051700261, 1.40216554912609, 0.932833116212497, 1.12612900591028, 
                    1.59024634304597, 1.38244167262157, 1.38898398786489, 0.923962843515001, 
                    1.11380958437724, 1.56719183657471, 1.36932770881932, 1.37595172535787, 
                    0.915386378581994, 1.10183426022178, 1.54462692599157, 1.35637153532236, 
                    1.3630716205394, 0.907093641174831, 1.09019393194762, 1.52254448146634, 
                    1.34357578664111, 1.35034626933109, 0.899074805097146, 1.07887958114475, 
                    1.50093700627199, 1.33094282970668, 1.33777801183386, 0.89132030273522, 
                    1.06788228949262, 1.47979668795232, 1.31847477179054, 1.32536893978401, 
                    0.883820828081197, 1.05719325369659, 1.45911544607085, 1.30617346866088, 
                    1.31312090423067, 0.876567338404172, 1.04680379851168, 1.4388849766195, 
                    1.29404053293171, 1.30103552339406, 0.869551054721558, 1.03670538800144, 
                    1.41909679318253, 1.28207734256312, 1.28911419066604, 0.862763461210944, 
                    1.02688963517278, 1.39974226496544, 1.2702850494734, 1.27735808271664, 
                    0.856196303691171, 1.01734831012073, 1.38081265180883, 1.25866458822613, 
                    1.26576816767268, 0.849841587290416, 1.00807334680985, 1.36229913631591, 
                    1.24721668475813, 1.25434521333641, 0.843691573408777, 0.99905684861186, 
                    1.34419285322773, 1.23594186511587, 1.24308979541463, 0.837738776073223, 
                    0.990291092711936, 1.3264849161844, 1.22484046417117, 1.23200230573094, 
                    0.831975957773722, 0.981768533488905, 1.3091664420122, 1.21391263428855, 
                    1.22108296039557, 0.826396124860962, 0.973481804967925, 1.29222857267723, 
                    1.20315835391962, 1.21033180790988, 0.820992522578248, 0.965423722437341, 
                    1.27566249504498, 1.19257743610158, 1.1997487371841, 0.815758629792936, 
                    0.957587283315014, 1.25945945858363, 1.18216953683933, 1.18933348544938, 
                    0.810688153486053, 0.949965667343234, 1.24361079114564, 1.17193416335278, 
                    1.17908564604669, 0.805775023052616, 0.942552236185361, 1.22810791295855, 
                    1.1618706821728, 1.16900467607732, 0.801013384459458, 0.935340532491705, 
                    1.21294234895183, 1.15197832707131, 1.15908990390117, 0.796397594302229, 
                    0.928324278496812, 1.19810573954163, 1.14225620681276, 1.14934053647087, 
                    0.791922213798429, 0.921497374205249, 1.18358984999023, 1.13270331271594, 
                    1.13975566649139, 0.787582002749033, 0.914853895218226, 1.16938657845174, 
                    1.12331852601676, 1.13033427939609, 0.783371913497309, 0.908388090248925, 
                    1.15548796281005, 1.11410062502406, 1.12107526013177, 0.779287084909823, 
                    0.902094378370215, 1.14188618640925, 1.10504829206212, 1.11197739974653, 
                    0.775322836401382, 0.895967346034515, 1.12857358277151, 1.09616012019459, 
                    1.10303940177539, 0.771474662022726, 0.89000174390193, 1.11554263939148, 
                    1.08743461972608, 1.09425988841997, 0.767738224627101, 0.884192483509395, 
                    1.10278600069095, 1.07887022447868, 1.08563740651938, 0.764109350129453, 
                    0.878534633810428, 1.09029647021224, 1.07046529784161, 1.07717043331055, 
                    0.760584021869845, 0.873023417612164, 1.07806701212338, 1.0622181385934, 
                    1.06885738197716, 0.757158375090736, 0.867654207933682, 1.06609075210328, 
                    1.05412698649671, 1.06069660698705, 0.753828691536061, 0.862422524307139, 
                    1.05436097767014, 1.04619002766682, 1.05268640921891, 0.750591394178479, 
                    0.857324029040955, 1.04287113801187, 1.03840539971537, 1.04482504087941, 
                    0.747443042079812, 0.852354523462208, 1.03161484337265, 1.03077119667177, 
                    1.03711071021304, 0.744380325388455, 0.847509944153447, 1.02058586404586, 
                    1.02328547368523, 1.02954158600691, 0.741400060476472, 0.84278635919742, 
                    1.00977812901932, 1.01594625151082, 1.02211580189372, 0.738499185218138, 
                    0.838179964441562, 0.999185724315382, 1.00875152078335, 1.0148314604562, 
                    0.735674754410845, 0.833687079792661, 0.988802891064574, 1.00169924608359, 
                    1.00768663713703, 0.732923935338581, 0.829304145550759, 0.978624023348409, 
                    0.994787369801221, 1.00067938395813, 0.730244003477527, 0.82502771879015, 
                    0.968643665843796, 0.988013815799624, 0.993807733054007, 0.727632338342786, 
                    0.820854469794238, 0.958856511298594, 0.981376492887521, 0.987069700023661, 
                    0.725086419474772, 0.816781178550003, 0.949257397865192, 0.974873298102964, 
                    0.980463287105968, 0.722603822563399, 0.812804731306956, 0.939841306316456, 
                    0.968502119815104, 0.973986486183567, 0.72018221570783, 0.808922117204624, 
                    0.930603357166104, 0.96226084064944, 0.967637281620371, 0.717819355809315, 
                    0.805130424971885, 0.921538807713388, 0.956147340242261, 0.961413652937977, 
                    0.715513085094331, 0.801426839700833, 0.912643049029987, 0.950159497830092, 
                    0.955313577336244, 0.713261327765121, 0.797808639697236, 0.903911602905182, 
                    0.944295194679964, 0.94933503206341, 0.711062086774516, 0.794273193409158, 
                    0.895340118763694, 0.938552316366345, 0.943475996641087, 0.708913440721818, 
                    0.79081795643482, 0.886924370569017, 0.932928754900547, 0.937734454949501, 
                    0.706813540866439, 0.787440468610379, 0.878660253723646, 0.927422410718391, 
                    0.932108397178287, 0.704760608255926, 0.784138351177934, 0.870543781976314, 
                    0.922031194531849, 0.926595821648137, 0.702752930964936, 0.780909304033727, 
                    0.862571084345134, 0.916753029050316, 0.921194736508511, 0.700788861441736, 
                    0.777751103056251, 0.854738402064476, 0.911585850577083, 0.915903161316576, 
                    0.698866813958788, 0.77466159751369, 0.847042085562406, 0.906527610486473, 
                    0.910719128502428, 0.696985262163986, 0.771638707549953, 0.839478591474624, 
                    0.901576276586997, 0.905640684725585, 0.695142736729139, 0.768680421748302, 
                    0.832044479699982, 0.896729834375788, 0.90066589212762, 0.693337823092351, 
                    0.765784794771495, 0.824736410501997, 0.891986288189406, 0.895792829485681, 
                    0.691569159290967, 0.762949945077154, 0.817551141659994, 0.887343662256013, 
                    0.891019593271567, 0.689835433881839, 0.760174052707016, 0.810485525673035, 
                    0.882800001653769, 0.886344298620863, 0.6881353839457, 0.757455357148564, 
                    0.803536507019115, 0.878353373180141, 0.88176508021654, 0.686467793172536, 
                    0.754792155267527, 0.796701119471732, 0.874001866136701, 0.877280093091286, 
                    0.684831490024889, 0.7521827993096, 0.789976483475408, 0.869743593033824, 
                    0.872887513352707, 0.683225345976117, 0.749625694969754, 0.783359803581409, 
                    0.865576690219552, 0.868585538835388, 0.681648273820716, 0.74711929952741, 
                    0.776848365944517, 0.86149931843675, 0.864372389683703, 0.680099226053889, 
                    0.744662120045772, 0.770439535881419, 0.857509663312525, 0.860246308869087, 
                    0.678577193317622, 0.742252711633559, 0.764130755491001, 0.853605935783729, 
                    0.856205562645381, 0.677081202910631, 0.739889675767395, 0.75791954133658, 
                    0.849786372462227, 0.852248440945717, 0.67561031735961, 0.737571658673098, 
                    0.751803482189895, 0.846049235943467, 0.84837325772426, 0.674163633049295, 
                    0.735297349764132, 0.745780236836504, 0.842392815061733, 0.844578351246036, 
                    0.67274027890897, 0.733065480135467, 0.739847531942042, 0.838815425095341, 
                    0.840862084327889, 0.671339415153075, 0.730874821111148, 0.734003159978691, 
                    0.835315407924883, 0.837222844533538, 0.66996023207371, 0.728724182843864, 
                    0.728244977211044, 0.831891132147497, 0.83365904432554, 0.66860194888288, 
                    0.726612412964853, 0.72257090174049, 0.828540993150013, 0.830169121176865, 
                    0.667263812602402, 0.724538395282482, 0.716978911607115, 0.825263413143681, 
                    0.826751537644663, 0.6659450969995, 0.722501048527904, 0.71146704294807, 
                    0.822056841163083, 0.823404781408691, 0.664645101566158, 0.720499325146197, 
                    0.706033388211285, 0.818919753031688, 0.820127365276744, 0.663363150540402, 
                    0.718532210131447, 0.700676094423347, 0.815850651296407, 0.81691782715934, 
                    0.662098591967731, 0.716598719904252, 0.695393361510352, 0.812848065133384, 
                    0.813774730015795, 0.660850796801012, 0.714697901230176, 0.690183440670466, 
                    0.809910550227147, 0.810696661773713, 0.659619158037194, 0.712828830177725, 
                    0.685044632796959, 0.807036688625131, 0.807682235223833, 0.65840308988929, 
                    0.710990611114427, 0.679975286950419, 0.804225088569508, 0.804730087892066, 
                    0.657202026992117, 0.709182375739667, 0.674973798878868, 0.801474384308113, 
                    0.801838881890468, 0.656015423640367, 0.707403282152963, 0.670038609584492, 
                    0.798783235886211, 0.799007303748793, 0.654842753057613, 0.705652513956377, 
                    0.665168203935704, 0.796150328920716, 0.796234064228217, 0.653683506694951, 
                    0.703929279389845, 0.66036110932326, 0.793574374358407, 0.793517898118688, 
                    0.652537193557989, 0.702232810498201, 0.655615894359165, 0.791054108219602, 
                    0.79085756402133, 0.651403339560998, 0.700562362328734, 0.650931167617108, 
                    0.788588291328647, 0.788251844117223, 0.650281486907035, 0.698917212158154, 
                    0.646305576413202, 0.786175709032535, 0.785699543923806, 0.649171193492958, 
                    0.697296658747865, 0.641737805625809, 0.78381517090886, 0.783199492040099, 
                    0.64807203233824, 0.695700021626499, 0.63722657655324, 0.781505510464259, 
                    0.780750539881858, 0.646983591036597, 0.694126640398684, 0.632770645808186, 
                    0.779245584824428, 0.778351561407705, 0.645905471229425, 0.692575874079065, 
                    0.628368804247696, 0.777034274416723, 0.776001452837246, 0.644837288100137, 
                    0.691047100450625, 0.624019875937613, 0.7748704826463, 0.773699132362083, 
                    0.643778669888508, 0.689539715446396, 0.619722717150336, 0.772753135566687, 
                    0.771443539850621, 0.642729257424162, 0.688053132553661, 0.615476215394868, 
                    0.77068118154563, 0.769233636547476, 0.641688703678407, 0.686586782239811, 
                    0.611279288478086, 0.768653590926998, 0.767068404768254, 0.640656673333622, 
                    0.685140111399023, 0.607130883596222, 0.766669355689472, 0.764946847590445, 
                    0.63963284236946, 0.683712582818977, 0.603029976455569, 0.764727489102711, 
                    0.76286798854108, 0.638616897665165, 0.682303674666839, 0.598975570421445, 
                    0.762827025381632, 0.760830871281802, 0.637608536617296, 0.680912879993784, 
                    0.594966695694488, 0.760967019339396, 0.758834559291943, 0.636607466772238, 
                    0.67953970625735, 0.591002408513372, 0.759146546039657, 0.75687813555014, 
                    0.635613405472859, 0.678183674860939, 0.587081790383065, 0.757364700448588, 
                    0.754960702215028, 0.634626079518729, 0.67684432070982, 0.583203947327774, 
                    0.755620597087154, 0.753081380305464, 0.633645224839319, 0.675521191782987, 
                    0.579368009167761, 0.75391336968409, 0.751239309380749, 0.632670586179664, 
                    0.674213848720291, 0.575573128819215, 0.752242170829969, 0.749433647221233, 
                    0.631701916797938, 0.672921864424234, 0.571818481616423, 0.750606171632756, 
                    0.747663569509711, 0.630738978174469, 0.671644823675892, 0.568103264655477, 
                    0.749004561375194, 0.745928269513948, 0.629781539731715, 0.67038232276441, 
                    0.564426696158795, 0.747436547174325, 0.74422695777065, 0.62882937856474, 
                    0.66913396912956, 0.560788014859765, 0.745901353643456, 0.742558861771208, 
                    0.627882279181762, 0.667899381016863, 0.557186479406819, 0.74439822255683, 
                    0.740923225649459, 0.626940033254362, 0.666678187144791, 0.553621367786292, 
                    0.742926412517243, 0.73931930987174, 0.626002439376954, 0.665470026383597, 
                    0.550091976763438, 0.741485198626834, 0.737746390929453, 0.62506930283514, 
                    0.664274547445332, 0.54659762134097, 0.740073872161253, 0.736203761034359
), dim = c(5L, 151L), dimnames = list(c("highest_pi_count", "second_pi_count", 
                                        "highest_si_count", "max_overhangz_minus", "max_overhangz_plus"
), NULL))

Codes:

library(latticeExtra)
library(ggplot2)
library(tidyverse)
rownames(data)
as.data.frame(data) |>
  rownames_to_column() %>%
  pivot_longer(-rowname) %>%
  ggplot(aes(factor(name, unique(name)), rowname, fill = value))   
  scale_x_discrete(labels = ~., breaks = ~ c(6:29/10))   ggtitle("Title")   theme(plot.title = element_text(hjust = 0.5))  
  geom_tile()  
  scale_fill_gradientn(colours = c("blue4", "white", "red3"))  
  scale_y_discrete(position = "right")  
  theme(legend.position = "none", 
        axis.ticks = element_blank(),
        axis.title = element_blank(),
        text = element_text(face = 2))

Output:

> rownames(data)
[1] "highest_pi_count"    "second_pi_count"     "highest_si_count"    "max_overhangz_minus" "max_overhangz_plus" 

enter image description here

CodePudding user response:

To fix your issue convert row name to factor with the order of the levels set according to the row names of your original dataset:

as.data.frame(data) |>
  rownames_to_column() |>
  pivot_longer(-rowname) |>
  mutate(rowname = factor(rowname, rownames(data))) |>
  ggplot(aes(factor(name, unique(name)), rowname, fill = value))  
  scale_x_discrete(labels = ~., breaks = ~ c(6:29 / 10))  
  ggtitle("Title")  
  theme(plot.title = element_text(hjust = 0.5))  
  geom_tile()  
  scale_fill_gradientn(colours = c("blue4", "white", "red3"))  
  scale_y_discrete(position = "right")  
  theme(
    legend.position = "none",
    axis.ticks = element_blank(),
    axis.title = element_blank(),
    text = element_text(face = 2)
  )

enter image description here

  • Related