0

I want to create sales report in R using ggplot2. I want dates in x-axis and $ sales in y-axis and I also want to show promotions we did during the whole sale period. So we can Identify which promotions did well. I was able to do it thru geom_point and use color=Promotions but some of my promotions are overlapping and don't know how to tackle that. What is the best way I can achieve this.

I am open to use any other graph like bar or histogram if that can help me achieve this scenario.

Here's the code I use.

ggplot(data = sales)+
 geom_point(mapping = aes(x = Date, y = Total_Sales, color = Promotions, size = Total_Sales))+ 
 theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

Thank you!

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Welcome to SO! It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. – stefan May 26 '22 at 21:10

1 Answers1

0

Going by the description of your data, it seems your have three variables, Date, Total_Sales, and Promotions and plotting colored dots, where the color indicates the amount of promotions, may make it difficult to see the relationship between sales/promotions. You could make a bar/line graph (code included), but this presents the problem of scale, the number of promotions may be single digits, and the amount of sales in the thousands. I just included the code to demonstrate how to do it, but it's not a good idea to do it this way.

It will be better to use proportions (percentages) to see the relationship between sales/promotions.

EDITED

Since your Promotions variable is discrete and your Total_Sales do not distinguish between promotions, then you can count the number of promotions and see the association between number of promotions and sales.

library(tidyverse)

sales %>% 
  group_by(Date, Total_Orders, Total_Sales) %>% 
  summarise(n_events = n()) %>% 
ggplot(aes(x = factor(Date), y = Total_Sales, fill = n_events)) +
  geom_bar(stat = "identity") +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
guasi
  • 1,461
  • 3
  • 12
  • Thanks for your input and it is very helpful. My Promotions column is not numeric datatype. [Sale-test1.csv](https://designsbyshs-my.sharepoint.com/:x:/p/nahmed/EdYN-_KUeAFOpnay9mPw8kUBwOPscocfGmrMzsm2fraAKw?e=BmrOYT) Also how can i change the date column to proper date m-d-y ? When I use my code it is changing years to 2020 – Nabeegh Ahmed May 27 '22 at 17:52
  • `sales$Date <- strptime(as.character(sales$Date), "%m/%d/%y")` `sales$Date <- as.Date(sales$Date, "%m/%d/%y")` – Nabeegh Ahmed May 27 '22 at 17:56
  • Take a look at my edits...it's even easier given `Promotions` is discrete – guasi May 28 '22 at 00:31
  • Thanks for the update but now the issue is that sales are summed for each promotion. For example, on 10/31 we have a sales of 20830.2122 but on graph it summed to 40000. – Nabeegh Ahmed May 31 '22 at 13:55
  • I see the problem. For the code above to work, sales have to be distinguished between promotions, and your data does not do that. You count count how many promotions were going on, but from your data you cannot get relationship between kind of promotion and amount of sales, the data does not have that info. – guasi May 31 '22 at 16:52