library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.4.4     ✔ tibble    3.2.1
## ✔ lubridate 1.9.3     ✔ tidyr     1.3.1
## ✔ purrr     1.0.2     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(plotly)
## 
## Attaching package: 'plotly'
## 
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## 
## The following object is masked from 'package:stats':
## 
##     filter
## 
## The following object is masked from 'package:graphics':
## 
##     layout
library(maps)
## 
## Attaching package: 'maps'
## 
## The following object is masked from 'package:purrr':
## 
##     map
df_state <-  read.csv("https://raw.githubusercontent.com/MAB592/DATA-608-SP2024/main/foodsecurity(state)%20(2).csv")
df_all <-  read.csv("https://raw.githubusercontent.com/MAB592/DATA-608-SP2024/main/foodsecurity(allhouseholds).xlsx.csv")
head(df_all)
##   Year              Category              Subcategory         Sub.subcategory
## 1 2001        All households                                                 
## 2 2001 Household composition With children < 18 years                        
## 3 2001 Household composition With children < 18 years With children < 6 years
## 4 2001 Household composition With children < 18 years Married-couple families
## 5 2001 Household composition With children < 18 years  Female head, no spouse
## 6 2001 Household composition With children < 18 years    Male head, no spouse
##    Total Food.secure.1.000 Food.secure.percent Food.insecure.1.000
## 1 107824             96303                89.3               11521
## 2  38330             32141                83.9                6189
## 3  16858             13920                82.6                2938
## 4  26182             23389                89.3                2793
## 5   9080              6185                68.1                2895
## 6   2389              2009                84.1                 380
##   Food.insecure.percent Low.food.security.1.000 Low.food.security.percent
## 1                  10.7                    8010                       7.4
## 2                  16.1                    4744                      12.4
## 3                  17.4                    2304                      13.7
## 4                  10.7                    2247                       8.6
## 5                  31.9                    2101                      23.1
## 6                  15.9                     298                      12.5
##   Very.low.food.security.1.000 Very.low.food.security.percent
## 1                         3511                            3.3
## 2                         1445                            3.8
## 3                          634                            3.8
## 4                          546                            2.1
## 5                          794                            8.7
## 6                           82                            3.4

Data Cleanining

df_all_household <- df_all[df_all$Sub.subcategory != "", ]
df_state <- df_state[!(df_state$State %in% c("U.S. total", "U.S.")), ]
head(df_state)
##        Year State Food.insecurity.prevalence Food.insecurity.margin.of.error
## 2 2006–2008    AK                       11.6                            1.66
## 3 2006–2008    AL                       13.3                            1.66
## 4 2006–2008    AR                       15.9                            3.19
## 5 2006–2008    AZ                       13.2                            1.51
## 6 2006–2008    CA                       12.0                            0.74
## 7 2006–2008    CO                       11.6                            1.13
##   Very.low.food.security.prevalence Very.low.food.security.margin.of.error
## 2                               4.4                                   1.31
## 3                               5.4                                   1.02
## 4                               5.6                                   1.50
## 5                               4.9                                   0.84
## 6                               4.3                                   0.48
## 7                               5.0                                   0.67
specific_items <- c("Male head, no spouse", "Female head, no spouse", "Married-couple families")
df_children_household <- df_all_household[df_all_household$Sub.subcategory %in% specific_items, ]
df_race <- df_all %>%
  filter(`Subcategory` %in% c("White non-Hispanic", "Black non-Hispanic", "Hispanic"))

