2016-09-18 2 views
0

가이드 기능을 사용하여 범례 콘텐츠를 중심으로 내 범례 제목을 이동하려고했습니다. 나는 다음과 같은 코드를 사용하려고했습니다 : 나는 재현 할 예제를 만들려고 생각ggplot2에서 범례 제목 이동

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

,하지만 나는 그것이 작동하지 않는 이유는 위의 라인의 나머지 부분과 일치하지 않는 함께 할 수있는 뭔가가 생각하는 내 코드. 그래서 여기 내 음모에 사용하고 나머지 코드입니다 : 나는 또한 값에서 hjust 값을 조정 한

guides(fill=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) 

내가하고 시도는 행운과 대신 다음 2시에서 20시 사이에 차이가 있었는지 알 수 있었지만 그렇지 않았습니다.

그래프의 그림을 첨부하여 지금까지 내가 무엇을 말하고 있는지 볼 것입니다. Note: I'm trying to shift the Co2 concentration (ppm) title to be centered over the legend objects

스택 오버플로에 관한 모든 질문을 살펴 보았습니다. 제 자신의 어딘가에 코딩 오류가 있기 때문에 중복되지 않습니다. 미리 감사드립니다!

답변

1

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

은 저에게 적합하지 않았습니다. 그것이 open issue과 관련이 있는지 궁금하다. ggplot2.

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

어떤 경우에는, 하나의 매뉴얼 접근 방식은 수동으로 새 범례 제목을 제거하고 배치하는 것
관련 문제