House of Representatives Demographics

Row

Breakdown of Party Majority

Row

Percentage of Votes to Win Congressional Seat

Distribution of Terms Served

Legislative Effectiveness Score

Row

Average bills passed per member

0.7435

Average Terms Served

5.2055

Average LES per year

1

Row

Bills the Became Laws by State

LES for the States with the Most and Least Laws Passed

Row

Bills Passed from 1973 to 2007

Histogram of LES

---
title: "Legislative Effectiveness for Lawmaking"
output: 
  flexdashboard::flex_dashboard:
    orientation: rows
    vertical_layout: fill
    social: menu
    source_code: embed
---

```{r setup, include=FALSE}
library(flexdashboard)
library(tidyverse)
library(plotly)
library(maps)
load("~/BUAN514.RData")
df_law <- week8_LES
colnames(df_law)[5]<-"Year"
colnames(df_law)[c(6,53)]<-c("State","LES")
colnames(df_law)[25]<-"Number_terms"
```

House of Representatives Demographics {data-icon="fa-archive"}
=======================================================================

Row
-----------------------------------------------------------------------


### Breakdown of Party Majority

```{r}
df_part<-df_law[c(5,8)]
colnames(df_part)<-c("Year","Party")

party<-rename(count(df_part,Year,Party),Freq=n)
Rep<-party[seq(1,nrow(party),3),]
Dem<-party[seq(2,nrow(party),3),]
thi<-party[seq(3,nrow(party),3),]

df_part1<-as.data.frame(cbind(Rep[,1],Rep[,3],Dem[,3],thi[,3]))
colnames(df_part1)<-c("Year","Republican","Democrat","Third Party")

df_new <- df_part1 %>% pivot_longer(cols=Republican:`Third Party`,
                                    names_to="Party",
                                    values_to="Freq")

col1<-c(Republican="#d92534",Democrat="#637aa6",`Third Party`="#d9ae30")

p1<-ggplot(df_new,aes(x=Year,y=Freq,group=Party,color=Party))+
  geom_line()+
  scale_color_manual(values=col1)+
  labs(color="Political Party",
       y="Number of Seats Held")

ggplotly(p1)
```


Row {.tabset}
-----------------------------------------------------------------------

### Percentage of Votes to Win Congressional Seat

```{r}
p2<-ggplot(df_law,aes(x=Percent.vote.received.to.enter.this.Congress))+
  geom_bar(fill="#637AA6")+
  labs(x="Percent of Votes Won",y="Count")

ggplotly(p2)
```

### Distribution of Terms Served

```{r}
p3<-ggplot(df_law,aes(x=Number_terms))+
  geom_bar(fill="#566D8C")+
  labs(x="Number of Terms",y="Count")

ggplotly(p3)
```


Legislative Effectiveness Score {data-icon="fa-archive"}
=======================================================================

Row
-----------------------------------------------------------------------


### Average bills passed per member

```{r}
max_bills <- round(mean(df_law$Total.bills.became.law),4)
valueBox(max_bills, icon ="fa-pencil")
```

### Average Terms Served

```{r}
df_na<-na.omit(df_law$Number_terms)
mean_term <- round(mean(df_na),4)
valueBox(mean_term,icon = "fa-eye")
```

### Average LES per year

```{r}
LES_mean <- round(mean(df_law$LES),4)
valueBox(LES_mean, icon = "fa-bell")
```

Row
-----------------------------------------------------------------------

### Bills the Became Laws by State

```{r}
df_state<-df_law[c(6,52)]
colnames(df_state)<-c("State","Laws")
df_state$Laws<- as.integer(df_state$Laws)

df_stfact<-data.frame(tapply(df_state$Laws,df_state$State,FUN=sum))
states <- rownames(df_stfact)
rownames(df_stfact) <- NULL
df_stfact<-cbind(states,df_stfact)
colnames(df_stfact)<-c("State","Laws")

df_stfact1<-df_stfact[-c(4,9,11,14,43,51),]
row.names(df_stfact1) <- NULL

state_abbs <- tibble(state = str_to_lower(state.name), abb = state.abb)

tbl_m <- left_join(df_stfact1, state_abbs, by = c("State" = "abb"))
tbl_m<-tbl_m[,-1]
tbl_m$region <- tolower(tbl_m$state)

state_map<-map_data("state")
map.df <- merge(state_map,tbl_m, by="region", all.x=T)
map.df <- map.df[order(map.df$order),]

p4<-ggplot(map.df,aes(x=long,y=lat,group=group,
                      text = paste("State:", region,"Laws:", Laws)))+
  geom_polygon(aes(fill=Laws))+
  geom_path()+
  scale_fill_gradientn(colours=rev(heat.colors(10)),na.value="grey90")+
  coord_map()+
  labs(x=" ",y=" ")

ggplotly(p4,tooltip = "text")
```

### LES for the States with the Most and Least Laws Passed

```{r}
df_ord<-df_stfact1%>%
  arrange(desc(Laws))

df_sub_up<-subset(df_law,State=="CA" | State=="NY" | State=="TX" |
               State=="IL" | State=="FL")
df_sub_up['val']="Most"

df_sub_dn<-subset(df_law,State=="DE" | State=="SD" | State=="ME" |
               State=="ND" | State=="VT")
df_sub_dn['val']="Least"

df_sub<-rbind(df_sub_up,df_sub_dn)

p5<-ggplot(df_sub, aes(factor(State), LES,col=val)) +
  geom_boxplot()+
  labs(x="State",y="LES",color="Laws Passed")+
  scale_color_manual(values=c("#f29f80","#566d8c"))+
  coord_flip()

 ggplotly(p5)
```


Row
-----------------------------------------------------------------------

### Bills Passed from 1973 to 2007

```{r}
yr_les<- df_law%>%
  group_by(Year)%>%
  summarise(Total.bills.became.law=sum(Total.bills.became.law))%>%
  `colnames<-`(c("Year","Bills Passed"))

p6<-ggplot(yr_les,aes(x=Year,y=`Bills Passed`))+
  geom_line(color="#566d8c")+
  labs(x="Year",y="Bills Passed")

ggplotly(p6)
```

### Histogram of LES

```{r}
p7<-ggplot(df_law,aes(LES))+
  geom_histogram(binwidth=0.25,fill="#637aa6")+
  labs(x="LES",y="Count")

ggplotly(p7)
```