2014-12-02 6 views
2

나는 누적 가로 막 대형 차트를 가지고 있지만 그래프 아래쪽에있는 범례를 많은 공백까지 이동하는 데 어려움을 겪고 있습니다.ggplot2의 범례에서 공백을 제거하는 방법

library(ggplot2) 
library(scales) 
library(reshape2) 
Rdates <- seq(as.Date("2004-02-01"), length=120, by="1 month") - 1 
Rdates <- as.Date(Rdates) 
Cnames <- c("Column 1 Really Long","Column 2 Really Long","Column 3 Really Long","Column 4 Really Long","Column 5 Really Long","Column 6 Really Long","Column 7 Really Long","Column 8 Really Long","Column 9 Really Long","Column 10 Really Long") 
MAINDF <- data.frame(replicate(10,runif(120,-0.03,0.03))) 
rownames(MAINDF) <- Rdates 
colnames(MAINDF) <- Cnames 
CUSTOMpalette <- c("#1a2ffa", "#0d177d", "#1a9ffa", "#fa751a", "#4b8e12", "#6fd21b", "#fae51a", "#c3b104", "#f5df05", "#dcc805") 
MAINDF[,"dates"] <- Rdates 

MAINDF <- melt(MAINDF,id.vars="dates") 

postive <- subset(MAINDF,value >= 0) 
negative <- subset(MAINDF,value < 0) 

gg <- ggplot() 
gg <- gg + geom_bar(data = postive , aes(x = dates, y = value, fill = variable),stat="identity") 
gg <- gg + geom_bar(data = negative , aes(x = dates, y = value, fill = variable),stat="identity") 
gg <- gg + scale_x_date(breaks = "3 months", labels=date_format("%b%y"),limits=c(min(as.Date(MAINDF$dates)),max(as.Date(MAINDF$dates)))) 
gg <- gg + theme(
    axis.text.x= element_text(color="black",angle=45, size=10, vjust=0.5), 
    axis.text.y= element_text(color="black", size=12, vjust=0.5), 
    axis.title.y = element_text(color="black",size=12, vjust=0.5), 
    plot.title = element_text(color="black",face="bold",size=14, hjust=0.5,vjust=1), 
    panel.background = element_blank(), 
    panel.border = element_rect(linetype = "solid", colour = "black",fill=NA), 
    legend.position="bottom", 
    legend.title = element_blank(), 
    legend.key = element_rect(fill="white"), legend.background = element_rect(fill=NA) 
) 
gg <- gg + xlab("") + ylab("Monthly Net Returns") 
gg <- gg + ggtitle("Contribution by Strategy") 
gg <- gg + scale_y_continuous(labels = percent_format()) 
gg <- gg + scale_fill_manual(values=CUSTOMpalette) 
gg <- gg + guides(fill=guide_legend(nrow=2,byrow=TRUE)) 
gg 

내가 legend.margin=unit(-2,"mm"),을 사용하려고하지만 제대로 작동 나던 :

여기 내 재현 할 코드입니다. 이 상자를 그래프 가까이로 움직이는 가장 좋은 방법은 무엇입니까? 함께 연주

+0

함께 그 ggplot의 모든 요소를 fyi – rawr

+0

'theme (axis.title.x = element_blank())'는 약간의 도움이되지만 많은 통제력을주지는 못합니다. 그러면 빈 x 제목이 공백을 차지하지 않게됩니다. –

답변

2

시도 : 또한

gg <- gg + theme(legend.position = c(0.5, 0.5))

, 당신은 진정한 재현성에 대한 귀하의 예제에 다음을 추가 할 수 있습니다 : 당신이 문자열 수

library(ggplot2) 
library(reshape2) 
library(scales) 
+0

당신은 내 잘못이야, 여전히 그걸 얻기 위해 노력하고있어. 도움을 주셔서 감사 드리며 앞으로도 계속해서 도서관을 포함하게 될 것입니다. –

+1

전설이 사라질 것이라고 생각합니까? 또 다른 해결책은 [guide_legend] (http://docs.ggplot2.org/0.9.2.1/guide_legend.html) – rawr

+0

을 사용하는 것입니다. 필자는 guide_legend로 놀았지만 모든 전설을 움직이는 행운은 없었습니다. –

관련 문제