我正在尝试排列热图,以使具有较高“总体”的值出现在顶部,而具有较低“总体”的值出现在底部。问题在于数据(from collections import defaultdict
d = defaultdict(list)
for row in df.to_dict('records'):
d[row.get('1')].append({row.get('0'): row.get('2')})
defaultdict(list,
{'BBB': [{'2019-01-21': 11101110}, {'2019-01-20': 11101110}],
'CCC': [{'2019-01-21': 11101010}, {'2019-01-20': 11101010}],
'DDD': [{'2019-01-21': 10101110}, {'2019-01-20': 10101110}]})
)的排列已经像这样,但是这种排列并未应用到最后。
有什么主意我可以解决这个问题吗?
谢谢。
mydata
由reprex package(v0.2.1.9000)于2019-01-21创建
答案 0 :(得分:1)
您需要获取希望track_name
进入的顺序,然后使用该顺序创建一个因子。
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)))
由reprex package(v0.2.1)于2019-01-21创建