2013-08-23 2 views
29

두 개의 열 범례가있는 플롯을 작성하려면 이라고 가정하는 다음 스크립트가 있습니다.ggplot에서 다중 열 범례 만들기

#!/usr/bin/Rscript 
library(ggplot2) 
library(plyr) 
library(reshape2) 
library(scales) 

file <- "http://dpaste.com/1354089/plain/"; 
dat <-read.table(file,header=TRUE); 
datm <- melt(dat) 

# Plot them 
ggplot(datm,aes(x = variable,y = value,fill = Term)) + 
geom_bar(position = "fill") + 
scale_y_continuous(labels = percent_format())+ 
theme(legend.direction ="vertical",legend.position = "bottom")+ 

guides(color=guide_legend(ncol=2))+ # this doesn't seem to work 

ggsave(file="~/Desktop/test.pdf",width=11,height=11) 

그러나 대신 enter image description here

어떻게 제대로 할 수 이 그림을 만들어?

답변

60

는 올바른 미학에 대한 안내를 할당해야합니다 그리고 당신은 채우기 사용 :

guides(fill=guide_legend(ncol=2)) 

을 그리고 당신은 간결한 답변 geom_bar

+0

1과 경고의주의를 기울여야한다. 어떤 제안, 경고를 해결하는 방법? – neversaint