2017-03-18 6 views
0

하나의 막대가 지정된 색상으로 구별되는 ggplot2로 막대 차트를 만들고 싶습니다.geom_bar를 사용하는 ggplot - 색상으로 단일 막대 구별

type <- c('apples','pears','bananas','plums','melons','pineapples') 
weight <- c(14,11,19,16,12,8) 
fruit <- data.frame(type,weight) 

이 내가 플롯을 생산하기 위해 지금까지 무엇을 가지고 :

는 간단한 데이터 프레임 설명하기 위해

library("ggplot2") 
f <- ggplot(fruit, aes(x=type, y=weight)) 

f + geom_bar(stat=’identity", fill = (ifelse(fruit$type=='bananas', 'yellow', 'gray'))) 
+0

OK, 그래서 문제가 무엇 : 우리는 요인의 수준은 바나나가 있음을 필요로하기 때문에

, 작동은 다음과의 마지막 줄을 교체? (""대신에'''를 제외하고) – Axeman

+0

바나나라면 '멜론'이 노란색으로 채워진다. – Curious56

+2

아, 괜찮아.'f + geom_col (aes (fill = ifelse $ type == 'bananas', 'yellow', 'gray'))) + scale_fill_identity()' – Axeman

답변

1

ggplot에서 바의 순서는 순서에 따라 달라집니다 요인 변수 fruit$type의 수준.

f + geom_bar(stat="identity", fill = (ifelse(levels(fruit$type)=='bananas', 'yellow', 'gray'))) 
+0

Axeman과 Robert의 솔루션은 모두 잘 작동합니다. – Curious56