Evan O.
Evan O.

Reputation: 1563

ggplot2 heatmap not arranging values from high to low

I'm trying to arrange my heatmap so that values with higher "Overall" will appear at the top and values with a lower "Overall" will appear at the bottom. The issue is that the data (mydata) is already arranged like this, but this arrangement isn't being applied to the end.

Any idea how I can fix this?

Thanks.

library(tidyverse)
library(RColorBrewer)
library(extrafont)

mydata <- structure(list(track_name = c("Turned Off", "Giving Bad People...", 
                                        "Shinshow", "Bottomless Pit", "Why A Birch Gotta...", "I Break Mirrors W...", 
                                        "Beyond Alive", "Disappointed", "Black Paint", "Whatever I Want (...", 
                                        "This Is Violence ...", "Death Grips 2.0", "Centuries Of Damn", 
                                        "Government Plates", "Inanimate Sensation", "BB Poison", "The Horn Section", 
                                        "The Fever (Aye Aye)", "Flies", "Hustle Bones", "Dilemma", "Pop", 
                                        "Bass Rattle Stars...", "On GP", "Stockton", "Ring A Bell", "The Power That B", 
                                        "Up My Sleeves", "Fudge That", "Big House", "Outro", "Hot Head", 
                                        "Trash", "Spikes", "Get Got", "Lost Boys", "The Fear", "Come Up And Get Me", 
                                        "Lock Your Doors", "Hacker", "Death Grips Is On...", "I've Seen Footage", 
                                        "Birds", "Linda's In Custody", "The Cage", "Have A Sad Cum", 
                                        "World Of Dogs", "Lil Boy", "Two Heavens", "Artificial Death ...", 
                                        "Feels Like a Wheel", "Black Quarterback", "80808", "Hahaha", 
                                        "Deep Web", "Warping", "Birch Please", "Billy Not Really", "Houdini", 
                                        "You Might Think H...", "Double Helix", "Fudge Me Out", "Pss Pss", 
                                        "No Love", "Big Dipper", "Blackjack", "I'm Overflow", "Anne Bonny", 
                                        "Little Richard", "Punk Weight", "Streaky", "Bubbles Buried In...", 
                                        "Black Dice", "Hunger Games", "Eh", "Three Bedrooms In...", "Whammy", 
                                        "Voila", "Say Hey Kid", "Bootleg (Don't Ne...", "System Blower"
), album_name = c("The Powers That B", "Bottomless Pit", "Year Of The Snitch", 
                  "Bottomless Pit", "The Powers That B", "The Powers That B", "The Powers That B", 
                  "Year Of The Snitch", "Year Of The Snitch", "Government Plates", 
                  "Government Plates", "The Powers That B", "The Powers That B", 
                  "Government Plates", "The Powers That B", "Bottomless Pit", "Year Of The Snitch", 
                  "The Money Store", "Year Of The Snitch", "The Money Store", "Year Of The Snitch", 
                  "No Love Deep Web", "No Love Deep Web", "The Powers That B", 
                  "No Love Deep Web", "Bottomless Pit", "The Powers That B", "The Powers That B", 
                  "The Money Store", "Government Plates", "Year Of The Snitch", 
                  "Bottomless Pit", "Bottomless Pit", "Bottomless Pit", "The Money Store", 
                  "The Money Store", "Year Of The Snitch", "No Love Deep Web", 
                  "No Love Deep Web", "The Money Store", "Year Of The Snitch", 
                  "The Money Store", "Government Plates", "Year Of The Snitch", 
                  "The Money Store", "The Powers That B", "No Love Deep Web", "No Love Deep Web", 
                  "Government Plates", "No Love Deep Web", "Government Plates", 
                  "The Powers That B", "Bottomless Pit", "Year Of The Snitch", 
                  "No Love Deep Web", "Bottomless Pit", "The Money Store", "The Powers That B", 
                  "Bottomless Pit", "Government Plates", "The Money Store", "The Powers That B", 
                  "The Powers That B", "No Love Deep Web", "The Powers That B", 
                  "The Money Store", "Government Plates", "Government Plates", 
                  "Year Of The Snitch", "The Money Store", "Year Of The Snitch", 
                  "Bottomless Pit", "No Love Deep Web", "No Love Deep Web", "Bottomless Pit", 
                  "Bottomless Pit", "No Love Deep Web", "The Powers That B", "The Powers That B", 
                  "Government Plates", "The Money Store"), Lyrics = c(31.08, 24.143, 
                                                                      25.737, 7.622, 51.646, 2.002, 4.594, 9.725, 2.516, 5.246, 100, 
                                                                      0, 12.335, 6.596, 8.465, 36.698, 0, 11.942, 1.842, 18, 10.878, 
                                                                      9.789, 5.666, 5.424, 16.887, 15.804, 17.004, 5.176, 9.238, 3.632, 
                                                                      0, 7.508, 9.869, 12.122, 8.513, 6.592, 14.161, 16.147, 27.919, 
                                                                      20.012, 9.206, 12.967, 1.5, 5.126, 23.855, 9.232, 55, 5.742, 
                                                                      5.234, 9.531, 0, 5.061, 2.269, 19.701, 16.347, 14.814, 27.729, 
                                                                      8.07, 5.929, 17.314, 7.662, 11.548, 1.597, 17.321, 1.677, 11.054, 
                                                                      17.419, 32.67, 4.546, 7.415, 1.216, 20.313, 7.6, 9.318, 6.528, 
                                                                      4.596, 4.502, 0, 3.595, 0, 12.304), Sound = c(96.824, 97.838, 
                                                                                                                    97.228, 100, 83.883, 97.707, 94.778, 89.887, 91.609, 90.273, 
                                                                                                                    55.503, 87.246, 79.609, 81.347, 80.133, 69.586, 80.187, 75.793, 
                                                                                                                    79.366, 73.455, 70.673, 70.528, 70.561, 70.202, 65.527, 65.39, 
                                                                                                                    61.156, 64.868, 62.079, 62.692, 63.933, 60.316, 58.697, 57.512, 
                                                                                                                    58.405, 58.814, 55.972, 54.812, 50.329, 52.691, 54.86, 53.172, 
                                                                                                                    56.819, 54.817, 47.866, 52.755, 35.19, 51.712, 51.845, 49.794, 
                                                                                                                    52.59, 50.728, 50.213, 43.865, 44.781, 44.701, 38.678, 44.933, 
                                                                                                                    44.271, 39.048, 41.711, 38.639, 42.115, 33.447, 38.191, 34.102, 
                                                                                                                    29.118, 23.501, 31.183, 29.854, 28.804, 21.675, 25.295, 23.077, 
                                                                                                                    23.863, 22.457, 18.032, 15.16, 9.148, 10.05, 0), Overall = c(100, 
                                                                                                                                                                                 98.563, 98.529, 94.893, 94.674, 90.722, 88.798, 85.864, 85.015, 
                                                                                                                                                                                 84.677, 84.126, 79.927, 76.854, 76.531, 76.011, 75.666, 73.113, 
                                                                                                                                                                                 73.033, 72.963, 72.886, 67.72, 67.2, 65.796, 65.365, 64.847, 
                                                                                                                                                                                 64.337, 60.667, 60.13, 58.853, 57.492, 57.424, 56.549, 55.809, 
                                                                                                                                                                                 55.45, 55.054, 54.78, 54.674, 54.246, 54.02, 53.545, 51.874, 
                                                                                                                                                                                 51.555, 51.08, 50.412, 50.226, 49.851, 48.842, 47.629, 47.58, 
                                                                                                                                                                                 47.097, 46.476, 46.442, 44.972, 44.917, 44.633, 44.022, 42.707, 
                                                                                                                                                                                 41.896, 40.511, 39.437, 38.644, 37.033, 36.921, 34.032, 33.161, 
                                                                                                                                                                                 32.482, 29.888, 29.779, 27.396, 27.114, 23.94, 23.713, 22.777, 
                                                                                                                                                                                 21.235, 21.022, 18.991, 14.687, 10.347, 5.796, 5.415, 0)), .Names = c("track_name", 
                                                                                                                                                                                                                                                       "album_name", "Lyrics", "Sound", "Overall"), class = c("tbl_df", 
                                                                                                                                                                                                                                                                                                              "tbl", "data.frame"), row.names = c(NA, -81L))