# View the filtered dataframe
print(df_race)
##    Year                     Category        Subcategory Sub.subcategory Total
## 1  2001 Race/ethnicity of households White non-Hispanic                 80337
## 2  2001 Race/ethnicity of households Black non-Hispanic                 13134
## 3  2001 Race/ethnicity of households           Hispanic                  9864
## 4  2002 Race/ethnicity of households White non-Hispanic                 80266
## 5  2002 Race/ethnicity of households Black non-Hispanic                 13515
## 6  2002 Race/ethnicity of households           Hispanic                 10344
## 7  2003 Race/ethnicity of households White non-Hispanic                 81080
## 8  2003 Race/ethnicity of households Black non-Hispanic                 13156
## 9  2003 Race/ethnicity of households           Hispanic                 12034
## 10 2004 Race/ethnicity of households White non-Hispanic                 81388
## 11 2004 Race/ethnicity of households Black non-Hispanic                 13509
## 12 2004 Race/ethnicity of households           Hispanic                 12014
## 13 2005 Race/ethnicity of households White non-Hispanic                 82144
## 14 2005 Race/ethnicity of households Black non-Hispanic                 13732
## 15 2005 Race/ethnicity of households           Hispanic                 12397
## 16 2006 Race/ethnicity of households White non-Hispanic                 82268
## 17 2006 Race/ethnicity of households Black non-Hispanic                 14054
## 18 2006 Race/ethnicity of households           Hispanic                 12879
## 19 2007 Race/ethnicity of households White non-Hispanic                 82882
## 20 2007 Race/ethnicity of households Black non-Hispanic                 14209
## 21 2007 Race/ethnicity of households           Hispanic                 13378
## 22 2008 Race/ethnicity of households White non-Hispanic                 82935
## 23 2008 Race/ethnicity of households Black non-Hispanic                 14441
## 24 2008 Race/ethnicity of households           Hispanic                 13504
## 25 2009 Race/ethnicity of households White non-Hispanic                 83259
## 26 2009 Race/ethnicity of households Black non-Hispanic                 14519
## 27 2009 Race/ethnicity of households           Hispanic                 13566
## 28 2010 Race/ethnicity of households White non-Hispanic                 83113
## 29 2010 Race/ethnicity of households Black non-Hispanic                 14600
## 30 2010 Race/ethnicity of households           Hispanic                 14109
## 31 2011 Race/ethnicity of households White non-Hispanic                 83304
## 32 2011 Race/ethnicity of households Black non-Hispanic                 14765
## 33 2011 Race/ethnicity of households           Hispanic                 14410
## 34 2012 Race/ethnicity of households White non-Hispanic                 83217
## 35 2012 Race/ethnicity of households Black non-Hispanic                 14976
## 36 2012 Race/ethnicity of households           Hispanic                 15613
## 37 2013 Race/ethnicity of households White non-Hispanic                 83496
## 38 2013 Race/ethnicity of households Black non-Hispanic                 15311
## 39 2013 Race/ethnicity of households           Hispanic                 15648
## 40 2014 Race/ethnicity of households White non-Hispanic                 84127
## 41 2014 Race/ethnicity of households Black non-Hispanic                 15424
## 42 2014 Race/ethnicity of households           Hispanic                 16148
## 43 2015 Race/ethnicity of households White non-Hispanic                 83931
## 44 2015 Race/ethnicity of households Black non-Hispanic                 15734
## 45 2015 Race/ethnicity of households           Hispanic                 16803
## 46 2016 Race/ethnicity of households White non-Hispanic                 84087
## 47 2016 Race/ethnicity of households Black non-Hispanic                 15940
## 48 2016 Race/ethnicity of households           Hispanic                 17054
## 49 2017 Race/ethnicity of households White non-Hispanic                 84548
## 50 2017 Race/ethnicity of households Black non-Hispanic                 16358
## 51 2017 Race/ethnicity of households           Hispanic                 17197
## 52 2018 Race/ethnicity of households White non-Hispanic                 84975
## 53 2018 Race/ethnicity of households Black non-Hispanic                 16613
## 54 2018 Race/ethnicity of households           Hispanic                 18101
## 55 2019 Race/ethnicity of households White non-Hispanic                 85196
## 56 2019 Race/ethnicity of households Black non-Hispanic                 16504
## 57 2019 Race/ethnicity of households           Hispanic                 18035
## 58 2020 Race/ethnicity of households White non-Hispanic                 85466
## 59 2020 Race/ethnicity of households Black non-Hispanic                 16686
## 60 2020 Race/ethnicity of households           Hispanic                 18543
## 61 2021 Race/ethnicity of households White non-Hispanic                 85866
## 62 2021 Race/ethnicity of households Black non-Hispanic                 16951
## 63 2021 Race/ethnicity of households           Hispanic                 19082
## 64 2022 Race/ethnicity of households White non-Hispanic                 85603
## 65 2022 Race/ethnicity of households Black non-Hispanic                 17271
## 66 2022 Race/ethnicity of households           Hispanic                 19507
##    Food.secure.1.000 Food.secure.percent Food.insecure.1.000
## 1              74230                92.4                6107
## 2              10331                78.7                2803
## 3               7717                78.2                2147
## 4              73859                92.0                6407
## 5              10546                78.0                2969
## 6               8099                78.3                2245
## 7              74733                92.2                6347
## 8              10251                77.9                2905
## 9               9347                77.7                2687
## 10             74383                91.4                7005
## 11             10303                76.3                3206
## 12              9404                78.3                2610
## 13             75444                91.8                6700
## 14             10658                77.6                3074
## 15             10176                82.1                2221
## 16             75810                92.2                6458
## 17             10991                78.2                3063
## 18             10367                80.5                2512
## 19             76342                92.1                6540
## 20             11057                77.8                3152
## 21             10694                79.9                2684
## 22             74041                89.3                8894
## 23             10732                74.3                3709
## 24              9873                73.1                3631
## 25             74066                89.0                9193
## 26             10908                75.1                3611
## 27              9922                73.1                3644
## 28             74117                89.2                8996
## 29             10941                74.9                3659
## 30             10416                73.8                3693
## 31             73823                88.6                9481
## 32             11066                74.9                3699
## 33             10629                73.8                3781
## 34             73914                88.8                9303
## 35             11287                75.4                3689
## 36             11982                76.7                3631
## 37             74636                89.4                8860
## 38             11322                73.9                3989
## 39             11939                76.3                3709
## 40             75335                89.5                8792
## 41             11393                73.9                4031
## 42             12527                77.6                3621
## 43             75563                90.0                8368
## 44             12357                78.5                3377
## 45             13592                80.9                3211
## 46             76271                90.7                7816
## 47             12346                77.5                3594
## 48             13907                81.5                3147
## 49             77083                91.2                7465
## 50             12799                78.2                3559
## 51             14108                82.0                3089
## 52             78106                91.9                6869
## 53             13087                78.8                3526
## 54             15164                83.8                2937
## 55             78438                92.1                6758
## 56             13356                80.9                3148
## 57             15216                84.4                2819
## 58             79387                92.9                6079
## 59             13058                78.3                3628
## 60             15359                82.8                3184
## 61             79843                93.0                6023
## 62             13597                80.2                3354
## 63             15983                83.8                3099
## 64             77682                90.7                7921
## 65             13406                77.6                3865
## 66             15453                79.2                4054
##    Food.insecure.percent Low.food.security.1.000 Low.food.security.percent
## 1                    7.6                    4072                       5.1
## 2                   21.3                    1986                      15.1
## 3                   21.8                    1613                      16.4
## 4                    8.0                    4294                       5.3
## 5                   22.0                    1999                      14.8
## 6                   21.7                    1654                      16.0
## 7                    7.8                    4169                       5.1
## 8                   22.1                    2010                      15.3
## 9                   22.3                    2034                      16.9
## 10                   8.6                    4632                       5.7
## 11                  23.7                    2108                      15.6
## 12                  21.7                    1903                      15.8
## 13                   8.2                    4305                       5.2
## 14                  22.4                    1894                      13.8
## 15                  17.9                    1559                      12.6
## 16                   7.8                    3937                       4.8
## 17                  21.8                    1944                      13.8
## 18                  19.5                    1780                      13.8
## 19                   7.9                    4008                       4.8
## 20                  22.2                    2064                      14.5
## 21                  20.1                    1798                      13.4
## 22                  10.7                    5154                       6.2
## 23                  25.7                    2251                      15.6
## 24                  26.9                    2439                      18.1
## 25                  11.0                    5394                       6.5
## 26                  24.9                    2259                      15.6
## 27                  26.9                    2388                      17.6
## 28                  10.8                    5512                       6.6
## 29                  25.1                    2332                      16.0
## 30                  26.2                    2507                      17.8
## 31                  11.4                    5689                       6.8
## 32                  25.1                    2155                      14.6
## 33                  26.2                    2583                      17.9
## 34                  11.2                    5452                       6.6
## 35                  24.6                    2136                      14.3
## 36                  23.3                    2469                      15.8
## 37                  10.6                    5033                       6.0
## 38                  26.1                    2442                      15.9
## 39                  23.7                    2660                      17.0
## 40                  10.5                    4966                       5.9
## 41                  26.1                    2434                      15.8
## 42                  22.4                    2502                      15.5
## 43                  10.0                    4759                       5.7
## 44                  21.5                    2127                      13.5
## 45                  19.1                    2132                      12.7
## 46                   9.3                    4591                       5.5
## 47                  22.5                    2048                      12.8
## 48                  18.5                    2152                      12.7
## 49                   8.8                    4389                       5.2
## 50                  21.8                    2164                      13.2
## 51                  18.0                    2137                      12.4
## 52                   8.1                    4109                       4.9
## 53                  21.2                    2021                      12.1
## 54                  16.2                    2018                      11.1
## 55                   7.9                    3966                       4.7
## 56                  19.1                    1891                      11.5
## 57                  15.6                    1937                      10.7
## 58                   7.1                    3547                       4.1
## 59                  21.7                    2295                      13.7
## 60                  17.2                    2263                      12.2
## 61                   7.0                    3754                       4.4
## 62                  19.8                    2019                      11.9
## 63                  16.2                    2059                      10.7
## 64                   9.3                    4522                       5.3
## 65                  22.4                    2283                      13.2
## 66                  20.8                    2682                      13.8
##    Very.low.food.security.1.000 Very.low.food.security.percent
## 1                          2035                            2.5
## 2                           817                            6.2
## 3                           534                            5.4
## 4                          2113                            2.6
## 5                           970                            7.2
## 6                           591                            5.7
## 7                          2178                            2.7
## 8                           895                            6.8
## 9                           653                            5.4
## 10                         2373                            2.9
## 11                         1098                            8.1
## 12                          707                            5.9
## 13                         2395                            2.9
## 14                         1180                            8.6
## 15                          662                            5.3
## 16                         2521                            3.1
## 17                         1119                            8.0
## 18                          732                            5.7
## 19                         2532                            3.1
## 20                         1088                            7.7
## 21                          886                            6.6
## 22                         3740                            4.5
## 23                         1458                           10.1
## 24                         1192                            8.8
## 25                         3799                            4.6
## 26                         1352                            9.3
## 27                         1256                            9.3
## 28                         3484                            4.2
## 29                         1327                            9.1
## 30                         1186                            8.4
## 31                         3792                            4.6
## 32                         1544                           10.5
## 33                         1198                            8.3
## 34                         3851                            4.6
## 35                         1553                           10.4
## 36                         1162                            7.4
## 37                         3827                            4.6
## 38                         1547                           10.1
## 39                         1049                            6.7
## 40                         3826                            4.5
## 41                         1597                           10.4
## 42                         1119                            6.9
## 43                         3609                            4.3
## 44                         1250                            7.9
## 45                         1079                            6.4
## 46                         3225                            3.8
## 47                         1546                            9.7
## 48                          995                            5.8
## 49                         3076                            3.6
## 50                         1395                            8.5
## 51                          952                            5.5
## 52                         2760                            3.2
## 53                         1505                            9.1
## 54                          919                            5.1
## 55                         2792                            3.3
## 56                         1257                            7.6
## 57                          882                            4.9
## 58                         2532                            3.0
## 59                         1333                            8.0
## 60                          921                            5.0
## 61                         2269                            2.6
## 62                         1335                            7.9
## 63                         1040                            5.5
## 64                         3399                            4.0
## 65                         1582                            9.2
## 66                         1372                            7.0
unique(df_state$Year)
##  [1] "2006–2008" "2007–2009" "2008–2010" "2009–2011" "2010–2012" "2011–2013"
##  [7] "2012–2014" "2013–2015" "2014–2016" "2015–2017" "2016–2018" "2017–2019"
## [13] "2018–2020" "2019–2021" "2020–2022"
filtered_data <- df_state %>% 
  filter(Year %in% c("2020–2022"))

