Clean ways to move ggplot2 caption away from plot (but still on image) without crazy hjust?

Hi everyone,

I'm plotting a heatmap with ggplot2 and, based on the structure of the heatmap plot, I'm having trouble putting a caption far to the right away from the actual plot.

Does anyone have any clean and easy ways I can get the caption away from the plot to the right without using a crazy hjust = 15 or something like that?

Thanks!

library(dplyr)
library(ggplot2)
library(RColorBrewer)

mydata <- structure(list(track_name = structure(c(13L, 12L, 11L, 10L, 9L, 
8L, 7L, 6L, 5L, 4L, 3L, 2L, 1L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 
6L, 5L, 4L, 3L, 2L, 1L, 13L, 12L, 11L, 10L, 9L, 8L, 7L, 6L, 5L, 
4L, 3L, 2L, 1L), .Label = c("System Blower", "Punk Weight", "Blackjack", 
"Double Helix", "Birtch Please", "The Cage", "I've Seen Footage", 
"Hacker", "Lost Boys", "Get Got", "Fudge That", "Hustle Bones", 
"The Fever (Aye Aye)"), class = "factor"), album_name = c("The Money Store", 
"The Money Store", "The Money Store", "The Money Store", "The Money Store", 
"The Money Store", "The Money Store", "The Money Store", "The Money Store", 
"The Money Store", "The Money Store", "The Money Store", "The Money Store", 
"The Money Store", "The Money Store", "The Money Store", "The Money Store", 
"The Money Store", "The Money Store", "The Money Store", "The Money Store", 
"The Money Store", "The Money Store", "The Money Store", "The Money Store", 
"The Money Store", "The Money Store", "The Money Store", "The Money Store", 
"The Money Store", "The Money Store", "The Money Store", "The Money Store", 
"The Money Store", "The Money Store", "The Money Store", "The Money Store", 
"The Money Store", "The Money Store"), key = c("Lyrics", "Lyrics", 
"Lyrics", "Lyrics", "Lyrics", "Lyrics", "Lyrics", "Lyrics", "Lyrics", 
"Lyrics", "Lyrics", "Lyrics", "Lyrics", "Sound", "Sound", "Sound", 
"Sound", "Sound", "Sound", "Sound", "Sound", "Sound", "Sound", 
"Sound", "Sound", "Sound", "Index", "Index", "Index", "Index", 
"Index", "Index", "Index", "Index", "Index", "Index", "Index", 
"Index", "Index"), value = c(11.94, 18, 9.24, 8.51, 6.59, 20.01, 
12.97, 23.86, 27.73, 7.66, 11.05, 7.42, 12.3, 75.79, 73.45, 62.08, 
58.4, 58.81, 52.69, 53.17, 47.87, 38.68, 41.71, 34.1, 29.85, 
0, 73.03, 72.89, 58.85, 55.05, 54.78, 53.55, 51.56, 50.23, 42.71, 
38.64, 32.48, 27.11, 0)), .Names = c("track_name", "album_name", 
"key", "value"), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, 
-39L))

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

mydata %>%
  ggplot(aes(x = ordered(key, levels = c("Lyrics", "Sound", "Index")), y = track_name, fill = value)) +
  geom_tile() + 
  coord_equal(expand = TRUE) + 
  scale_fill_gradientn(colors = heatmap_palette(100), breaks = c(55, 20), labels = c("More Angry", "Less Angry")) +
  guides(show.legend = FALSE) +
  labs(title = "How Angry Are Songs From Each Death Grips Album?", x = "", y = "", subtitle = "No Love Deep Web (2012)", caption = "@OppenheimerEvan") +
  theme_void() +
  theme(plot.title = element_text(family = "mono", hjust = 0.5, size = 45, face = "bold", margin = margin(1, 0, 20, 0)), 
        axis.text.x = element_text(family = "mono", size = 20, angle = 40, hjust = 1, face = "bold", margin = margin(6,0,3,0)),
        axis.text.y = element_text(family = "mono", size = 20, 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 = "mono", size = 18, face = "bold", margin = margin(1, 1, 1, 10)),
        legend.margin = margin(0, 0, 0, 30),
        plot.margin = margin(25, 20, 25, 20),
        plot.caption = element_text(family = "mono", size = 18, face = "bold"),
        plot.subtitle = element_text(family = "mono", hjust = 0.5, size = 25, face = "bold", margin = margin(0, 0, 30, 0)))


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

I think the answers to this thread might be of help, in which case you'd be using a grid grob.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.