2012-03-09 2320 views
18

我正在调整ggplot2标签的字体大小以使它们在大格式中更具可读性。除了图例标题外,这种方法非常有效。这是由下面的代码所示:在ggplot2中调整图例标题的位置和字体大小

library(ggplot2) 
p <- ggplot(diamonds, aes(carat, price, colour=cut)) + geom_point() + 
    xlab("Carat") + 
    ylab("Price") + 
    opts(legend.position=c(0.85, 0.3)) + 
    opts(axis.title.x=theme_text(size=16)) + 
    opts(axis.title.y=theme_text(size=16, angle=90)) + 
    opts(plot.title=theme_text(size=20)) + 
    opts(legend.text=theme_text(size=14)) + 
    opts(legend.title=theme_text(size=14)) + 
    opts(title="Diamond Prices") 
p 

重新大小的图例标题在图例框不再正确对齐,而是伸出到左边。对于较长的游戏,效果更糟。我曾尝试为vjust和hjust参数定义自定义值,但没有明显的响应。

有没有办法调整重新调整大小的图例标题的对齐方式?

回答

14

是,使用新版本0.9.0的特征,guides

p + guides(colour = guide_legend(title.hjust = 0.5)) 

你可以阅读有关guideshere

19

如果您正在使用ggplot 0.9.1版本,这个工程的改变图例标题的位置和大小

p + theme(legend.position=c(0.85, 0.3),legend.title=element_text(size=14))