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

Pau*_*l M 20 r 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
Run Code Online (Sandbox Code Playgroud)

重新调整大小的图例标题不再在图例框中正确对齐,而是向左突出.对于较长的标题,效果甚至更糟.我已经尝试为vjust和hjust参数定义自定义值,但没有明显的响应.

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

Amm*_*Amm 24

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

p + theme(legend.position=c(0.85, 0.3),legend.title=element_text(size=14))
Run Code Online (Sandbox Code Playgroud)


jor*_*ran 15

是的,使用新0.9.0版本的功能,guides:

p + guides(colour = guide_legend(title.hjust = 0.5))
Run Code Online (Sandbox Code Playgroud)

你可以在guides 这里阅读.