ggplot2: theme(legend.title) ignores vjust and hjust arguments

1,207 views
Skip to first unread message

trichter

unread,
May 30, 2015, 1:17:59 AM5/30/15
to ggp...@googlegroups.com
From the code below, the arguments `hjust` and `vjust` in the `theme(legend.title)` do not do anything (at least on my machine). The two plots produced from both `ggplot()` calls are the same. I tested if `theme_bw()` was the culprit, but it wasnt. What would be the solution to move the legend title around?

This question wasnt answered on stack overflow.

   
  
  library(ggplot2)
    library
(reshape)
    library
(RColorBrewer)
    test
<- structure(list(names = structure(c(1L, 4L, 5L, 6L, 7L, 8L, 9L,
   
10L, 11L, 2L, 3L), .Label = c("Spec1", "Spec10", "Spec11", "Spec2",
   
"Spec3", "Spec4", "Spec5", "Spec6", "Spec7", "Spec8", "Spec9"
   
), class = "factor"), A = c(0.217031528, 0.370637045, 0.152473461,
   
0.08340091, 0.094257483, 0.00619633, 0.043205056, 0.017717884,
   
0.004354587, 0.000349229, 0.010376486), B = c(0.312070285, 0.311236549,
   
0.139193608, 0.07284637, 0.097903335, 0.003568091, 0.028477238,
   
0.021042976, 0.004963161, 0.000374577, 0.00832381), C = c(0.281876963,
   
0.326092831, 0.152957419, 0.07237009, 0.10259602, 0.004158686,
   
0.026662316, 0.020823785, 0.004313649, 0.00037274, 0.007775501
   
), D = c(0.275453548, 0.337083347, 0.154469202, 0.070573145,
   
0.099172727, 0.005437018, 0.02768352, 0.018713749, 0.003950163,
   
0.000362092, 0.00710149), E = c(0.278508956, 0.334527205, 0.154663425,
   
0.068355587, 0.101934925, 0.005645608, 0.025961027, 0.019175166,
   
0.004090645, 0.000386265, 0.00675119), F = c(0.306981913, 0.328928827,
   
0.147765154, 0.058429727, 0.094863912, 0.003795248, 0.024415702,
   
0.02349575, 0.003980418, 0.000353114, 0.006990233)), .Names = c("names",
   
"A", "B", "C", "D", "E", "F"), class = "data.frame", row.names = c(NA,
   
-11L))

    n1
<- as.vector(test$names)
    test
.melt <- melt(test, id.vars="names")
    names
(test.melt) <- c("Species", "Treatment", "Abundance")
       
    colourCount
<- length(unique(test$names))
    getPalette
<- colorRampPalette(brewer.pal(colourCount, "Set3"))
       
    ggplot
() +
          geom_bar
(data=test.melt, aes(x = Treatment, y = Abundance, fill = Species), stat="identity", colour="black") +
          scale_fill_manual
(values= getPalette(colourCount), breaks=n1) +
          xlab
("Treatments") +
          ylab
("Relative Abundance") +
          ggtitle
("Some Title") +
          theme_bw
() +
          theme
(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"),
                legend
.title = element_text(size=16, face="bold", vjust=20, hjust=10),
                plot
.title = element_text(size=20, vjust = 2, face="bold"))
       
    ggplot
() +
          geom_bar
(data=test.melt, aes(x = Treatment, y = Abundance, fill = Species), stat="identity", colour="black") +
          scale_fill_manual
(values= getPalette(colourCount), breaks=n1) +
          xlab
("Treatments") +
          ylab
("Relative Abundance") +
          ggtitle
("Some Title") +
          theme_bw
() +
          theme
(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"),
                legend
.title = element_text(size=16, face="bold", vjust=2, hjust=1),
                plot
.title = element_text(size=20, vjust = 2, face="bold"))


Dennis Murphy

unread,
May 30, 2015, 10:15:53 PM5/30/15
to trichter, ggplot2
Hi:

I don't know if I have much to offer here, but I did a little
experiment with your code, starting by reducing the size of the legend
title to see if it were easier to spot changes in positioning by
slightly modifying the code.

