forked from DACSS/601_Fall_2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchallenge7.qmd
More file actions
101 lines (84 loc) · 2.14 KB
/
challenge7.qmd
File metadata and controls
101 lines (84 loc) · 2.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
---
title: "Challenge 7"
author: "Emily Duryea"
desription: "Challenge 7"
date: "12/20/2022"
format:
html:
toc: true
code-fold: true
code-copy: true
code-tools: true
categories:
- challenge_7
---
# Challenge 7
```{r}
#| label: setup
#| warning: false
#| message: false
library(tidyverse)
library(ggplot2)
knitr::opts_chunk$set(echo = TRUE, warning=FALSE, message=FALSE)
```
## Read in Data
```{r}
egg <- read.csv("_data/eggs_tidy.csv")
egg
```
This dataset shows egg sales by month/year, as well as the size (half dozen large, dozen large, half dozen extra large, dozen extra large).
## Tidying & Mutating the Data
```{r}
# Making a column to combine months and years
column = names(egg)
column <- column[!column %in% c("year","month")]
column
# Pivoting the data
egg <- egg %>%
pivot_longer(egg, cols=column, names_to = "carton_type", values_to = "sales")
newegg
```
## Visualization with Multiple Dimensions
```{r}
# Grouping by sales and year
egggroup <- egg %>%
group_by(year) %>%
summarise(
total_sales = sum(sales)
)
egggroup
# Creating a line plot of total sales and year
ggplot(egggroup, aes(x = year, y = total_sales)) +
geom_line(color = "black") +
theme_minimal() +
theme(
plot.background = element_rect(fill = "lightyellow"),
panel.grid = element_line(color = "grey")
)
egggroup2 <- egg %>%
group_by(year, carton_type) %>%
summarise(
total = sum(sales)
)
egggroup2
ggplot(egggroup2, aes(x=year, y=total, fill=carton_type)) +
geom_col(color="black", size=0.5) +
theme(text = element_text(family="Times")) +
geom_vline(xintercept=c(2010, 2015, 2020), color="blue", linetype="dashed", size=1) +
scale_fill_brewer(type="seq", palette="Reds")
ggplot(data=egggroup2, aes(x=year, y=total, color= carton_type)) +
geom_line() +
geom_point() +
labs(
x = "Year",
y = "Total Sales",
color = "Carton Type",
title = "Total Sales of Egg Carton Types Over the Years"
) +
guides(color = guide_legend(title="Carton Type")) +
theme_minimal() +
theme(
text = element_text(family="Times", size=12, color="black"),
panel.background = element_rect(fill="lightyellow")
)
```