geom_tile绘制更长的瓷砖而不是正方形瓷砖

时间:2020-03-28 13:52:08

标签: r ggplot2 tidyverse

enter image description here我正在尝试在R中使用geom_tile()绘制热图,但是我的绘图中的图块不会像此处的预期结果那样是正方形的。(https://twitter.com/jakekaupp/status/1092974571383386112 )。

图块就像一个矩形,而不是正方形。我尝试使用cord_equal(),更改大小,宽度等,但没有任何结果。

library(tidyverse)
library(office)
office <- schrute::theoffice

top_3_lines_per_episode <- office %>% 
  group_by(season,episode,episode_name,imdb_rating) %>% 
  count(character) %>%
  top_n(3, n) %>% ungroup() %>% 
  mutate(episode_id = group_indices(., season,episode,episode_name,imdb_rating))

top_3_lines <- top_3_lines_per_episode %>% 
  mutate(episode_mod = episode_id + 3 * parse_number(season)) %>%
  group_by(season) %>%
  mutate(mid = mean(episode_mod)) %>% 
  group_by(character) %>% 
  add_count(name="total_lines") %>% ungroup() %>% 
  mutate(character=fct_lump(character,10),
         character=fct_reorder(character,total_lines)) 

ggplot(top_3_lines, aes(x = as.factor(episode_mod), y = character, fill = imdb_rating)) +
  geom_tile(color = "white", size = 0.05) +
  #coord_equal(ratio = 200) +
 labs(x = NULL, y = NULL, title = "The Office, Chacracters with top 3 lines") +
  scale_fill_viridis(discrete = FALSE, name = "IMDB rating") +
  theme(axis.text.y =element_text(size=8,face="bold"),
        axis.text.x =element_blank())

1 个答案:

答案 0 :(得分:1)

您无能为力,因为您有太多的x值而很多缺失。我认为这是您可以做的最好的事情...

ggplot(top_3_lines, aes(x = as.factor(episode_mod), y = character, fill = imdb_rating)) +
  geom_tile() +
  coord_equal(ratio = 10) +
 labs(x = NULL, y = NULL, title = "The Office, Chacracters with top 3 lines") +
  viridis::scale_fill_viridis(discrete = FALSE, name = "IMDB rating") +
  theme(axis.text.y =element_text(size=8,face="bold"),
        axis.text.x =element_blank())

enter image description here