图例标题的垂直对齐(ggplot2) [英] Vertical alignment of legend title (ggplot2)

查看:138
本文介绍了图例标题的垂直对齐(ggplot2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将图例标题与ggplot2中的colorbar对齐。我怎样才能使标题垂直对齐到颜色条(而不是整个颜色条加数字)?

现在我得到这个结果,尝试各种主题 element_text 指南选项,无论我做垂直位置title不会改变。



  library(ggplot2)
library(grid)
theme_set(theme_bw()+
theme .grid.major = element_blank(),
panel.grid.minor = element_blank(),
legend.position =bottom,
legend.text = element_text(size = 14),
legend.title = element_text(size = 14,hjust = -0.4),
legend.direction ='horizo​​ntal',
legend.box =vertical,
strip。 background = element_rect(color ='black',fill =white),
plot.margin =单位(0.05 * c(1,1,1,1),'npc'),
图.title伪= element_text(大小= 18) ,
axis.title = element_text(size = 18,vjust = 0.2),
axis.text = element_text(size = 14),
strip.text = element_text(size = 14)) )

ggplot(钻石,aes(x,y,color = z))+
geom_point()+
scale_colour_gradient2('Time [min]',
low ='lightgray',
mid ='red3',
high ='red4',
midpoint = 15)
title.vjust = 来更改图例标题的垂直位置。 >在 guide_colourbar = 中的函数 guides()



<$ ($ color = guide_colourbar(title.vjust = 1))


I am trying to align the legend title to the colorbar in ggplot2. How can I make the title vertically aligned to the colorbar only (instead of the whole colorbar plus number)?

Up now I get this result, trying various theme, element_text and guides options, whatever I do the vertical position of the title does not change.

library(ggplot2)
library(grid)
theme_set(theme_bw() +
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        legend.position="bottom",
        legend.text=element_text(size=14),
        legend.title=element_text(size=14,hjust=-0.4),
        legend.direction='horizontal',
        legend.box = "vertical",
        strip.background = element_rect(colour='black',fill = "white"),
        plot.margin=unit(0.05*c(1,1,1,1),'npc'),
        plot.title=element_text(size=18),
        axis.title=element_text(size=18,vjust=0.2),
        axis.text=element_text(size=14),
        strip.text=element_text(size=14)))

ggplot(diamonds,aes(x,y,color=z))+
  geom_point()+
  scale_colour_gradient2('Time [min]',
                         low='lightgray',
                         mid='red3',
                         high='red4',
                         midpoint=15)

解决方案

You can change vertical possition of legend title with the argument title.vjust= inside guide_colourbar= of function guides().

 + guides(color=guide_colourbar(title.vjust=1))

这篇关于图例标题的垂直对齐(ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