2

I am trying to move the two legend text labels to the top and bottom of the colored legend, respectively. The hjust and vjust argument successfully moves the first text label, but does not change the second. I've tried using c() or even mutating a "newvjust" variable to use in the argument, but am given an "error in valid.viewport...must all be units of length 1."

Here is some relevant code (renamed generically), and a picture of where I've gotten thus far.

    p <- ggplot(data, aes(xvar, yvar)) + 
  geom_tile(aes(fill = colour)) + 
  # ggtitle("Heatmap") + 
  labs(x = "xaxis", 
       y = "yaxis") + theme_classic() + 
  theme(axis.text.y = element_text(angle = 17.43, hjust = 1, size = 5),
        legend.justification = c(0, 1), 
        legend.position = "right",
        legend.text=element_text(size = 3.7, vjust = 1.5, hjust = -2))  +
  scale_x_continuous(breaks = c(106:115)) + 
  geom_vline(xintercept = c(106.5, 107.5, 108.5, 109.5, 110.5, 111.5, 112.5, 113.5, 114.5, 115.5), alpha = 0.15) + 
  geom_hline(yintercept = c((1:126) + .5), alpha = .15)
p + scale_fill_manual(values = c("lightblue", 
                                   "green"),
                        labels = c("Legend Label 1", 
                                   "Legend Label 2"), 
                        name = NULL) 

As you see, only the "Lab1" legend text is moving, though I want Lengend Label 2 to appear at the bottom:

enter image description here

Thanks in advance!!

s__
  • 9,270
  • 3
  • 27
  • 45
  • 1
    Welcome to StackOverflow. When posting a question, be sure to post a [complete, reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Anonymous coward Oct 25 '18 at 17:06
  • This looks like a bug in ggplot. I haven't looked to see if there's already an issue posted, but here's a reprex: `library(ggplot2); data.frame(LET = rep(LETTERS[1:3], each = 3), let = rep(letters[1:3], times = 3), colour = sample(c("First label", "Second label"), 9, replace = T)) %>% ggplot(aes(LET, let, fill = colour)) + geom_tile() + theme(legend.text=element_text(vjust = 0.5, hjust = -1))` – Jon Spring Oct 26 '18 at 00:44
  • Thanks @Jon Spring for providing the reprex, I appreciate you doing that! And alright, so it may be a bug, thanks. – Eliza Riley Oct 26 '18 at 14:25
  • @Anonymous coward, does having this reprex help - any further thoughts on how to resolve this ? – Eliza Riley Oct 26 '18 at 14:25
  • There's either a special way you have to modify it, or like Jon Spring said, a bug. Messing with `hjust` only modifies the first label, but `vjust` modifies both. `ggplot(aes(LET, let, fill = colour)) + geom_tile() + scale_fill_discrete(guide = guide_legend(label.vjust = 4, label.hjust = 1))`. I have a solution that would place both labels either above or below the key, basically rotate the legend horizontally, but can't figure out one above and one below. – Anonymous coward Oct 26 '18 at 15:05
  • One unwieldy but possible advanced R solution would be to get under the hood with the grobs (graphical object / element) and move it there. Or more simply, output the graph as a PDF or SVG (for instance using `rvg`) and tweak the positioning outside of R. – Jon Spring Oct 26 '18 at 19:51
  • 1
    Here's a thread in the `ggplot` issues page on a related note. Looks like it might be possible to adjust using `guides` but text alignment not currently working in `theme`. https://github.com/tidyverse/ggplot2/issues/2465 – Jon Spring Oct 26 '18 at 19:54

0 Answers0