2017-02-14 4 views
3

여기 내 그룹화 된 바 플롯 주문은 일부 R 코드와 그래프가 생성됩니다R - 그룹

library(ggplot2) 
year <- c("1950", "1950", "1960", "1960", "1970", "1970") 
weight <- c(15, 10, 20, 25, 18, 20) 
name <- c("obj1", "obj2", "obj3", "obj4", "obj5", "obj1") 
object.data <- data.frame(year, weight, name) 
ggplot(object.data, aes(x=factor(year), y=weight, 
    fill=reorder(name, -weight))) + geom_bar(stat="identity", position="dodge") 

enter image description here

은 어떻게 바가은 높은 것에서 낮은 것 (weight으로 분류되어 있음을 확인 할) 각 개별 그룹 내에서?

obj1은 두 개의 서로 다른 날짜에 두 개의 다른 weight 값이 두 번 나타납니다.

답변

3
# Create a new variable with your desired order. 
object.data1 = object.data %>% 
    group_by(year) %>% 
    mutate(position = rank(-weight)) 

# Then plot 
ggplot(object.data1, 
    aes(x=year, y=weight, fill=reorder(name, -weight), group = position)) + 
    geom_bar(stat="identity", position="dodge") 

enter image description here