如何将图例标题与ggplot2中的图例中间对齐?

编程入门 行业动态 更新时间:2024-10-28 17:18:46
本文介绍了如何将图例标题与ggplot2中的图例中间对齐?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想将图例标题 sex 右移一点到图例框的水平中心。我试过主题和 guide_legend ,但失败了。这两种方式不会改变图例的标题位置。

#来自www.cookbook-r的示例数据/图/传说_(ggplot2)/ df1 < - data.frame( sex = factor(c(女,女,男,男)),时间=因子(c(午餐,晚餐,午餐,晚餐),水平= c(午餐,晚餐)), total_bill = c(13.53,16.81 ,16.24,17.42)) library(ggplot2)p < - ggplot(data = df1,aes(x = time,y = total_bill,group = sex,shape = $ b#不变p + theme(legend.title = element_text(hjust = 0.5))p + guides(color = guide_legend(title.hjust = 0.5))

另外,I我使用ggplot2_2.2.0。

解决方案

您需要 legend.title.align 而不是 legend.title :

p +主题title.align = 0.5)

I want to move the legend title sex a little right to the horizontal center of legend box. I tried theme and guide_legend but failed. Both ways won't change the legend title position.

# example data from www.cookbook-r/Graphs/Legends_(ggplot2)/ df1 <- data.frame( sex = factor(c("Female","Female","Male","Male")), time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")), total_bill = c(13.53, 16.81, 16.24, 17.42) ) library(ggplot2) p <- ggplot(data=df1, aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) + geom_line() + geom_point() # no change p + theme(legend.title = element_text(hjust = 0.5)) p + guides(color=guide_legend(title.hjust=0.5))

In addition, I am using ggplot2_2.2.0.

解决方案

You need legend.title.align rather than legend.title:

p + theme(legend.title.align=0.5)

更多推荐

如何将图例标题与ggplot2中的图例中间对齐?

本文发布于:2023-07-19 17:06:09,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1158583.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图例   如何将   标题

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!