I have following type of data. But I would like to have graph with X-axis as Year and add all the sales added up in that year on Y- axis.

The Data frame name is salesdata. I have nearly 8000 rows of data. and the column headers are in the picture below.

enter image description here



2 Answers 2


Step 1: Changing dates to years

First you can change your column of dates (Order_Date) to simply years by using this formula:

> salesdata$Order_Date = format(salesdata$Order_Date, "%Y")

This will give your Order Dates just the year the sales was done in.

Step 2: Summarizing your sales data

After this you can create a vector with the summarizations of your sales data per year. This can be done using tapply:

> tapply(salesdata$Sales, salesdata$Order_Date, FUN=sum)

This will give you an overview of the total sales per year.

Step 3: Plotting your graph

You can use barplot() to plot your bar chart using the values you got in your tapply function.


The function as a oneliner:

> barplot(tapply(salesdata$Sales, format(salesdata$Order_Date, "%Y"), FUN=sum))

Of course you can edit your graph to your liking.

Example of the bar chart


Step 1: Summarizing

You can use the dplyr package for that purpose, as:

x %>% 
  group_by(Order_Date) %>% 
  summarise(Sales = sum(Sales))

OR, this approach will work as well,

x %>% 
  group_by(Order_Date) %>% 

Step 2: Plot

Once you're done with it, use ggplot to draw a plot of your interest, just use the respective function.

Additional Resources

For more details visit these blogs-

Hope it helps!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.