Reputation: 3763
I'm trying to create a barplot using R, but the group coloring in the legend looks wrong.
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))
)
I can imagine what's causing this. My initial guess is that I should've used a matrix instead of a vector and then set beside = True
, but when I do this the bars aren't evenly spaced.
Upvotes: 3
Views: 282
Reputation: 6750
You can override the fill
option of legend.
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))
)
Upvotes: 6