2017-09-14 2 views
3

R을 사용하여 막대 그래프를 만들려고하지만 범례의 색상이 잘못된 것 같습니다. R 바코드의 그룹 색상이 잘못되었습니다.

enter image description here

data = c(29,5,22,12,20,11,14,15,21,8) 

colors = c(gray.colors(1, start = .1),gray.colors(1, start = .1), 
      gray.colors(1, start = .3),gray.colors(1, start = .3), 
      gray.colors(1, start = .5),gray.colors(1, start = .5), 
      gray.colors(1, start = .7),gray.colors(1, start = .7), 
      gray.colors(1, start = .9),gray.colors(1, start = .9)) 

names = c('1','1','2','2','3','3','4','4','5','5') 

barplot(rev(data), horiz=TRUE, col = rev(colors), names.arg = rev(names), 
     legend.text = rev(c("1","2","3","4","5")), las=1, xlim = c(0,30), 
     args.legend = list(x ='bottomright', inset=c(0,0.05)) 
) 

나는이 원인을 무엇을 상상할 수

. 내 초기 추측은 내가 벡터 대신 행렬을 사용하고 나서 beside = True으로 설정해야한다는 것입니다.하지만이 작업을 수행 할 때 바가 균등하게 배치되어 있지 않습니다.

+0

어쩌면 틀 렸지만 전설에 너무 적은 색상이 있습니다. 벡터의 길이를 두 배로하면'legend.text = rev (rep (c ("1", "2", "3", "4", "5"), each = 2))' 더 이상 틀리게 보입니다. 그러나 전설은 당신이 기대하는 바가 아닙니다. –

답변

6

범례의 fill 옵션을 무시할 수 있습니다.

barplot(rev(data), horiz=TRUE, col = rev(colors), 
     names.arg = rev(names), 
     legend.text = rev(c("1","2","3","4","5")), 
     las=1, xlim = c(0,30), 
     args.legend = list(x ='bottomright', inset=c(0,0.05), 
          fill=unique(colors)) 
) 
관련 문제