Analyzing Food Security and Nutrition Data

We start by examining household-level data on food security and nutrition in the US. This data provides insights into the prevalence of food insecurity across different household compositions, including those with children and different racial and ethnic backgrounds.

Food Insecurity Trends Let’s first explore the trends in food insecurity among different household compositions over the years. The graph below illustrates the percentage of food-insecure households from 2008 to 2022.

# Food Insecurity by Household Composition
plot_ly(
  data = df_all_household,
  x = ~Year,
  y = ~Food.insecure.percent,
  color = ~Sub.subcategory,
  type = 'scatter',
  mode = 'lines+markers',
  text = ~paste("Year: ", Year, "<br>Food Insecure Households Percent: ", Food.insecure.percent),
  hoverinfo = 'text',
  width = 800,
  height = 400
) %>%
  layout(
    title = "<b>FOOD INSECURITY BY HOUSEHOLD COMPOSITION IN THE U.S.</b>",
    xaxis = list(
      title = "Year",
      tickmode = "linear",
      dtick = 1
    ),
    yaxis = list(title = "Food Insecurity Households Percent"),
    legend = list(x = 1, y = 1),
    margin = list(b = 60, l = 60, r = 60, t = 60),
    annotations = list(
      list(
        text = "Food Insecurity by household composition, 2008 - 2022",
        x = 0, y = 1.1,
        xref = "paper", yref = "paper",
        showarrow = FALSE,
        font = list(size = 14)
      )
    )
  )
## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

## Warning in RColorBrewer::brewer.pal(N, "Set2"): n too large, allowed maximum for palette Set2 is 8
## Returning the palette you asked for with that many colors

Food Insecurity by State

Next, let’s examine the variation in food insecurity prevalence across different states in the US from 2020 to 2022.

# Food Insecurity by State
ggplot(filtered_data, aes(x = reorder(State, -Food.insecurity.prevalence), y = Food.insecurity.prevalence, fill = State)) +
  geom_bar(stat = "identity") +
  scale_fill_manual(values = rep(c("#FF9999", "#66CCCC", "#99CC99"), length(unique(filtered_data$State)))) + 
  labs(title = "Food Insecurity Prevalence by State (2020-2022)",
       x = "State", y = "Food Insecurity Prevalence") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +  
  coord_flip() +  
  guides(fill = 'none')

Food Insecurity Among Children

Lastly, we investigate food insecurity among children, focusing on different household compositions and racial/ethnic backgrounds.

# Food Insecurity Among Children by Household Composition
plot_ly(
  data = df_children_household,
  x = ~Year,
  y = ~Food.insecure.percent,
  color = ~Sub.subcategory,
  type = 'scatter',
  mode = 'lines+markers',
  text = ~paste("Year: ", Year, "<br>Food Insecure Households Percent: ", Food.insecure.percent),
  hoverinfo = 'text',
  width = 800,
  height = 400
) %>%
  layout(
    title = "<b>FOOD INSECURITY AMONG CHILDREN BY HOUSEHOLD COMPOSITION IN THE U.S.</b>",
    xaxis = list(
      title = "Year",
      tickmode = "linear",
      dtick = 1
    ),
    yaxis = list(title = "Percent of Households with Children"),
    legend = list(x = 1.00, y = 1),
    margin = list(b = 60, l = 60, r = 60, t = 60),
    annotations = list(
      list(
        text = "Food Insecurity among children by household composition, 2008 - 2022",
        x = 0.0, y = 1.10,
        xref = "paper", yref = "paper",
        showarrow = FALSE,
        font = list(size = 14)
      )
    )
  )
