简体   繁体   English

在ggplot2中调整图例标题的位置和字体大小

[英]Adjust position and font size of legend title in ggplot2

I am adjusting the font size of ggplot2 labels to make them more readable in large format.我正在调整 ggplot2 标签的字体大小,以使它们在大格式下更具可读性。 This works very well except for the legend title.除了图例标题外,这非常有效。 This is illustrated by the following code:下面的代码说明了这一点:

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

The re-sized legend title is no longer properly aligned in the legend box, but is protruding out to the left.重新调整大小的图例标题不再在图例框中正确对齐,而是向左突出。 The effect is even worse for longer titles.对于较长的标题,效果更糟。 I have tried defining custom values for the vjust and hjust parameters, but there is no apparent response.我曾尝试为 vjust 和 hjust 参数定义自定义值,但没有明显的响应。

Is there a way to adjust the alignment of a re-sized legend title?有没有办法调整调整大小的图例标题的对齐方式?

如果您使用的是 ggplot 0.9.1 版本,这适用于更改图例标题的位置和大小

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

Yes, using a feature of the new 0.9.0 version, guides :是的,使用新 0.9.0 版本的一个特性, guides

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

You can read about guides here .您可以在此处阅读guides

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM