Vaibhav Singh
Vaibhav Singh

Reputation: 1209

geom_tile plotting longer tiles & not square tiles

enter image description hereI am trying to plot a heatmap using geom_tile() in R, however tiles in my plot doesn't come out to be square shape like something over here Expected Output format:(https://twitter.com/jakekaupp/status/1092974571383386112).

The tiles are like a rectangle instead of squares. I have tried using cord_equal(), changes size, width etc but nothing works out.

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())

Upvotes: 1

Views: 1034

Answers (1)

Edward
Edward

Reputation: 18543

There's not much you can do because you have so many x values and many missing. I think this is the best you can do...

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

Upvotes: 1

Related Questions