1

My R data frame (electronics) look like this:

date                  manufacture    price
2019-11-22 11:30:04   Apple          600.00
2019-01-16 01:74:14   Samsung        877.00
2019-07-12 04:23:31   Apple          925.00
2019-07-12 01:21:54   Apple          1200.00
2019-01-16 04:48:34   Samsung        1100.00
2019-07-12 12:20:22   Apple          1450.00    
2019-03-28 06:23:11   Apple          1250.00

What I want to do are the following:

  • Take the sum of price for each given day (date) and create a chart to see the sale of each Manufacture by date
  • date is going to be on x-axis and line plot that has the price of manufacture
  • Basically I want to see the chart sum of sales overtime per the manufacture.

What did I do?

ggplot(electronics,
   aes(date,sum(price))) +
   geom_point() + 
   geom_smooth(method="lm") +
   facet_grid(manufacture)

However, it seems like I am not getting proper chart. Please kindly help me solve this. Thank you!

3
  • 1
    Aggregate your data before plotting. Commented Sep 30, 2019 at 17:38
  • 2
    you're missing a ~ in facet_grid(~manufacture) Commented Sep 30, 2019 at 17:42
  • @zx8754 is it not possible to do in real-time? How do I do aggregate by Date and price first? Commented Sep 30, 2019 at 18:48

1 Answer 1

1

I tried the following according to your data:

library(tidyverse)
electronics <- data.frame(date = c("2019-11-22 11:30:04",
"2019-01-16 01:74:14", "2019-07-12 04:23:31",
"2019-07-12 01:21:54", "2019-01-16 04:48:34",
"2019-07-12 12:20:22", "2019-03-28 06:23:11"), 
manufacture = c("Apple","Samsung","Apple","Apple",
"Samsung","Apple","Apple"), 
price = c(600,877,925,1200,1100,1450,1250))

electronics$date <- as.Date(electronics$date)

electronics %>% group_by(date,manufacture) %>% 
summarise(sum = sum(price)) %>% ggplot(aes(date,sum)) + 
geom_point() + geom_smooth(method="lm") + facet_grid(~manufacture)

As per your question and observation I have edited the code so now you can aggregate with "group_by", in this case by date and manufacture. After that, you summarise the sum of the prices.

enter image description here

Hope it helps. Regards,

Alexis

Sign up to request clarification or add additional context in comments.

2 Comments

I am supposed to take the sum of price and then group by date. I do not think you did the sum for each day.
Hello @floss, I have edited my answer, so now its grouped by date and manufacture, and summarised with the sum function. 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.