-1

I'm working with a merged dataset that lists individual transactions from thousands of product numbers. Each of those product numbers falls into various product categories, and all sales are attributed to a specific order platform (online, TV, etc.)

I'd like to show a bar graph with order platform as the x axis, sales dollars (in thousands) as the y axis, and group the bars by product category.

If you take a look at the posted screenshot of my dashboard, I think I get close, however the product category groups don't seem to actually all group in the same spot, and instead spread themselves out.

enter image description here

If you have any advice, I would love to hear from you all, I will continue tinkering and post an update if I solve anything.

user20650
  • 24,654
  • 5
  • 56
  • 91
  • 3
    Welcome to stackoverflow (SO)! It's more likely that we will be able to help you if you make a minimal reproducible example to go along with your question. Something we can work from and use to show you how it might be possible to solve your problem. You can have a look at [this SO post](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on how to make a great reproducible example in R. Furthermore, it would also be helpful if you outline what you have already tried. – Eric Fail Jan 02 '16 at 16:52
  • Try `as.factor(ORDER_PLATFORM)`. Also, the chart seems to be more or less sowing what you need. It seems like there is no data for some of the categories, is that right? – Konrad Jan 02 '16 at 17:15
  • 1
    You probably have to aggregate your sales per category before you plot. But as others already wrote, it is hard to help you without a reproducible example... – Stibu Jan 02 '16 at 17:29
  • It could be that you have multiple records for each (product X order), and as you are using `stat=identity` you will get multipe bars of the same colour. You (perhaps) need to aggregate the data before plotting – user20650 Jan 02 '16 at 17:30

1 Answers1

0

It could be that you have multiple records for each (product X order), and as you are using stat=identity you will get multiple bars of the same colour. You (perhaps) need to aggregate the data before plotting as Stibu mentions in the comments.

Create an example with the mtcars dataset. Note that there are multiple y values for each x and grouping variable.

ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(gear))) + 
               geom_bar(stat="identity")

Which produces a plot similar to yours

enter image description here

You can get the total for each category by choosing a different statistic to use in the geom_bar call. So in this example calculate the total

ggplot(mtcars, aes(factor(cyl), mpg, fill=factor(gear))) +     
               geom_bar(stat="summary", fun.y=sum)

enter image description here

Check totals

aggregate(mpg ~ cyl + gear, mtcars, sum)

You could also do this by using the aggregated data in the ggplot call with stat=identity

ggplot(aggregate(mpg ~ cyl + gear, mtcars, sum), 
          aes(factor(cyl), mpg, fill=factor(gear))) +  
          geom_bar(stat="identity")
user20650
  • 24,654
  • 5
  • 56
  • 91