On 9/10/2012 11:43 PM, thomas kern wrote:
> Hi,
>
> is there a way to get legend entries into a legend produced by a stacked
> bar chart?
>
> data = data.frame(x=c(1,2,3,2,2))
> ggplot(data,aes(x=1,fill=factor(x))) + geom_bar(position="stack")
>
> how would I add 4, 5 and 6 to the legend?
The most straightforward way is to make x in data be a factor with all
the possible levels (1 through 6). Then you just need a
scale_fill_discrete call to include all levels.
data = data.frame(x=factor(c(1,2,3,2,2), levels=1:6))
ggplot(data,aes(x=1,fill=x)) +
geom_bar(position="stack") +
scale_fill_discrete(drop=FALSE)
> thank you very much,
> thomas
>
--
Brian S. Diggs, PhD
Senior Research Associate, Department of Surgery
Oregon Health & Science University