LionelBlair
LionelBlair

Reputation: 33

ggplot histogram showing extra grey bar

So I'm trying to get this histogram to display by the variable compage. Code I'm using is this:

hist.wrinko.age <- ggplot(ESSdata.oldage3, aes(wrinco2,fill=compage))
                        + theme(legend.position = "right")
                        + geom_bar(aes(y=..density..), binwidth=1, position="dodge")
                        + labs(x="WRINCO by age group", y = "Density")
                        + scale_x_continuous(breaks=seq(0, 10, 1)
                          )

It works OK, but the histogram looks like this:

histogram

Basically the legend shows 2 variables, but the histogram shows 3.

I've no idea where the third grey lines are coming from - is there something wrong with my code?

Upvotes: 3

Views: 882

Answers (1)

Gregor Thomas
Gregor Thomas

Reputation: 145965

Grey bars are added when there is data to plot (x and y values), but the fill variable is NA.

The easiest way to omit NA from your data is the na.omit function

ggplot(na.omit(ESSdata.oldage3), ...

Upvotes: 1

Related Questions