Student Name: Senet Manandhar

Mini Class 2: Ordered Bar Chart

Data Received from CDC Website

Data on Invasive cancer in different states in which rates are 100,000 per person.

library(readxl)
Cancer_by_State <- read_excel("C:/Users/senet/Desktop/CSC 463 Data Visualization Tools/R/Mini Class 2/Cancer by State.xlsx")
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.3.3
ggplot(Cancer_by_State, aes(x=Location, y=Rate)) +
  geom_bar(stat = "identity",width = .5, fill="Red")+
  labs(title="Bar Chart", 
       subtitle="Location vs Avg. Rate", 
       caption="Source: cdc")+
  theme(axis.text.x= element_text(angle = 65, vjust = 0.6))

Ordered Bar Chart is a Bar Chart that is ordered by the Y axis variable. Just sorting the dataframe by the variable of interest isn’t enough to order the bar chart. In order for the bar chart to retain the order of the rows, the X axis variable (i.e. the categories) has to be converted into a factor.

colnames(Cancer_by_State) <-c("Location", "Rate")

Cancer_by_State<-Cancer_by_State[order(-Cancer_by_State$Rate),]

Cancer_by_State$Location <- factor(Cancer_by_State$Location, levels = Cancer_by_State$Location)
head(Cancer_by_State)
## # A tibble: 6 × 2
##       Location  Rate
##         <fctr> <dbl>
## 1     Kentucky 513.7
## 2     Delaware 488.1
## 3    Louisiana 478.7
## 4 Pennsylvania 477.3
## 5     New York 476.5
## 6        Maine 474.6
library(ggplot2)
theme_set(theme_bw())

ggplot(Cancer_by_State, aes(x=Location, y=Rate)) +
  geom_bar(stat = "identity",width = .5, fill="Red")+
  labs(title="Ordered Bar Chart", 
       subtitle="Location vs Avg. Rate", 
       caption="Source: cdc")+
  theme(axis.text.x= element_text(angle = 65, vjust = 0.6))