2017-10-11 4 views
-4

데이터 프레임을 준비하고 ggplot을 사용합니다. 그러나 초기 명령은 존중되지 않습니다. 나는이 명령을 어떻게 존중할 수 있는가? geom_bar (stat = "identity")로 ggplot을 재정렬하십시오.

Patient Nb_peptides Type_affinite 
1  22   563    a 
2  22  1040    b 
3  22  11139    c 
4  24   489    a 
5  24  1120    b 
6  24  11779    c 
7  13   467    a 
8  13  1239    b 
9  13  14600    c 


g_plot <- ggplot(data = nb_peptides_type, 
       aes(x = reorder(Patient, True_order), 
        y = Nb_peptides, 
        fill = Type_affinite)) + 
    geom_bar(stat = "identity") 

print(g_plot) 

enter image description here

+0

'True_order'는 무엇입니까? 나는 당신의 코드에서 그것을 보지 않는다 ... –

답변

0

쉽게하기 위해 독립적 인 코드를 입력하십시오.

나는 요인 레벨을 재정렬하기 위해 플롯의 외부에 levels을 사용할 것입니다. 찾고있는 것이 맞습니까?

## fake dataframe 
df <- data.frame(patient = as.factor(rep((21:30), each=3)), 
       nb = rpois(30, 1000), 
       type=sample(letters[1:3], replace =T, size =30)) 

## initial plot 
ggplot(data = df, 
     aes(x = patient, 
      y = nb, 
      fill = type)) + 
    geom_bar(stat = "identity") 

## adjust factors levels 
True_order <- sample((21:30), 10) 
levels(df$patient) <- True_order 


## re-plot 
ggplot(data = df, 
     aes(x = patient, 
      y = nb, 
      fill = type)) + 
    geom_bar(stat = "identity") 
관련 문제