mypalette <- colorRampPalette(RColorBrewer::brewer.pal(9, "Reds"), space = "Lab")

mydata %>%
  gather(key, value, -c(track_name, album_name)) %>%
  filter(album_name == "The Money Store") %>%
  ggplot(aes(x = ordered(key, levels = c("Lyrics", "Sound", "Overall")), y = track_name, fill = value)) +
  geom_tile() + 
  coord_equal(expand = TRUE) + 
  labs(title = "How Angry Are Songs from Each Death Grips Album?", x = "", y = "", subtitle = "The Money Store (2012)") + 
  scale_fill_gradientn(colors = mypalette(100), breaks = c(60, 20), labels = c("More Angry", "Less Angry")) +
  guides(show.legend = FALSE) +
  theme_void() +
  theme(plot.title = element_text(family = "Tw Cen MT Condensed", hjust = 0.5, size = 40, face = "bold", margin = margin(1, 0, 20, 0)), 
          axis.text.x = element_text(family = "Tw Cen MT Condensed", size = 18, angle = 90, hjust = 1, face = "bold", margin = margin(6,0,3,0)),
          axis.text.y = element_text(family = "Tw Cen MT Condensed", size = 18, face = "bold", margin = margin(0, 10, 0, 0)), 
          axis.ticks.y = element_blank(),
          axis.title = element_blank(), 
          legend.title = element_blank(),
          legend.text = element_text(family = "Tw Cen MT Condensed", size = 14, face = "bold", margin = margin(1, 1, 1, 10)),
          legend.margin = margin(0, 0, 0, 30),
          plot.margin = unit(c(5, 0, 8, 0), "mm"),
          plot.subtitle = element_text(family = "Tw Cen MT Condensed", hjust = 0.5, size = 22, face = "bold", margin = margin(0, 0, 30, 0)))

