---
title: "Dashboard"
output:
flexdashboard::flex_dashboard:
orientation: columns
vertical_layout: fill
source: embed
---
```{r setup, include=FALSE}
library(tidyverse)
library(dplyr)
library(usmap)
library(ggplot2)
require(maps)
library(plotly)
library(flexdashboard)
```
Column {data-width=650}
-----------------------------------------------------------------------
### Chart A: Where marijuana is legal in the United States?
```{r}
legal_state = read_csv("data/legalization_state.csv")
legal_state = legal_state %>%
rename(full = State)
legal_state = legal_state %>%
mutate(status = case_when(
Recreational == "Yes" & Medical == "Yes" ~ "Recreational/Medical", #rec/med
Recreational == "No" & Medical == "Yes" ~ "Medical", #medical legal
Recreational == "No" & Medical == "No" ~ "Illegal"#none
))
data(statepop)
mapping_df <-inner_join(statepop, legal_state, by = "full")
mapping_df <- mapping_df %>%
select(-pop_2015) %>%
mutate(status = factor(status))
labels = usmapdata::centroid_labels("states")
state_labels <- merge(mapping_df, labels, by = "fips")
map = plot_usmap(regions = "states", data=mapping_df, values = "status",color = "white", labels = TRUE)+
theme(panel.background = element_rect(colour = "black")) +
scale_fill_manual(values = c(`Recreational/Medical` = "red", `Medical` = "blue", `Illegal` = "grey" ), name = "Legalized states") +
theme(legend.position = "bottom") +
labs(title = "Map: Legalized status for marijuana in the United States") +
theme(plot.caption = element_text(hjust = 0, face= "italic"))+
theme(plot.title = element_text(hjust = 0.5))
map$layers[[2]]$aes_params$size <- 3
ggplotly(map) %>%
highlight("plotly_hover", selected = attrs_selected(line = list(color = "black"))) %>%
layout(legend = list(orientation = "h", x = 0.5, y = -0.2))
```
### Chart B: When marijuana legalized in each State?
```{r}
year_df = mapping_df %>%
select(full,Year_legalized_REC,Year_legalized_MED) %>%
pivot_longer(
Year_legalized_REC:Year_legalized_MED,
names_to = "rec_med",
values_to = "year")
legal_year <- year_df %>%
plot_ly(x = ~ year, y = ~full, color = ~rec_med, type = "scatter", mode = "markers") %>%
layout(title = "Legalized year of each state",
yaxis = list(title = "States", tickfont = list(size = 5)), xaxis = list(title = "Legalized year")) %>%
layout(legend = list(orientation = "h", x = 0.5, y = -0.2))
legal_year
```
Column {data-width=350}
-----------------------------------------------------------------------
### Chart C: Legalization data
```{r}
legalized_states <- legal_state %>%
select(-Recreational, -Medical) %>%
knitr::kable(col.names = c("State", "Legalization Year for Recreational", "Legalization Year for Medical",
"Legalized status"))
legalized_states
```