-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathColorado Marijuana Sales.Rmd
240 lines (172 loc) · 9.74 KB
/
Colorado Marijuana Sales.Rmd
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
---
title: "R Notebook"
output: html_notebook
---
## retrieve and prepare the previously cleaned csv file (Merged_County_Sales.csv)
```{r}
library(stringr)
library(dplyr)
library(formattable)
library(ggplot2)
#retrieve manually cleaned file
merged_county_sales <- read.csv("C:/Users/David/Desktop/MSDS696/Colorado MJ Datasets/Merged_County_Sales.csv", header=TRUE, colClasses = c("character", "character", "factor", "character", "character"))
#convert date field to R recognized date format
merged_county_sales$Date <- as.Date(merged_county_sales$Date)
#convert currency fields from factor to numeric
merged_county_sales$Med_County_Sales <- as.numeric(as.character(merged_county_sales$Med_County_Sales))
merged_county_sales$Rec_County_Sales <- as.numeric(as.character(merged_county_sales$Rec_County_Sales))
#convert currency fields from numeric to currency
merged_county_sales$Med_County_Sales <- currency(merged_county_sales$Med_County_Sales, symbol="$", big.mark=",", digits = 0L)
merged_county_sales$Rec_County_Sales <- currency(merged_county_sales$Rec_County_Sales, symbol="$", big.mark=",", digits = 0L)
summary(merged_county_sales)
summary(merged_county_sales$Rec_County_Sales)
options(scipen=10)
ggplot(data=merged_county_sales, mapping=aes(x=Date))+
geom_bar(mapping=aes(y=Rec_County_Sales/100000), stat="identity", position="dodge", fill="green")+
geom_bar(mapping=aes(y=Med_County_Sales/100000), stat="identity", position="dodge", fill="red")+
labs(title="Colorado Medical and Retail Marijuana Sales 2014-2018", x= "Years of Recreational Legalization", y= "Sales per $100K")+
theme_bw()
ggplot(data=merged_county_sales, aes(x=as.factor(merged_county_sales$County)))+
geom_boxplot(aes(y=Rec_County_Sales), color="green")+
geom_boxplot(aes(y=Med_County_Sales), color="red")+
labs(title="Colorado Retail and Medical Marijuana Sales since 2014", x= "Colorado County", y= "Sales")+
theme_bw()+
theme(axis.text.x = element_text(angle = 90, hjust = 1))
### investigate county sales by year
Year <- as.integer(paste(word(merged_county_sales$Date, 1, sep="-")))
merged_county_sales <- cbind(merged_county_sales, Year)
#x <- group_by(merged_county_sales, Year)
#x <- summarize(Year, Med_County_Sales = sum(Med_County_Sales))
#x
#write.csv(merged_county_sales, file = "merged_county_sales.csv")
```
```{r}
library(readr)
library(dplyr)
library(ggplot2)
library(ggrepel)
colopop_df <- as.data.frame(read.csv("CountyPopulation.xls", col.names=c("CountyFIPS", "Area", "Year", "Population"), colClasses = c("character", "factor", "factor", "numeric")))
state_pop <- filter(colopop_df, Area == "COLORADO STATE")
county_pop <- filter(colopop_df, Area != "COLORADO STATE")
summary(state_pop)
options(scipen=10)
#View(state_pop)
ggplot(data=state_pop, mapping=aes(x=Year, y=Population, group=Area))+
geom_line(color="red")+
geom_point(color="red")+
scale_y_continuous(limits = c(min(state_pop$Population), max(state_pop$Population)))+
geom_label_repel(aes(label = Population), nudge_x = 1)+
labs(title="Colorado Estimated Population Growth since 2010 Census")+
theme_bw()
```
```{r}
#retrieve manually cleaned file
#library(readr)
#library(dplyr)
#library(ggplot2)
#co_pop <- as.data.frame(read.csv("C:/Users/David/Desktop/MSDS696/Colorado Demographics/CountyRankings.csv", col.names=c("CountyFIPS", "County", "pop_2010", "pop_2011", "pop_2012", "pop_2013", "pop_2014", "pop_2015", "pop_2016", "pop_2017", "rank_2017"), colClasses = c("character", "factor", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "numeric", "integer")))
#state_pop <- co_pop[1, -1]
#county_pop <- co_pop[-1, -1]
#reorder by city population rank (2017)
#county_pop <- county_pop[order(county_pop$rank_2017), ]
#county_pop <- county_pop[1:20, ]
#county_pop.f1 <- filter(county_pop, pop_2010 >= 100000)
#county_pop.f2 <- filter(county_pop, pop_2010 < 100000 & pop_2010 > 25000)
#county_pop.f3 <- filter(county_pop, pop_2010 < 25000)
#rm(co_pop)
#View(county_pop)
#transpose_df <- as.data.frame(t(county_pop))
#View(county_pop)
#options(scipen=10)
#ggplot(data=county_pop.f1, mapping=aes(x=County))+
#geom_col(mapping=aes(y=pop_2017, fill="2017"), position = "dodge")+
#geom_col(mapping=aes(y=pop_2014, fill="2014"), position = "dodge")+
#geom_col(mapping=aes(y=pop_2010, fill="2010"), position = "dodge")+
#labs(title="", x= "Colorado County", y= "Sales per $1M")+
#theme_bw()+
#theme(axis.text.x = element_text(angle = 90, hjust = 1))
#ggplot(data=county_pop.f2, mapping=aes(x=County))+
#geom_col(mapping=aes(y=pop_2017, fill="2017"), position = "dodge")+
#geom_col(mapping=aes(y=pop_2014, fill="2014"), position = "dodge")+
#geom_col(mapping=aes(y=pop_2010, fill="2010"), position = "dodge")+
#labs(title="", x= "Colorado County", y= "Sales per $1M")+
#theme_bw()+
#theme(axis.text.x = element_text(angle = 90, hjust = 1))
#ggplot(data=county_pop.f3, mapping=aes(x=County))+
#geom_col(mapping=aes(y=pop_2017, fill="2017"), position = "dodge")+
#geom_col(mapping=aes(y=pop_2014, fill="2014"), position = "dodge")+
#geom_col(mapping=aes(y=pop_2010, fill="2010"), position = "dodge")+
#labs(title="", x= "Colorado County", y= "Sales per $1M")+
#theme_bw()+
#theme(axis.text.x = element_text(angle = 90, hjust = 1))
```
```{r}
library(ggrepel)
library(ggplot2)
co_sin_tax <- as.data.frame(read.csv("colorado_sin_revenue.csv"))
summary(co_sin_tax)
options(scipen=10)
#create a dataframe with the sin tax and co population data. the data will need to be normalized.
df <- co_sin_tax
df <- merge(co_sin_tax, state_pop)
df$med_marijuana_tax <- currency(df$med_marijuana_tax, symbol="$", big.mark=",", digits = 0L)
df$rec_marijuana_tax <- currency(df$rec_marijuana_tax, symbol="$", big.mark=",", digits = 0L)
df$alcohol_tax <- currency(df$alcohol_tax, symbol="$", big.mark=",", digits = 0L)
df$tobacco_tax <- currency(df$tobacco_tax, symbol="$", big.mark=",", digits = 0L)
df$gambling <- currency(df$gambling, symbol="$", big.mark=",", digits = 0L)
ggplot(data=df, mapping=aes(x = Year)) +
geom_point(aes(y=rec_marijuana_tax/100000, color="Recreational Marijuana"))+
geom_line(aes(y=rec_marijuana_tax/100000, color="Recreational Marijuana"), size=2)+
geom_point(aes(y=alcohol_tax/100000, color="Alcohol"))+
geom_line(aes(y=alcohol_tax/100000, color="Alcohol"))+
geom_point(aes(y=tobacco_tax/100000, color="Tobacco"))+
geom_line(aes(y=tobacco_tax/100000, color="Tobacco"))+
geom_point(aes(y=gambling/100000, color="Gambling"))+
geom_line(aes(y=gambling/100000, color="Gambling"))+
geom_label_repel(aes(y=rec_marijuana_tax/100000, label=rec_marijuana_tax), nudge_x = 0, nudge_y = -200)+
scale_colour_manual("",
breaks = c("Medical Marijuana", "Recreational Marijuana", "Alcohol", "Tobacco", "Gambling"),
values = c("Medical Marijuana"="green", "Recreational Marijuana"="red",
"Alcohol"="purple", "Tobacco"="blue", "Gambling"="orange"))+
labs(title="Comparisons of Colorado 'sin' Taxes 2013-2017", x= "Year", y= "Colorado Marijuana Revenue per $100K")+
theme_bw()+
theme(legend.position="top")
###perform correlation analysis - I don't think really tells me anything of value.
#scale the data
#df_year <- df[1]
#df_scaled <- df[c(2:6,9)]
#df_scaled <- cbind(df_year, df_scaled)
#library(corrplot)
#m <- cor(df_scaled[-1, 2:6])
#corrplot(m, type = "lower", order = "hclust", method="number")
View(df)
```
```{r}
library(stringr)
library(dplyr)
library(formattable)
library(ggplot2)
merged_county_sales2 <- read.csv("C:/Users/David/Desktop/MSDS696/Colorado MJ Datasets/Merged_County_Sales_v2.csv", header=TRUE)
#merged_county_sales2 <- as.data.frame(read.csv("C:/Users/David/Desktop/MSDS696/Colorado MJ Datasets/Merged_County_Sales_v2.csv", header=TRUE, colClasses = c("character", "character", "factor", "numeric", "numeric", "integer", "numeric", "numeric", "numeric")))
merged_county_sales2$Med_County_Sales <- currency(merged_county_sales2$Med_County_Sales, symbol="$", big.mark=",", digits = 0L)
merged_county_sales2$Rec_County_Sales <- currency(merged_county_sales2$Rec_County_Sales, symbol="$", big.mark=",", digits = 0L)
summary(merged_county_sales2)
ggplot(data=merged_county_sales2, mapping=aes(x=Year))+
geom_col(mapping=aes(y=Rec_County_Sales/100000), fill="green")+
geom_col(mapping=aes(y=Med_County_Sales/100000), fill="red", alpha=.9)+
labs(title="Colorado Medical and Retail Marijuana Sales 2014-2018", x= "Years of Recreational Legalization", y= "Sales per $100K")+
theme_bw()
#test <- merged_county_sales2[order(merged_county_sales2$Rec_County_Sales, merged_county_sales2$County),]
ggplot(data=merged_county_sales2, aes(x=County))+
geom_boxplot(aes(y=Rec_County_Sales/100000), color="green")+
geom_boxplot(aes(y=Med_County_Sales/100000), color="red")+
labs(title="Colorado Retail and Medical Marijuana Sales since 2014", x= "Colorado County", y= "Sales")+
theme_bw()+
theme(axis.text.x = element_text(angle = 90))
ggplot(data=merged_county_sales2, mapping=aes(x = Year)) +
geom_col(aes(y=County_Population, color="County_Population"))
View(merged_county_sales2)
```
Add a new chunk by clicking the *Insert Chunk* button on the toolbar or by pressing *Ctrl+Alt+I*.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the *Preview* button or press *Ctrl+Shift+K* to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike *Knit*, *Preview* does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.