Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 530
LionelBlair Avatar asked May 28 '26 17:05

LionelBlair


1 Answers

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), ...
like image 60
Gregor Thomas Avatar answered May 30 '26 09:05

Gregor Thomas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!