Created on 2019-01-21 by the reprex package (v0.2.1.9000)

Upvotes: 0

Views: 213

Answers (1)

Jake Kaupp
Jake Kaupp

Reputation: 8072

You need to get the order that you want track_name to be in, and then use that order to create a factor.

library(tidyverse)
library(RColorBrewer)
library(extrafont)

plot_data <- mydata %>%
  gather(key, value, -c(track_name, album_name)) %>%
  filter(album_name == "The Money Store")

order <- plot_data %>%
  filter(key == "Overall") %>% 
  arrange(value) %>% 
  pull(track_name)

plot_data %>% 
  mutate(track_name = factor(track_name, order)) %>% 
  ggplot(aes(x = ordered(key, levels = c("Lyrics", "Sound", "Overall")), y = track_name, fill = value)) +
  geom_tile() + 
  coord_equal(expand = TRUE) + 
  labs(title = "How Angry Are Songs from Each Death Grips Album?", x = "", y = "", subtitle = "The Money Store (2012)") + 
  scale_fill_gradientn(colors = mypalette(100), breaks = c(60, 20), labels = c("More Angry", "Less Angry")) +
  guides(show.legend = FALSE) +
  theme_void() +
  theme(plot.title = element_text(family = "Arial", hjust = 0.5, size = 40, face = "bold", margin = margin(1, 0, 20, 0)), 
        axis.text.x = element_text(family = "Arial", size = 18, angle = 90, hjust = 1, face = "bold", margin = margin(6,0,3,0)),
        axis.text.y = element_text(family = "Arial", size = 18, face = "bold", margin = margin(0, 10, 0, 0)), 
        axis.ticks.y = element_blank(),
        axis.title = element_blank(), 
        legend.title = element_blank(),
        legend.text = element_text(family = "Arial", size = 14, face = "bold", margin = margin(1, 1, 1, 10)),
        legend.margin = margin(0, 0, 0, 30),
        plot.margin = unit(c(5, 0, 8, 0), "mm"),
        plot.subtitle = element_text(family = "Arial", hjust = 0.5, size = 22, face = "bold", margin = margin(0, 0, 30, 0)))

Created on 2019-01-21 by the reprex package (v0.2.1)

Upvotes: 1

Related Questions