Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

ggplot2

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?

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")) 
like image 860
nouse Avatar asked Dec 21 '25 08:12

nouse


1 Answers

I don't know if this was fixed recently, but the solution is to use legend.title.align in theme()

with the example above

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"), 
      legend.title.align=-30, #<<- here
        plot.title = element_text(size=20, vjust = 2, face="bold")) 

plot

like image 142
RBA Avatar answered Dec 23 '25 20:12

RBA