简体   繁体   English

ggplot图例标题顶部中心

[英]ggplot legend title top center

Is there a way to put legend title on top and center compared to the legend itself?与图例本身相比,有没有办法将图例标题放在顶部和中心?

Example:例子:

cons2 <- data.table(
value_date  = c(as.Date('2013-04-30'),as.Date('2013-04-30'),as.Date('2013-06-13'),as.Date('2013-06-13')),
ticker = c('AAPL','FTW','AAPL','FTW'),
discount = c(0.34,0.10,0.25,0.20),
b = c(0.40,0.55,.60,0.90),
yield = c(0.08,0.04, 0.06,0.03))


headers <- cons2[ticker == 'AAPL' & value_date == '2013-06-13']

p <- ggplot(cons2) 
p <- p + geom_point(aes(yield,b, size = discount, color=factor(value_date))) 
p <- p + guides(size = "none")
p <- p + scale_colour_manual(values = c("#1c2f80","#779438"))
p <- p + geom_text(data = headers, aes(yield, b, label=ticker), size = 4)
p <- p + geom_smooth(data = cons2[value_date == '2013-06-13'], aes(yield, b), method="lm", se=FALSE)
p <- p + geom_line(data = cons2, aes(yield, b, group = ticker))
p <- p + theme(legend.position="bottom")
p <- p + guides(colour=guide_legend(title.position="top"))
p

You can play with title.hjust parameter like this : 您可以像这样使用title.hjust参数播放:

p <- p + guides(colour=guide_legend(title.position="top", 
                                     title.hjust =0.5))

在此处输入图片说明

I think the above is outdated.我认为上面的内容已经过时了。 The following is how to center a legend in ggplot > 3.0:以下是如何在 ggplot > 3.0 中将图例居中:

+ theme(legend.justification = "center")

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

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