2014-01-25 5 views
1
test<-data.frame(a=c('1','1','2','2'),b=c(2,-2,4,-3),d=c('m','n','m','n')) 
p+geom_bar(position=position_dodge(),stat='identity',fill='deeppink',colour='black',width=0.5) 

enter image description hereggplot2

에서 2 개 개의 서로 다른 색상으로 backgroup 기입 I barplot가 나는 B 값에 대하여 (> 0 < 0) 또는 2 개 가지 색상 플롯 backgroup를 작성하고자 woule 가지고 인자 d (m과 n).

ggplot2에서 가능합니까?

답변

1

편집 : OP의 오해 된 질문 --- 패널 및 geom_rect를 사용하여 원하는대로 어느 정도 접근 할 수 있습니다. 좀 더 조정할 필요는 있지만 이상적으로는 자신 만의 아이디어가 있습니다. Conditional formatting of panel background in GGplot2

EDIT2 : 비슷한 질문 여기를 참조하십시오 다른 변수 (패널) 따라서 시팅가 B에 근거하여 더 이상 발생을 도입하고 좋네요

require(ggplot2) 
test<-data.frame(a=c('1','1','2','2'),b=c(2,-2,4,-3),d=c('m','n','m','n')) 
x <- rep(seq(1,2),2) 
test$panel <- x 
test$colorindicator = ifelse(test$b<0,"negative","positive") 
p=ggplot(data=test,aes(x=a,y=b)) 
p = p + geom_rect(aes(fill = colorindicator), xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, alpha = 1) 
p = p + geom_bar(fill='deeppink',color="black",position=position_dodge(),stat='identity',width=0.5) 
p = p + facet_grid(.~panel,scales="free") 
print(p) 

enter image description here

+0

감사 회신. 하지만 난 줄 바둑판 색상을하지 플롯 backgroup 색상을 변경하고 싶습니다. – user36102

1

@CMichael을, 감사합니다 보인다 제비. 당신의 대답에서, 나는 조명 된 수정 후에 내가 원하는 것을 정확히 가지고 있습니다.

require(ggplot2) 
test<-data.frame(a=c('1','1','2','2'),b=c(2,-2,4,-3),d=c('m','n','m','n')) 
p=ggplot(data=test,aes(x=a,y=b)) 
p = p + geom_rect(fill = 'red', xmin = -Inf, xmax = Inf, ymin =0, ymax = Inf, alpha =0.05) 
p = p + geom_rect(fill = 'green', xmin = -Inf, xmax = Inf, ymin =-Inf, ymax = 0, alpha =0.05) 
p = p + geom_bar(fill='deeppink',color="black",position=position_dodge(),stat='identity',width=0.5) 
print (p) 

enter image description here

+0

좋아 - 이제 알았어. :) – CMichael