2015-05-29 32 views
2

從下面的代碼中,theme(legend.title)中的參數hjustvjust不會執行任何操作(至少在我的機器上)。兩個ggplot()調用產生的兩個地塊是相同的。我測試如果theme_bw()是罪魁禍首,但它沒有。移動圖例標題的解決方案是什麼?ggplot2:theme(legend.title)忽略vjust和hjust參數

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")) 
+0

從[這裏] (http://docs.ggplot2.org/0.9.3.1/element_text.html),'hjust'和'vjust'必須在[0,1]。另外,請在使用它們時將調用添加到「RColorBrewer」和「reshape」(或「reshape2」)包中。 – 2015-05-29 08:37:24

+0

Thanks.Ill試試。根據https://github.com/hadley/ggplot2/wiki/Legend-Attributes,語法是一個單一的數字,但。 – nouse

+0

@Pascal'vjust'和'hjust'值可能在[0,1]範圍之外。此外,您也可以爲它們使用負值。例如,當您使用'axis.text = element_text(size = 12,vjust = -1.5)'時,您可以看到效果。 – Jaap

回答

1

我不知道這是否是最近固定的,但解決的辦法是在theme()

使用legend.title.align與上面的例子中

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

+1

「legend.title.align = -30」水平調整,不會在我的身材上垂直偏移。 – Raphael