ggplot() +
geom_bar(data=test.melt, aes(x = Treatment, y = Abundance,
fill = Species), stat="identity", colour="black") +
scale_fill_manual(values= getPalette(colourCount), breaks=n1) +
xlab("Treatments") +
ylab("Relative Abundance") +
ggtitle("Some Title") +
theme_bw() +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"),
legend.title = element_text(size=8, face="bold"),
plot.title = element_text(size=20, vjust = 2, face="bold"))

Changing vjust and hjust inside the element_text() call for
legend.title had no effect on the legend title at all. I tried 0, 1
and 2 as values of hjust and 0, 1, 2, 10, -10 for vjust inside
element_text() with no effect on the title.

Next, I tried guide_legend() and had some partial success:

last_plot() + guides(fill = guide_legend(title.hjust = 2)) #
results in movement of the title (so did 1)
last_plot() + guides(fill = guide_legend(title.vjust = 2) # no
change in vertical positioning

Any value in the previous set for vjust applied with this call had no
effect on the legend title. When you make the legend title size 16pt,
then its horizontal adjustment no longer 'works' when legend_guide()
is called.

In the case of horizontal adjustment, I imagine that the size of the
legend box constrains the movement of the title text when it is 16pt
in size. The same may apply to the vertical adjustment, but the lack
of movement with any of the calls above seems a bit suspicious to me.
I'm not willing to call it a bug, but you might want to file an issue
on the ggplot2 issues page regarding vertical adjustments in legend
titles...unless someone comes up with a more satisfying answer.

Dennis
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Pan, Yuanji (NIH/NCI) [C]

unread,
Jun 1, 2015, 12:37:12 PM6/1/15
to Dennis Murphy, trichter, ggplot2
Hi

I tried following code based on some examples I found line. It seem that following code can adjust both hjust and vjust with some exceptions

1. if the size is above 13, can not adjust hjust, can only adjust vjust
2. if size less than or equal to 13, can adjust both hjust and vjust
3. angel in element_test must be greater than 0 in order to adjust both hjust and vjust
4. if angel in element_test is 0, can only adjust hjust

ggplot() +
geom_bar(data=test.melt, aes(x = Treatment, y = Abundance, fill = Species), stat="identity", colour="black") +
scale_fill_manual(values= getPalette(colourCount), breaks=n1) +
xlab("Treatments") +
ylab("Relative Abundance") +
ggtitle("Some Title") +
theme_bw() +
theme(axis.text=element_text(size=12),axis.title=element_text(size=14,face="bold"),
legend.title = element_text(size=16, face="bold"),
plot.title = element_text(size=20, vjust = 2, face="bold"))

last_plot() + guides(fill = guide_legend(
title.theme = element_text(size=13, face="bold", colour = "black", angle = 0.05), title.hjust=-2, title.vjust=200))


last_plot() + guides(fill = guide_legend(
title.theme = element_text(size=13, face="bold", colour = "black", angle = 0.05), title.hjust=2, title.vjust=20))


last_plot() + guides(fill = guide_legend(
title.theme = element_text(size=13, face="bold", colour = "black", angle = 0.05), title.hjust=0, title.vjust=0))


david

Joe Ceradini

unread,
Aug 29, 2015, 8:24:22 PM8/29/15
to ggplot2, djm...@gmail.com, tric...@uni-bremen.de, pa...@mail.nih.gov
I was recently struggling with the same problem: hjust and vjust apparently not working within theme(legend.title()). Although I have no idea why that is, I thought I'd share my workaround.

First make the legend box transparent, 
legend.background = element_rect(fill="transparent")

Then add a title with annotate, 
annotate(geom="text", x = , y = , label = "Legend Title", size = , fontace = "plain", family = "Times")

When I tried annotate without making the legend background transparent, the legend covered the annotate text.

Joe

Hadley Wickham

unread,
Aug 30, 2015, 2:36:27 PM8/30/15
to trichter, ggplot2
This should be fixed in the dev version, which also introduces margins
around the titles.
Hadley
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.



--
http://had.co.nz/
Reply all
Reply to author
Forward
0 new messages