# Food Insecurity Among Children by Race and Ethnicity
plot_ly(
  data = df_race,
  x = ~Year,
  y = ~Food.insecure.percent,
  color = ~Subcategory,
   type = 'scatter',
  mode = 'lines+markers',
  text = ~paste("Year: ", Year, "Food Insecurity Among Children: ", Food.insecure.percent),
  hoverinfo = 'text',
  width = 800,
  height = 400
) %>%
  layout(
    title = list(
      text = "<b>FOOD INSECURITY AMONG CHILDREN BY RACE AND ETHNICITY IN THE U.S.</b>",
      x = -.10
    ),
    xaxis = list(title = "Year"),
    yaxis = list(title = "Food Insecurity Among Children"),
    legend = list(x = 1.00, y = 1),
    margin = list(b = 60, l = 60, r = 60, t = 60),
    height = 400,
    width = 800,
    annotations = list(
      list(
        text = "Food Insecurity among children by race and ethnicity of head of household, 2008 - 2022",
        x = 1.01, y = 1.10,
        xref = "paper", yref = "paper",
        showarrow = FALSE,
        font = list(size = 14)
      )
    )
  )
## Warning: Specifying width/height in layout() is now deprecated.
## Please specify in ggplotly() or plot_ly()

##Conclusion

Analyzing the data on food security and nutrition in the US reveals significant disparities across different household compositions, racial and ethnic backgrounds, and states. It’s evident that addressing food insecurity requires targeted interventions addressing the root causes, including poverty, systemic inequalities, and access to resources. As citizens, it’s imperative to advocate for policies and initiatives that aim to eradicate food insecurity and ensure the well-being of all individuals and families in the US.