Import Your Data

In the following code hunk, import your data.

data1 <- drop_na(read.csv("C:/shiny/rsconnect/f01.csv"))
data2 <- drop_na(read.csv("C:/shiny/rsconnect/f02.csv"))
data3 <- drop_na(read.csv("C:/shiny/rsconnect/f03.csv"))
data4 <- drop_na(read.csv("C:/shiny/rsconnect/f04.csv"))
data5 <- drop_na(read.csv("C:/shiny/rsconnect/f05.csv"))
data6 <- drop_na(read.csv("C:/shiny/rsconnect/f06.csv"))

Part 1

For my first figure, I am going to create a lineplot that plots year on the x axis and rate of nonfatal violent victimization per 1,000 people on the y axis.And the plot has three lines which are grouped by different ages(12 to 17,18 to 24,25 or older).I will create a three column tibble with these data.

data_long <- melt(data1, id.vars = "Year")

data_long <- data_long %>%
  mutate(value = as.numeric(as.character(value)))

data_long
##    Year     variable value
## 1  1993    X12.to.17 184.8
## 2  1994    X12.to.17 189.3
## 3  1995    X12.to.17 156.3
## 4  1996    X12.to.17 146.8
## 5  1997    X12.to.17 135.5
## 6  1998    X12.to.17 119.1
## 7  1999    X12.to.17 108.7
## 8  2000    X12.to.17  73.2
## 9  2001    X12.to.17  74.4
## 10 2002    X12.to.17  62.7
## 11 2003    X12.to.17  78.3
## 12 2004    X12.to.17  49.7
## 13 2005    X12.to.17  59.8
## 14 2006    X12.to.17   0.0
## 15 2007    X12.to.17  61.8
## 16 2008    X12.to.17  54.3
## 17 2009    X12.to.17  43.0
## 18 2010    X12.to.17  28.1
## 19 2011    X12.to.17  37.7
## 20 2012    X12.to.17  48.4
## 21 2013    X12.to.17  52.1
## 22 2014    X12.to.17  30.1
## 23 2015    X12.to.17  31.3
## 24 2016    X12.to.17  25.1
## 25 2017    X12.to.17  33.5
## 26 2018    X12.to.17  34.2
## 27 2019    X12.to.17  35.2
## 28 2020    X12.to.17  17.4
## 29 2021    X12.to.17  13.2
## 30 2022    X12.to.17  27.4
## 31 1993    X18.to.24 141.6
## 32 1994    X18.to.24 144.2
## 33 1995    X18.to.24 135.4
## 34 1996    X18.to.24 122.7
## 35 1997    X18.to.24 110.0
## 36 1998    X18.to.24 112.2
## 37 1999    X18.to.24  88.9
## 38 2000    X18.to.24  75.0
## 39 2001    X18.to.24  59.2
## 40 2002    X18.to.24  68.5
## 41 2003    X18.to.24  63.8
## 42 2004    X18.to.24  55.4
## 43 2005    X18.to.24  61.0
## 44 2006    X18.to.24   0.0
## 45 2007    X18.to.24  46.6
## 46 2008    X18.to.24  43.2
## 47 2009    X18.to.24  38.5
## 48 2010    X18.to.24  33.9
## 49 2011    X18.to.24  49.1
## 50 2012    X18.to.24  41.0
## 51 2013    X18.to.24  33.8
## 52 2014    X18.to.24  26.8
## 53 2015    X18.to.24  25.1
## 54 2016    X18.to.24  29.6
## 55 2017    X18.to.24  34.7
## 56 2018    X18.to.24  35.9
## 57 2019    X18.to.24  37.2
## 58 2020    X18.to.24  29.6
## 59 2021    X18.to.24  23.1
## 60 2022    X18.to.24  36.8
## 61 1993 X25.or.older  56.0
## 62 1994 X25.or.older  55.3
## 63 1995 X25.or.older  49.3
## 64 1996 X25.or.older  45.0
## 65 1997 X25.or.older  43.7
## 66 1998 X25.or.older  36.7
## 67 1999 X25.or.older  32.6
## 68 2000 X25.or.older  26.9
## 69 2001 X25.or.older  22.9
## 70 2002 X25.or.older  22.3
## 71 2003 X25.or.older  21.1
## 72 2004 X25.or.older  20.7
## 73 2005 X25.or.older  19.4
## 74 2006 X25.or.older   0.0
## 75 2007 X25.or.older  19.8
## 76 2008 X25.or.older  19.1
## 77 2009 X25.or.older  17.4
## 78 2010 X25.or.older  16.0
## 79 2011 X25.or.older  16.9
## 80 2012 X25.or.older  21.2
## 81 2013 X25.or.older  18.1
## 82 2014 X25.or.older  17.9
## 83 2015 X25.or.older  16.2
## 84 2016 X25.or.older  17.7
## 85 2017 X25.or.older  17.2
## 86 2018 X25.or.older  20.2
## 87 2019 X25.or.older  17.2
## 88 2020 X25.or.older  14.6
## 89 2021 X25.or.older  16.0
## 90 2022 X25.or.older  21.3
data_long %>% 
  ggplot(aes(x = Year, y = value, group = variable, color = variable)) +
  geom_line(size = 2) +
  theme(legend.position="top") +
  theme(panel.grid.major.x = element_line(colour = "gray", linetype = "dotted"))+
  scale_y_continuous(breaks = seq(0, 200, by = 20))+
  ggtitle("Rate of nonfatal violent victimization(by age of victim) from 1993 to 2022")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

