在ggplot2中移动图例标题

时间:2016-09-18 07:23:26

标签: r ggplot2 legend

我一直试图使用指南功能将我的图例标题转移到图例内容的中心位置。我一直在尝试使用以下代码:

guides(colour=guide_legend(title.hjust = 20))

我想过尝试制作一个可重现的例子,但我认为它不起作用的原因与上述行没有特别匹配我的其余代码有关。所以这是我在我的情节中使用的其余代码:

NH4.cum <- ggplot(data=NH4_by_Date, aes(x=date, y=avg.NH4, group = CO2, colour=CO2)) +
  geom_line(aes(linetype=CO2), size=1) +              #line options
  geom_point(size=3) +                                      #point symbol sizes
  #scale_shape_manual(values = c(1, 16)) +                    #manually choose symbols
  theme_bw()+
  theme(axis.text.x=element_text(colour="white"),  #change x axis labels to white.
        axis.title=element_text(size=12),
    axis.title.x = element_text(color="white"),  #Change x axis label colour to white
    panel.border = element_blank(),                          #remove box boarder
        axis.line.x = element_line(color="black", size = 0.5),      #add x axis line
        axis.line.y = element_line(color="black", size = 0.5),      #add y axis line
        legend.key = element_blank(),                                           #remove grey box from around legend
       legend.position = c(0.9, 0.6))+                                              #change legend position
  geom_vline(xintercept=c(1.4,7.5), linetype="dotted", color="black")+   #put in dotted lines for season boundaries
  scale_color_manual(values = c("#FF6600", "green4", "#0099FF"),
                 name=expression(CO[2]~concentration~(ppm))) +                 #manually define line colour
  scale_linetype_manual(guide="none", values=c("solid", "solid", "solid")) +      #manually define line types 
  scale_shape_manual(values = c(16, 16, 16)) +                    #manually choose symbols
  guides(colour=guide_legend(title.hjust = 20))+
  scale_y_continuous(expand = c(0, 0), limits = c(0,2200), breaks=seq(0,2200,200))+     #change x axis to intercept y axis at 0
   xlab("Date")+
  ylab(expression(Membrane~available~NH[4]^{" +"}~-N~(~mu~g~resin^{-1}~14~day^{-1})))+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  geom_errorbar(aes(ymin = avg.NH4 - se.NH4,                #set y error bars
                ymax = avg.NH4 + se.NH4),
            width=0.1) 

我尝试过以下操作而没有运气:

guides(fill=guide_legend(title.hjust=20)

我还调整了hjust值从-2到20之间的值,只是为了看看是否有所作为,但它没有。

到目前为止,我会尝试附上图表的图片,以便您可以看到我正在谈论的内容。 Note: I'm trying to shift the Co2 concentration (ppm) title to be centered over the legend objects

我已经查看了所有关于堆栈溢出的问题,据我所知,这不是重复的,因为它特定于我自己某处的编码错误。 提前谢谢!!

1 个答案:

答案 0 :(得分:1)

显而易见的方法,例如

theme(legend.title = element_text(hjust = .5))

对我不起作用。我想知道它是否与ggplot2中的open issue有关。在任何情况下,一种手动方法是删除图例标题,并手动定位新标题:

ggplot(mtcars, aes(x = wt, y = mpg, colour = factor(cyl))) +
  geom_point() +
  stat_smooth(se = FALSE) +
  theme_bw() +
  theme(legend.position = c(.85, .6),
        legend.title = element_blank(),
        legend.background = element_rect(fill = alpha("white", 0)),
        panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()) +
  annotate("text", x = 5, y = 27, size = 3,
           label = "CO[2]~concentration~(ppm)", parse = TRUE)

输出: enter image description here