2012-06-16 5 views
3

누구든지 A-atype B-btype C-ctype에만을 qplot 그래프의 범례로 추가 할 수 있습니까? 내 전설은 그래프 상단에 있어야하며 또한 수평이어야합니다. 그래프를 생성하기 위해 사용하는 데이터와 qplot 코드에 대해서도 아래를 참조하십시오.내 자신의 전설 qplot에

데이터 :

type name value 
A john 47.6 
B john 55.6 
C john 57.7 
A amy 13.15 
B amy 23.1 
C amy 24.9 
A lily 14.0 
B lily 18.1 
C lily 21.9 
A sam 38.8 
B sam 44.8 
C sam 45.9 
A frank 63.6 
B frank 64.4 
C frank 64.7 
A xia 52.8 
B xia 54.1 
C xia 56.4 
A yan 53.8 
B yan 56.1 
C yan 58.6 

코드 : 당신이 후있어이 같은

qplot(type, value, geom="bar", fill=c(""),data=temp, group=name, 
    facets= .~name,stat="identity") 
+ theme_bw()    
+ opts(axis.title.y=theme_text(size=16,angle=90),axis.title.x=theme_text(size=16,angle=0)) 
+ opts(axis.text.x=theme_text(size=10)) 
+ opts(axis.text.y=theme_text(size=14))  
+ opts(strip.background = theme_rect(colour = 'steelblue', fill = 'white', size = 2)) 
+ opts(legend.title=theme_blank()) 
+ opts(legend.position="top") 

enter image description here

답변

7

인가? 먼저 범례를 그린 다음 범례의 색이 지정된 키를 제거합니다. fill = type은 범례를 그립니다. scale_fill_manual은 막대가 동일한 색상을 가지도록하고 원하는 레이블을 범례에 삽입합니다. guides은 유색 키를 제거하거나 범례 레이블 만 남겨두고 키를 흰색으로 채 웁니다. 아마도 guides으로 전화를하면 fill = NA도 작동합니다. 편집 : ggplot2 버전 업데이트 된 코드 2

library(ggplot2) 

temp = read.table(text = " 
type name value 
A john 47.6 
B john 55.6 
C john 57.7 
A amy 13.15 
B amy 23.1 
C amy 24.9 
A lily 14.0 
B lily 18.1 
C lily 21.9 
A sam 38.8 
B sam 44.8 
C sam 45.9 
A frank 63.6 
B frank 64.4 
C frank 64.7 
A xia 52.8 
B xia 54.1 
C xia 56.4 
A yan 53.8 
B yan 56.1 
C yan 58.6", header = TRUE, sep = "") 


qplot(type, value, geom="col", fill=type, data=temp, group=name, facets= .~name) + 
    scale_fill_manual(values = rep("salmon",3), labels = c("A - atype", "B - btype", "C - ctype")) + 
    guides(fill = guide_legend(override.aes = list(fill = "white"))) + 
    theme_bw() + 
    theme(axis.title.y=element_text(size=16,angle=90), 
     axis.title.x=element_text(size=16,angle=0), 
     axis.text.x=element_text(size=10), 
     axis.text.y=element_text(size=14), 
     strip.background = element_rect(colour = 'steelblue', fill = 'white', size = 2), 
     legend.title=element_blank(), 
     legend.position="top", 
     legend.key = element_rect(colour = NA)) 

enter image description here