Part 2

For my second figure, I am going to create a histogram that includes five sets which means five different years(2018-2022),and each year has three bars which group by age of victim(12 to 17,18 to 24,25 or older).The xlab is year and the ylab is rate of nonfatal violent victimization per 1,000 persons.I will create a three column tibble with these data.

data_long2 <- data2 %>%
  pivot_longer(cols = -Year, names_to = "Age_Group", values_to = "Rate")


data_long2$Age_Group <- recode(data_long2$Age_Group,
                               `X12.to.17` = "12 to 17",
                               `X18.to.24` = "18 to 24",
                               `X25.or.older` = "25 or older")
data_long2
## # A tibble: 15 × 3
##     Year Age_Group    Rate
##    <int> <chr>       <dbl>
##  1  2018 12 to 17     34.2
##  2  2018 18 to 24     35.9
##  3  2018 25 or older  20.2
##  4  2019 12 to 17     35.2
##  5  2019 18 to 24     37.2
##  6  2019 25 or older  17.2
##  7  2020 12 to 17     17.4
##  8  2020 18 to 24     29.6
##  9  2020 25 or older  14.6
## 10  2021 12 to 17     13.2
## 11  2021 18 to 24     23.1
## 12  2021 25 or older  16  
## 13  2022 12 to 17     27.4
## 14  2022 18 to 24     36.8
## 15  2022 25 or older  21.3
ggplot(data_long2, aes(x = Year, y = Rate, fill = Age_Group)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  scale_fill_manual(values = c("12 to 17" = "pink2", "18 to 24" = "plum", "25 or older" = "#A6CEE3")) +
  labs(y = "Rate per 1,000 persons", fill = "Age Group") +
  theme_minimal() +
  theme(legend.position = "top")+
  ggtitle("Rate of nonfatal violent victimization(by age of victim) from 2018 to 2022")

Part 3

For my third figure, I am going to create a accumulated area chart that plots year on the x axis and number of homicides on the y axis.And different color means different group of age.I will create a three column tibble with these data.

data_long3 <- data3 %>%
  pivot_longer(cols = -Year, 
               names_to = "Age_Group", values_to = "Count")

data_long3$Age_Group <- recode(data_long3$Age_Group,
                               `X11.or.younger` = "11 or younger",
                               `X12.to.17` = "12 to 17",
                               `X18.or.older` = "18 or older")
data_long3
## # A tibble: 90 × 3
##     Year Age_Group     Count
##    <int> <chr>         <dbl>
##  1  1993 11 or younger  1034
##  2  1993 12 to 17       1852
##  3  1993 18 or older   21641
##  4  1994 11 or younger   964
##  5  1994 12 to 17       1735
##  6  1994 18 or older   20628
##  7  1995 11 or younger   913
##  8  1995 12 to 17       1762
##  9  1995 18 or older   18931
## 10  1996 11 or younger   959
## # ℹ 80 more rows
ggplot(data_long3, aes(x = Year, y = Count, fill = Age_Group)) +
  geom_area() +
  scale_fill_brewer(palette = "Purples", breaks = rev(levels(data_long3$Age_Group)))+
  labs(title = "Number of homicides(by age of victim) from 1993 to 2022",
       x = "Year", y = "Number",
       fill = "Age Group") +
  theme_minimal()+
  ggtitle("Number of homicides(by age of victim) from 1993 to 2022")

Part 4

For my fourth figure, I am going to create a histogram that includes five sets which means five different years(2018-2022),and each year has three bars which group by age of victim(11 or younger,12 to 17,18 or older).The xlab is year and the ylabis Homicide rate per 100,000 persons.I will create a three column tibble with these data.

data_long4 <- data4 %>%
  pivot_longer(cols = -Year, names_to = "Age_Group", values_to = "Rate")


data_long4$Age_Group <- recode(data_long4$Age_Group,
                               `X11.or.younger` = "11 or younger",
                               `X12.to.17` = "12 to 17",
                               `X18.or.older` = "18 or older")
data_long4
## # A tibble: 15 × 3
##     Year Age_Group      Rate
##    <int> <chr>         <dbl>
##  1  2018 11 or younger  1.31
##  2  2018 12 to 17       2.87
##  3  2018 18 or older    5.93
##  4  2019 11 or younger  1.39
##  5  2019 12 to 17       2.89
##  6  2019 18 or older    5.99
##  7  2020 11 or younger  1.48
##  8  2020 12 to 17       4.41
##  9  2020 18 or older    7.7 
## 10  2021 11 or younger  1.55
## 11  2021 12 to 17       4.96
## 12  2021 18 or older    8.17
## 13  2022 11 or younger  1.45
## 14  2022 12 to 17       5.4 
## 15  2022 18 or older    7.54
ggplot(data_long4, aes(x = Year, y = Rate, fill = Age_Group)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  scale_fill_manual(values = c("11 or younger" = "pink2", "12 to 17" = "plum", "18 or older" = "#A6CEE3")) +
  labs(y = "Rate per 100,000 persons", fill = "Age Group") +
  theme_minimal() +
  theme(legend.position = "top")+
  ggtitle("Homicide rate per 100,000 persons(by age of victim) from 2018 to 2022")

Part 5

For my fifth figure, I am going to create a histogram that includes two sets which means two different years(2021 and 2022),and each year has five bars which group by age of victim(11 or younger,12 to 17,18 to 29,30 or older,Multiple offenders of various ages).The xlab is year and the ylab is Percent of nonfatal violent incidents.I will create a three column tibble with these data.

data_long5 <- data5 %>%
  pivot_longer(cols = c("X2021", "X2022"), names_to = "Year", values_to = "Percent")

data_long5
## # A tibble: 10 × 3
##    Age_Group                          Year  Percent
##    <chr>                              <chr>   <dbl>
##  1 11 or younger                      X2021     1.4
##  2 11 or younger                      X2022     1.2
##  3 12 to 17                           X2021     7.7
##  4 12 to 17                           X2022     9.3
##  5 18 to 29                           X2021    25  
##  6 18 to 29                           X2022    23.9
##  7 30 or older                        X2021    60.3
##  8 30 or older                        X2022    59.7
##  9 Multiple offenders of various ages X2021     5.7
## 10 Multiple offenders of various ages X2022     5.8
ggplot(data_long5, aes(x = Year, y = Percent, fill = Age_Group)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8), width = 0.7) +
  scale_fill_manual(values = c("pink", "pink3", "plum", "plum4", "#A6CEE3")) +
  labs(title = "Percent of nonfatal violent incidents(by age of offender) in 2021 and 2022",
       x = "Year", y = "Percent",
       fill = "Age Group") +
  theme_minimal() +
  theme(legend.position = "top")

Part 6

For my sixth figure, I am going to create a bar chart that includes twelve sets which means different Offense type,and each type has two bars which means different year(2021 and 2022).The xlab is different offense type and the ylab is Percent of all arrests that were of persons age 17 or younger.I will create a three column tibble with these data.

data_long6 <- data6 %>%
  pivot_longer(cols = c("X2021", "X2022"), names_to = "Year", values_to = "Percent")

data_long6
## # A tibble: 26 × 3
##    Offense.type       Year  Percent
##    <chr>              <chr>   <dbl>
##  1 Violent crime      X2021     8.7
##  2 Violent crime      X2022     9.9
##  3 Homicide           X2021     8.9
##  4 Homicide           X2022    11  
##  5 Rape               X2021    15.5
##  6 Rape               X2022    14.7
##  7 Robbery            X2021    17.9
##  8 Robbery            X2022    19.9
##  9 Aggravated assault X2021     6.7
## 10 Aggravated assault X2022     7.8
## # ℹ 16 more rows
ggplot(data_long6, aes(x = Percent, y = `Offense.type`, fill = Year)) +
  geom_bar(stat = "identity", position = position_dodge(width = 0.8), width = 0.7) +
  scale_fill_manual(values = c("X2021" = "pink2", "X2022" = "#A6CEE3")) +
  labs(title = "Percent of Offense Types for 2021 and 2022",
       x = "Percent", y = "Offense Type",
       fill = "Year") +
  theme_minimal() +
  theme(legend.position = "top")

Part 7

For my seventh figure, I am going to create a lineplot that plots year on the x axis and Rate of nonfatal violent victimization per 1,000 persons on the y axis.And the plot has three lines which are grouped by different ages(12 to 17,18 to 24,25 or older).I will create a three column tibble with these data.

data_long2 <- melt(data2, id.vars = "Year")
data_long2 <- data_long2 %>%
  mutate(value = as.numeric(as.character(value)))

data_long2
##    Year     variable value
## 1  2018    X12.to.17  34.2
## 2  2019    X12.to.17  35.2
## 3  2020    X12.to.17  17.4
## 4  2021    X12.to.17  13.2
## 5  2022    X12.to.17  27.4
## 6  2018    X18.to.24  35.9
## 7  2019    X18.to.24  37.2
## 8  2020    X18.to.24  29.6
## 9  2021    X18.to.24  23.1
## 10 2022    X18.to.24  36.8
## 11 2018 X25.or.older  20.2
## 12 2019 X25.or.older  17.2
## 13 2020 X25.or.older  14.6
## 14 2021 X25.or.older  16.0
## 15 2022 X25.or.older  21.3
data_long2 %>% 
  ggplot(aes(x = Year, y = value, group = variable, color = variable)) +
  geom_line(size = 2) +
  theme(legend.position="top") +
  theme(panel.grid.major.x = element_line(colour = "gray", linetype = "dotted"))+
  scale_y_continuous(breaks = seq(0, 200, by = 20))

ggtitle("Rate of nonfatal violent victimization(by age of victim) from 2018 to 2022")
## $title
## [1] "Rate of nonfatal violent victimization(by age of victim) from 2018 to 2022"
## 
## attr(,"class")
## [1] "labels"

Part 8

For my eighth figure, it is an extension of figure two,I am going to use gganimate create a animation that has five histogram plots which means five different years(2018-2022),and each year has three bars which group by age of victim(12 to 17,18 to 24,25 or older).The xlab is year and the ylab is rate of nonfatal violent victimization per 1,000 persons.Through this app you can choose which year’s plot that you want to look for.

data_long2 <- data2 %>%
  pivot_longer(cols = -Year, names_to = "Age_Group", values_to = "Rate")

# Recode Age_Group
data_long2$Age_Group <- recode(data_long2$Age_Group,
                               `X12.to.17` = "12 to 17",
                               `X18.to.24` = "18 to 24",
                               `X25.or.older` = "25 or older")


p <- ggplot(data_long2, aes(x = Year, y = Rate, fill = Age_Group)) +
  geom_bar(stat = "identity", position = position_dodge()) +
  scale_fill_manual(values = c("12 to 17" = "pink2", "18 to 24" = "plum", "25 or older" = "#A6CEE3")) +
  labs(y = "Rate per 1,000 persons", fill = "Age Group") +
  theme_minimal() +
  theme(legend.position = "top") +
  ggtitle("Rate of nonfatal violent victimization (by age of victim) from 2018 to 2022") +
  transition_states(Year, transition_length = 1, state_length = 1) +
  enter_fade() +
  exit_fade()

animate(p)