Import Your Data

In the following code hunk, import your data.

#### Use read_csv() or another function

#### Make sure your data is converted into a tibble. 

dat<- read.csv("Electric_Vehicle_Population_Data.csv")
dat <- as_tibble(dat)
view(dat)

Part 1

The resulting bar chart illustrates the distribution of electric vehicle types in Washington state, focusing on the specified brands(the Top 10 of the most used vehicle brands). Each bar represents the count of electric vehicles of a particular type, and different colors within each bar indicate the contribution of various brands to that type.

# Vehicle Type Distribution
dat1 <- dat %>% select(Electric.Vehicle.Type,State,Make) %>% filter(State=='WA')%>% filter(Make=='TESLA'|Make=='NISSAN'|Make=='CHEVROLET'|Make=='FORD'|Make=='BMW'|Make=='KIA'|Make=='TOYOTA'|Make=='VOLKSWAGEN'|Make=='JEEP'|Make=='HYUNDAI')

ggplot(dat1, aes(x = `Electric.Vehicle.Type`, fill = `Make`)) +
  geom_bar() +
  ggtitle("Vehicle Type Distribution in WA") +
  xlab("Electric Vehicle Type") +
  ylab("Count") 

Part 2

The resulting stacked bar chart visually represents the contribution of different manufacturers (the Top 10 of the most used vehicle brands) to the total count of electric vehicles over various years. Each stack in a particular year represents the count of electric vehicles for a specific brand, and the height of the stack corresponds to the total count.

dat2<-dat %>% select(Model.Year,Make) %>% filter(Make=='TESLA'|Make=='NISSAN'|Make=='CHEVROLET'|Make=='FORD'|Make=='BMW'|Make=='KIA'|Make=='TOYOTA'|Make=='VOLKSWAGEN'|Make=='JEEP'|Make=='HYUNDAI')

# Manufacturer Contribution Over Time
ggplot(dat2, aes(x = `Model.Year`, fill = Make)) +
  geom_bar(position = "stack") +
  ggtitle("Manufacturer Contribution Over Time") +
  xlab("Year") +
  ylab("Count") +
  theme_minimal() +
  theme(legend.position = "right")

Part 3

The resulting bar chart visually represents the count of Clean Alternative Fuel Vehicles’ eligibility status over different years. Each bar corresponds to a specific year, and the bars are colored based on whether the vehicles are eligible, not eligible, or if the eligibility is unknown.

dat3<-dat %>% drop_na() %>% select(Model.Year,Clean.Alternative.Fuel.Vehicle..CAFV..Eligibility)

CAFV<-recode(dat3$Clean.Alternative.Fuel.Vehicle..CAFV..Eligibility,'Clean Alternative Fuel Vehicle Eligible'='Eligible','Eligibility unknown as battery range has not been researched'='Unknown','Not eligible due to low battery range'='Not eligible')
dat3 <- add_column(dat3,CAFV)

ggplot(dat3, aes(x=Model.Year,fill=CAFV))+
  geom_bar()+scale_fill_manual(values=c("green","red","grey"))+
  labs(x="Year",y="Count",title = "Clean Alternative Fuel Vehicle Eligibility Count")+
  facet_wrap(~CAFV)

Part 4

Relationship Betwwen Electric Range and Lowest Manufacturer’s Suggested Retail Price

library(ggrepel)
## Warning: package 'ggrepel' was built under R version 4.0.5
dat4 <- dat %>% drop_na() %>% select(Electric.Range,Base.MSRP,Make)

####jitter adds random noise to the data to avoid overplotting
ggplot(dat4,aes(x=Electric.Range,y=Base.MSRP,color=Make))+
  geom_jitter()+
  labs(x="Electric Range",y="Lowest MSRP",title="Relationship Betwwen Electric Range and Lowest Manufacturer's Suggested Retail Price ")+
  geom_text(data = filter(dat4, Base.MSRP > 100000), aes(label = Make), nudge_y = 1000)+
  theme(legend.position="bottom")

Part 5

Number of Vehicles in Washington

dat5<-dat %>% drop_na() %>% select(City) 
car_number<- table(dat5) %>% as_tibble() %>% filter(n>1000)

ggplot(car_number,aes(x=n,y=reorder(dat5,n)))+
  geom_point(size=5)+
  theme(panel.grid.major.x=element_blank(),
        panel.grid.minor.x=element_blank(),
        panel.grid.major.y=element_line(linetype="dashed",color="blue"))+
  labs(x="Number of Vehicles",y="",title = "Number of Vehicles in Washington")

Part 6

Number of Vehicles Eligible for Clean Alternative Fuel in Washington

dat6<-dat %>% drop_na() %>% select(City,Clean.Alternative.Fuel.Vehicle..CAFV..Eligibility) %>% filter(Clean.Alternative.Fuel.Vehicle..CAFV..Eligibility == "Clean Alternative Fuel Vehicle Eligible")
CAFV_Eligibility <- table(dat6) %>% as_tibble() %>% filter(n>500)
view(CAFV_Eligibility)

ggplot(CAFV_Eligibility,aes(x=n,y=reorder(City,n)))+
  geom_point(size=5,pch=20)+
  theme(panel.grid.major.x=element_blank(),
        panel.grid.minor.x=element_blank(),
        panel.grid.major.y=element_line(linetype="dashed",color="green"))+
  labs(x="Number of Vehicles Eligible for Clean Alternative Fuel",y="",title = "Number of Vehicles Eligible in Washington")

Part 7

Distribution of The Model Year Of The Vehicle In Washington

dat7 <- dat %>% drop_na() %>% select(Model.Year)

ggplot(dat7,aes(x=Model.Year))+
  geom_histogram(bins = 60)+
  labs(x="Model Year Of The Vehicle",y="Count",title="Distribution of The Model Year Of The Vehicle In Washington")

Part 8

Electric Range of Different Model Year(1997-2024)

library(gganimate)
library(gifski)
## Warning: package 'gifski' was built under R version 4.0.5
library(transformr)
## Warning: package 'transformr' was built under R version 4.0.5
dat8 <- dat %>% drop_na() %>% select(Electric.Vehicle.Type,Electric.Range,Model.Year)
dat8$Model.Year <- as.factor(dat8$Model.Year)

###facetted box plot
ggplot(dat8, aes(x=factor(Electric.Vehicle.Type), y=Electric.Range)) +
  geom_boxplot()+
  facet_wrap(~Model.Year)

my_anim<- ggplot(dat8, aes(x=factor(Electric.Vehicle.Type), y=Electric.Range)) +
  geom_boxplot() +
  transition_states(Model.Year)+
   labs( title = 'Year: {closest_state}',x = 'Electric Vehicle Type', y = 'Electric Range')

my_anim