Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display 0 value in a bar chart using ggplot2

Tags:

r

colors

ggplot2

I have this data frame called data:

head(data)

     date         total_sold purchasability visibility
81 2014-05-01          3              3          3
82 2014-05-02          2              2          3
83 2014-05-03          1              2          3
84 2014-05-04          1              3          3
85 2014-05-05          3              2          3
86 2014-05-06          0              0          3

And I would like to do a bar chart with x = date and y = total_sold with a color depending on the purchasability. I this ggplot2 to do that :

bar <- ggplot(data = data, aes(x = date, fill=as.factor(purchasability),y = total_sold)) + geom_bar(stat = 'identity')

The output is very nice but the problem is that where total_sold = 0 there is not chart and thus no way to know the purchasability. Is it possible to still display a bar (maybe from 0.5 to -0.5) when total_sold = 0 ?

Thanks

like image 738
user2741700 Avatar asked Jan 28 '26 22:01

user2741700


1 Answers

You can just use geom bar, please look this code

df <- data.frame(time = factor(c("Lunch","Dinner","breakfast","test"), levels=c("Lunch","Dinner","breakfast","test")),
                  total_bill = c(14.89, 0,0.5,-0.5))
# Add a black outline
ggplot(data=df, aes(x=time, y=total_bill, fill=time)) + geom_bar(colour="black", stat="identity")

enter image description here

like image 130
rischan Avatar answered Jan 31 '26 12:01

rischan



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!