cis-ds / Discussion

Public discussion
10 stars 15 forks source link

01-drop-na-example #180

Closed bensoltoff closed 2 years ago

bensoltoff commented 3 years ago

Post your reproducible example here

nearridge commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

ellamarrero commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v1.0.0)

jthedu commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

chidatlam commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

margotbarranco commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"
lkataja commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

bensoltoff commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

liu15611 commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

vinsgromeo commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

claramfong commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"
msoba22 commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt~  2019 Aug       4 Dayton,~ "PENDI~          9      27            36
#>  2 El P~  2019 Aug       3 El Paso~ "PENDI~         20      26            46
#>  3 Gilr~  2019 Jul      28 Gilroy,~ "Santi~          3      12            15
#>  4 Virg~  2019 May      31 Virgini~ "DeWay~         12       4            16
#>  5 Harr~  2019 Feb      15 Aurora,~ "Gary ~          5       6            11
#>  6 Penn~  2019 Jan      24 State C~ "Jorda~          3       1             4
#>  7 SunT~  2019 Jan      23 Sebring~ "Zephe~          5       0             5
#>  8 Merc~  2018 Nov      19 Chicago~ "Juan ~          3       0             3
#>  9 Thou~  2018 Nov       7 Thousan~ "Ian D~         12      22            34
#> 10 Tree~  2018 Oct      27 Pittsbu~ "Rober~         11       6            17
#> # ... with 104 more rows, and 5 more variables: location_type <chr>,
#> #   male <lgl>, age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v1.0.0)

bensoltoff commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race)
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

avivawaldman commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt~  2019 Aug       4 Dayton,~ "PENDI~          9      27            36
#>  2 El P~  2019 Aug       3 El Paso~ "PENDI~         20      26            46
#>  3 Gilr~  2019 Jul      28 Gilroy,~ "Santi~          3      12            15
#>  4 Virg~  2019 May      31 Virgini~ "DeWay~         12       4            16
#>  5 Harr~  2019 Feb      15 Aurora,~ "Gary ~          5       6            11
#>  6 Penn~  2019 Jan      24 State C~ "Jorda~          3       1             4
#>  7 SunT~  2019 Jan      23 Sebring~ "Zephe~          5       0             5
#>  8 Merc~  2018 Nov      19 Chicago~ "Juan ~          3       0             3
#>  9 Thou~  2018 Nov       7 Thousan~ "Ian D~         12      22            34
#> 10 Tree~  2018 Oct      27 Pittsbu~ "Rober~         11       6            17
#> # ... with 104 more rows, and 5 more variables: location_type <chr>,
#> #   male <lgl>, age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

reillyoflaherty commented 3 years ago

Copy the code below to generate a reproducible example

using the reprex package. Once you generate it, post it on

https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)

>

> Attaching package: 'dplyr'

> The following objects are masked from 'package:stats':

>

> filter, lag

> The following objects are masked from 'package:base':

>

> intersect, setdiff, setequal, union

library(ggplot2)

get data from rcfss package

install latest version if not already installed

devtools::install_github("uc-cfss/rcfss")

library(rcfss)

load the data

data("mass_shootings") mass_shootings

> # A tibble: 114 x 14

> case year month day location summary fatalities injured total_victims

>

> 1 Dayt… 2019 Aug 4 Dayton,… "PENDI… 9 27 36

> 2 El P… 2019 Aug 3 El Paso… "PENDI… 20 26 46

> 3 Gilr… 2019 Jul 28 Gilroy,… "Santi… 3 12 15

> 4 Virg… 2019 May 31 Virgini… "DeWay… 12 4 16

> 5 Harr… 2019 Feb 15 Aurora,… "Gary … 5 6 11

> 6 Penn… 2019 Jan 24 State C… "Jorda… 3 1 4

> 7 SunT… 2019 Jan 23 Sebring… "Zephe… 5 0 5

> 8 Merc… 2018 Nov 19 Chicago… "Juan … 3 0 3

> 9 Thou… 2018 Nov 7 Thousan… "Ian D… 12 22 34

> 10 Tree… 2018 Oct 27 Pittsbu… "Rober… 11 6 17

> # … with 104 more rows, and 5 more variables: location_type , male ,

> # age_of_shooter , race , prior_mental_illness

Generate a bar chart that identifies the number of mass shooters

associated with each race category. The bars should be sorted

from highest to lowest.

using reorder() and aggregating the data before plotting

mass_shootings %>% count(race) %>% drop_na(race) %>% ggplot(mapping = aes(x = reorder(race, -n), y = n)) + geom_col() + labs( title = "Mass shootings in the United States (1982-2019)", x = "Race of perpetrator", y = "Number of incidents" )

> Error in drop_na(., race): could not find function "drop_na"

lisettegonzalez commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

mnjones323 commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

reillyoflaherty commented 3 years ago

image

bensoltoff commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v1.0.0)

reillyoflaherty commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v0.3.0)

sterlingfearing commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case   year month   day location summary fatalities injured total_victims
#>    <chr> <dbl> <chr> <int> <chr>    <chr>        <dbl>   <dbl>         <dbl>
#>  1 Dayt…  2019 Aug       4 Dayton,… "PENDI…          9      27            36
#>  2 El P…  2019 Aug       3 El Paso… "PENDI…         20      26            46
#>  3 Gilr…  2019 Jul      28 Gilroy,… "Santi…          3      12            15
#>  4 Virg…  2019 May      31 Virgini… "DeWay…         12       4            16
#>  5 Harr…  2019 Feb      15 Aurora,… "Gary …          5       6            11
#>  6 Penn…  2019 Jan      24 State C… "Jorda…          3       1             4
#>  7 SunT…  2019 Jan      23 Sebring… "Zephe…          5       0             5
#>  8 Merc…  2018 Nov      19 Chicago… "Juan …          3       0             3
#>  9 Thou…  2018 Nov       7 Thousan… "Ian D…         12      22            34
#> 10 Tree…  2018 Oct      27 Pittsbu… "Rober…         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-02-18 by the reprex package (v1.0.0)

sterlingfearing commented 3 years ago
## Use the script below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/182
## 
## Hint: look at the input and outfile arguments to reprex()

library(tidyverse)
library(here)
#> here() starts at /Users/sterlingfearing/Desktop/uc-cfss-reproducible-examples-and-git-47116e5

# import data file
urban <- read_csv(here("data", "urbanization-state.csv"))
#> 
#> ── Column specification ────────────────────────────────────────────────────────
#> cols(
#>   state = col_character(),
#>   urbanindex = col_double()
#> )

# how do I reorder the bars from largest to smallest?
ggplot(data = urban, mapping = aes(x = state, y = urbanindex)) +
  geom_col() +
  coord_flip()

Created on 2021-02-18 by the reprex package (v1.0.0)

AlexPrizzy commented 3 years ago

Copy the code below to generate a reproducible example

using the reprex package. Once you generate it, post it on

https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)

>

> Attaching package: 'dplyr'

> The following objects are masked from 'package:stats':

>

> filter, lag

> The following objects are masked from 'package:base':

>

> intersect, setdiff, setequal, union

>

> Attaching package: 'dplyr'

> The following objects are masked from 'package:stats':

>

> filter, lag

> The following objects are masked from 'package:base':

>

> intersect, setdiff, setequal, union

library(ggplot2)

get data from rcfss package

install latest version if not already installed

devtools::install_github("uc-cfss/rcfss")

library(rcfss)

load the data

data("mass_shootings") mass_shootings

> # A tibble: 114 x 14

> case year month day location summary fatalities injured total_victims

>

> 1 Dayt… 2019 Aug 4 Dayton,… "PENDI… 9 27 36

> 2 El P… 2019 Aug 3 El Paso… "PENDI… 20 26 46

> 3 Gilr… 2019 Jul 28 Gilroy,… "Santi… 3 12 15

> 4 Virg… 2019 May 31 Virgini… "DeWay… 12 4 16

> 5 Harr… 2019 Feb 15 Aurora,… "Gary … 5 6 11

> 6 Penn… 2019 Jan 24 State C… "Jorda… 3 1 4

> 7 SunT… 2019 Jan 23 Sebring… "Zephe… 5 0 5

> 8 Merc… 2018 Nov 19 Chicago… "Juan … 3 0 3

> 9 Thou… 2018 Nov 7 Thousan… "Ian D… 12 22 34

> 10 Tree… 2018 Oct 27 Pittsbu… "Rober… 11 6 17

> # … with 104 more rows, and 5 more variables: location_type , male ,

> # age_of_shooter , race , prior_mental_illness

> # A tibble: 114 x 14

> case year month day location summary fatalities injured total_victims

>

> 1 Dayt… 2019 Aug 4 Dayton,… "PENDI… 9 27 36

> 2 El P… 2019 Aug 3 El Paso… "PENDI… 20 26 46

> 3 Gilr… 2019 Jul 28 Gilroy,… "Santi… 3 12 15

> 4 Virg… 2019 May 31 Virgini… "DeWay… 12 4 16

> 5 Harr… 2019 Feb 15 Aurora,… "Gary … 5 6 11

> 6 Penn… 2019 Jan 24 State C… "Jorda… 3 1 4

> 7 SunT… 2019 Jan 23 Sebring… "Zephe… 5 0 5

> 8 Merc… 2018 Nov 19 Chicago… "Juan … 3 0 3

> 9 Thou… 2018 Nov 7 Thousan… "Ian D… 12 22 34

> 10 Tree… 2018 Oct 27 Pittsbu… "Rober… 11 6 17

> # … with 104 more rows, and 5 more variables: location_type , male ,

> # age_of_shooter , race , prior_mental_illness

Generate a bar chart that identifies the number of mass shooters

associated with each race category. The bars should be sorted

from highest to lowest.

using reorder() and aggregating the data before plotting

mass_shootings %>% count(race) %>% drop_na(race) %>% ggplot(mapping = aes(x = reorder(race, -n), y = n)) + geom_col() + labs( title = "Mass shootings in the United States (1982-2019)", x = "Race of perpetrator", y = "Number of incidents" )

> Error in drop_na(., race): could not find function "drop_na"

dipro-ray commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-07-13 by the reprex package (v2.0.0)

karenliu0212 commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-07-13 by the reprex package (v2.0.0)

bethwang06 commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-07-13 by the reprex package (v2.0.0)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.1.0 (2021-05-18) #> os macOS Big Sur 10.16 #> system x86_64, darwin17.0 #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/Chicago #> date 2021-07-13 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [1] CRAN (R 4.1.0) #> backports 1.2.1 2020-12-09 [1] CRAN (R 4.1.0) #> cli 2.5.0 2021-04-26 [1] CRAN (R 4.1.0) #> colorspace 2.0-1 2021-05-04 [1] CRAN (R 4.1.0) #> crayon 1.4.1 2021-02-08 [1] CRAN (R 4.1.0) #> DBI 1.1.1 2021-01-15 [1] CRAN (R 4.1.0) #> digest 0.6.27 2020-10-24 [1] CRAN (R 4.1.0) #> dplyr * 1.0.7 2021-06-18 [1] CRAN (R 4.1.0) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.1.0) #> evaluate 0.14 2019-05-28 [1] CRAN (R 4.1.0) #> fansi 0.5.0 2021-05-25 [1] CRAN (R 4.1.0) #> fs 1.5.0 2020-07-31 [1] CRAN (R 4.1.0) #> generics 0.1.0 2020-10-31 [1] CRAN (R 4.1.0) #> ggplot2 * 3.3.4 2021-06-16 [1] CRAN (R 4.1.0) #> glue 1.4.2 2020-08-27 [1] CRAN (R 4.1.0) #> gtable 0.3.0 2019-03-25 [1] CRAN (R 4.1.0) #> highr 0.9 2021-04-16 [1] CRAN (R 4.1.0) #> htmltools 0.5.1.1 2021-01-22 [1] CRAN (R 4.1.0) #> knitr 1.33 2021-04-24 [1] CRAN (R 4.1.0) #> lifecycle 1.0.0 2021-02-15 [1] CRAN (R 4.1.0) #> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.1.0) #> munsell 0.5.0 2018-06-12 [1] CRAN (R 4.1.0) #> pillar 1.6.1 2021-05-16 [1] CRAN (R 4.1.0) #> pkgconfig 2.0.3 2019-09-22 [1] CRAN (R 4.1.0) #> purrr 0.3.4 2020-04-17 [1] CRAN (R 4.1.0) #> R6 2.5.0 2020-10-28 [1] CRAN (R 4.1.0) #> rcfss * 0.2.1 2021-06-23 [1] Github (uc-cfss/rcfss@5b60f61) #> reprex 2.0.0 2021-04-02 [1] CRAN (R 4.1.0) #> rlang 0.4.11 2021-04-30 [1] CRAN (R 4.1.0) #> rmarkdown 2.9 2021-06-15 [1] CRAN (R 4.1.0) #> rstudioapi 0.13 2020-11-12 [1] CRAN (R 4.1.0) #> scales 1.1.1 2020-05-11 [1] CRAN (R 4.1.0) #> sessioninfo 1.1.1 2018-11-05 [1] CRAN (R 4.1.0) #> stringi 1.6.2 2021-05-17 [1] CRAN (R 4.1.0) #> stringr 1.4.0 2019-02-10 [1] CRAN (R 4.1.0) #> styler 1.4.1 2021-03-30 [1] CRAN (R 4.1.0) #> tibble 3.1.2 2021-05-16 [1] CRAN (R 4.1.0) #> tidyselect 1.1.1 2021-04-30 [1] CRAN (R 4.1.0) #> utf8 1.2.1 2021-03-12 [1] CRAN (R 4.1.0) #> vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.1.0) #> withr 2.4.2 2021-04-18 [1] CRAN (R 4.1.0) #> xfun 0.24 2021-06-15 [1] CRAN (R 4.1.0) #> yaml 2.2.1 2020-02-01 [1] CRAN (R 4.1.0) #> #> [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library ```
agodinez711 commented 3 years ago

library(dplyr) library(ggplot2)

get data from rcfss package

install latest version if not already installed

devtools::install_github("uc-cfss/rcfss")

library(rcfss)

load the data

data("mass_shootings") mass_shootings

Generate a bar chart that identifies the number of mass shooters

associated with each race category. The bars should be sorted

from highest to lowest.

using reorder() and aggregating the data before plotting

mass_shootings %>% count(race) %>% drop_na(race) %>% ggplot(mapping = aes(x = reorder(race, -n), y = n)) + geom_col() + labs( title = "Mass shootings in the United States (1982-2019)", x = "Race of perpetrator", y = "Number of incidents" )

anabellxu commented 3 years ago
 ## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na" 
gdicera commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-07-13 by the reprex package (v2.0.0)

ssmyzs commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"
mjparness commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-07-13 by the reprex package (v2.0.0)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.0.1 (2020-06-06) #> os Red Hat Enterprise Linux 8.4 (Ootpa) #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/Chicago #> date 2021-07-13 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [2] CRAN (R 4.0.1) #> backports 1.2.1 2020-12-09 [2] CRAN (R 4.0.1) #> cli 2.5.0 2021-04-26 [1] CRAN (R 4.0.1) #> colorspace 2.0-0 2020-11-11 [2] CRAN (R 4.0.1) #> crayon 1.4.1 2021-02-08 [2] CRAN (R 4.0.1) #> DBI 1.1.1 2021-01-15 [2] CRAN (R 4.0.1) #> digest 0.6.27 2020-10-24 [2] CRAN (R 4.0.1) #> dplyr * 1.0.5 2021-03-05 [2] CRAN (R 4.0.1) #> ellipsis 0.3.1 2020-05-15 [2] CRAN (R 4.0.1) #> evaluate 0.14 2019-05-28 [2] CRAN (R 4.0.1) #> fansi 0.4.2 2021-01-15 [2] CRAN (R 4.0.1) #> fs 1.5.0 2020-07-31 [2] CRAN (R 4.0.1) #> generics 0.1.0 2020-10-31 [2] CRAN (R 4.0.1) #> ggplot2 * 3.3.3 2020-12-30 [2] CRAN (R 4.0.1) #> glue 1.4.2 2020-08-27 [2] CRAN (R 4.0.1) #> gtable 0.3.0 2019-03-25 [2] CRAN (R 4.0.1) #> highr 0.8 2019-03-20 [2] CRAN (R 4.0.1) #> htmltools 0.5.1.1 2021-01-22 [2] CRAN (R 4.0.1) #> knitr 1.31 2021-01-27 [2] CRAN (R 4.0.1) #> lifecycle 1.0.0 2021-02-15 [2] CRAN (R 4.0.1) #> magrittr 2.0.1 2020-11-17 [2] CRAN (R 4.0.1) #> munsell 0.5.0 2018-06-12 [2] CRAN (R 4.0.1) #> pillar 1.5.1 2021-03-05 [2] CRAN (R 4.0.1) #> pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.0.1) #> purrr 0.3.4 2020-04-17 [2] CRAN (R 4.0.1) #> R6 2.5.0 2020-10-28 [2] CRAN (R 4.0.1) #> rcfss * 0.2.1 2021-06-23 [1] Github (uc-cfss/rcfss@5b60f61) #> reprex 2.0.0 2021-04-02 [2] CRAN (R 4.0.1) #> rlang 0.4.11 2021-04-30 [1] CRAN (R 4.0.1) #> rmarkdown 2.7 2021-02-19 [2] CRAN (R 4.0.1) #> rstudioapi 0.13 2020-11-12 [2] CRAN (R 4.0.1) #> scales 1.1.1 2020-05-11 [2] CRAN (R 4.0.1) #> sessioninfo 1.1.1 2018-11-05 [2] CRAN (R 4.0.1) #> stringi 1.5.3 2020-09-09 [2] CRAN (R 4.0.1) #> stringr 1.4.0 2019-02-10 [2] CRAN (R 4.0.1) #> styler 1.4.1 2021-03-30 [2] CRAN (R 4.0.1) #> tibble 3.1.0 2021-02-25 [2] CRAN (R 4.0.1) #> tidyselect 1.1.0 2020-05-11 [2] CRAN (R 4.0.1) #> utf8 1.2.1 2021-03-12 [2] CRAN (R 4.0.1) #> vctrs 0.3.7 2021-03-29 [2] CRAN (R 4.0.1) #> withr 2.4.1 2021-01-26 [2] CRAN (R 4.0.1) #> xfun 0.22 2021-03-11 [2] CRAN (R 4.0.1) #> yaml 2.2.1 2020-02-01 [2] CRAN (R 4.0.1) #> #> [1] /home/mjparness/R/x86_64-pc-linux-gnu-library/4.0 #> [2] /opt/R/4.0.1/lib/R/library ```
bensoltoff commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-07-13 by the reprex package (v2.0.0)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.0.1 (2020-06-06) #> os Red Hat Enterprise Linux 8.4 (Ootpa) #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/Chicago #> date 2021-07-13 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [2] CRAN (R 4.0.1) #> backports 1.2.1 2020-12-09 [2] CRAN (R 4.0.1) #> cli 2.5.0 2021-04-26 [1] CRAN (R 4.0.1) #> colorspace 2.0-0 2020-11-11 [2] CRAN (R 4.0.1) #> crayon 1.4.1 2021-02-08 [2] CRAN (R 4.0.1) #> DBI 1.1.1 2021-01-15 [2] CRAN (R 4.0.1) #> digest 0.6.27 2020-10-24 [2] CRAN (R 4.0.1) #> dplyr * 1.0.7 2021-06-18 [1] CRAN (R 4.0.1) #> ellipsis 0.3.2 2021-04-29 [1] CRAN (R 4.0.1) #> evaluate 0.14 2019-05-28 [2] CRAN (R 4.0.1) #> fansi 0.5.0 2021-05-25 [1] CRAN (R 4.0.1) #> fs 1.5.0 2020-07-31 [2] CRAN (R 4.0.1) #> generics 0.1.0 2020-10-31 [2] CRAN (R 4.0.1) #> ggplot2 * 3.3.3 2020-12-30 [2] CRAN (R 4.0.1) #> glue 1.4.2 2020-08-27 [2] CRAN (R 4.0.1) #> gtable 0.3.0 2019-03-25 [2] CRAN (R 4.0.1) #> highr 0.8 2019-03-20 [2] CRAN (R 4.0.1) #> htmltools 0.5.1.1 2021-01-22 [2] CRAN (R 4.0.1) #> knitr 1.31 2021-01-27 [2] CRAN (R 4.0.1) #> lifecycle 1.0.0 2021-02-15 [2] CRAN (R 4.0.1) #> magrittr 2.0.1 2020-11-17 [2] CRAN (R 4.0.1) #> munsell 0.5.0 2018-06-12 [2] CRAN (R 4.0.1) #> pillar 1.6.1 2021-05-16 [1] CRAN (R 4.0.1) #> pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.0.1) #> purrr 0.3.4 2020-04-17 [2] CRAN (R 4.0.1) #> R6 2.5.0 2020-10-28 [2] CRAN (R 4.0.1) #> rcfss * 0.2.1 2021-04-02 [2] Github (uc-cfss/rcfss@4d9144d) #> reprex 2.0.0 2021-04-02 [2] CRAN (R 4.0.1) #> rlang 0.4.11 2021-04-30 [1] CRAN (R 4.0.1) #> rmarkdown 2.9 2021-06-15 [1] CRAN (R 4.0.1) #> rstudioapi 0.13 2020-11-12 [2] CRAN (R 4.0.1) #> scales 1.1.1 2020-05-11 [2] CRAN (R 4.0.1) #> sessioninfo 1.1.1 2018-11-05 [2] CRAN (R 4.0.1) #> stringi 1.5.3 2020-09-09 [2] CRAN (R 4.0.1) #> stringr 1.4.0 2019-02-10 [2] CRAN (R 4.0.1) #> styler 1.4.1 2021-03-30 [2] CRAN (R 4.0.1) #> tibble 3.1.2 2021-05-16 [1] CRAN (R 4.0.1) #> tidyselect 1.1.1 2021-04-30 [1] CRAN (R 4.0.1) #> utf8 1.2.1 2021-03-12 [2] CRAN (R 4.0.1) #> vctrs 0.3.8 2021-04-29 [1] CRAN (R 4.0.1) #> withr 2.4.2 2021-04-18 [1] CRAN (R 4.0.1) #> xfun 0.22 2021-03-11 [2] CRAN (R 4.0.1) #> yaml 2.2.1 2020-02-01 [2] CRAN (R 4.0.1) #> #> [1] /home/soltoffbc/R/x86_64-pc-linux-gnu-library/4.0 #> [2] /opt/R/4.0.1/lib/R/library ```
cslewis12 commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"
reprex::reprex()
#> ℹ Non-interactive session, setting `html_preview = FALSE`.
#> CLIPR_ALLOW has not been set, so clipr will not run interactively
#> Error in switch(where, expr = stringify_expression(x_expr), clipboard = ingest_clipboard(), : EXPR must be a length 1 vector

Created on 2021-07-13 by the reprex package (v2.0.0)

agodinez711 commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-07-13 by the reprex package (v2.0.0)

Maggie-Rivera commented 3 years ago
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"
anabellxu commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/181

library(tidyverse)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using forcats::fct_infreq() and using the raw data for plotting
mass_shootings %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = fct_infreq(race))) +
  geom_bar() +
  coord_flip() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )

Created on 2021-07-13 by the reprex package (v2.0.0)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.0.1 (2020-06-06) #> os Red Hat Enterprise Linux 8.4 (Ootpa) #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/Chicago #> date 2021-07-13 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [2] CRAN (R 4.0.1) #> backports 1.2.1 2020-12-09 [2] CRAN (R 4.0.1) #> broom 0.7.5 2021-02-19 [2] CRAN (R 4.0.1) #> cellranger 1.1.0 2016-07-27 [2] CRAN (R 4.0.1) #> cli 3.0.0 2021-06-30 [1] CRAN (R 4.0.1) #> colorspace 2.0-0 2020-11-11 [2] CRAN (R 4.0.1) #> crayon 1.4.1 2021-02-08 [2] CRAN (R 4.0.1) #> curl 4.3 2019-12-02 [2] CRAN (R 4.0.1) #> DBI 1.1.1 2021-01-15 [2] CRAN (R 4.0.1) #> dbplyr 2.1.0 2021-02-03 [2] CRAN (R 4.0.1) #> digest 0.6.27 2020-10-24 [2] CRAN (R 4.0.1) #> dplyr * 1.0.7 2021-06-18 [1] CRAN (R 4.0.1) #> ellipsis 0.3.1 2020-05-15 [2] CRAN (R 4.0.1) #> evaluate 0.14 2019-05-28 [2] CRAN (R 4.0.1) #> fansi 0.4.2 2021-01-15 [2] CRAN (R 4.0.1) #> farver 2.1.0 2021-02-28 [2] CRAN (R 4.0.1) #> forcats * 0.5.1 2021-01-27 [2] CRAN (R 4.0.1) #> fs 1.5.0 2020-07-31 [2] CRAN (R 4.0.1) #> generics 0.1.0 2020-10-31 [2] CRAN (R 4.0.1) #> ggplot2 * 3.3.3 2020-12-30 [2] CRAN (R 4.0.1) #> glue 1.4.2 2020-08-27 [2] CRAN (R 4.0.1) #> gtable 0.3.0 2019-03-25 [2] CRAN (R 4.0.1) #> haven 2.3.1 2020-06-01 [2] CRAN (R 4.0.1) #> highr 0.9 2021-04-16 [1] CRAN (R 4.0.1) #> hms 1.0.0 2021-01-13 [2] CRAN (R 4.0.1) #> htmltools 0.5.1.1 2021-01-22 [2] CRAN (R 4.0.1) #> httr 1.4.2 2020-07-20 [2] CRAN (R 4.0.1) #> jsonlite 1.7.2 2020-12-09 [2] CRAN (R 4.0.1) #> knitr 1.33 2021-04-24 [1] CRAN (R 4.0.1) #> labeling 0.4.2 2020-10-20 [2] CRAN (R 4.0.1) #> lifecycle 1.0.0 2021-02-15 [2] CRAN (R 4.0.1) #> lubridate 1.7.10 2021-02-26 [2] CRAN (R 4.0.1) #> magrittr 2.0.1 2020-11-17 [1] CRAN (R 4.0.1) #> mime 0.11 2021-06-23 [1] CRAN (R 4.0.1) #> modelr 0.1.8 2020-05-19 [2] CRAN (R 4.0.1) #> munsell 0.5.0 2018-06-12 [2] CRAN (R 4.0.1) #> pillar 1.5.1 2021-03-05 [2] CRAN (R 4.0.1) #> pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.0.1) #> purrr * 0.3.4 2020-04-17 [2] CRAN (R 4.0.1) #> R6 2.5.0 2020-10-28 [2] CRAN (R 4.0.1) #> rcfss * 0.2.1 2021-06-24 [1] Github (uc-cfss/rcfss@5b60f61) #> Rcpp 1.0.6 2021-01-15 [2] CRAN (R 4.0.1) #> readr * 1.4.0 2020-10-05 [2] CRAN (R 4.0.1) #> readxl 1.3.1 2019-03-13 [2] CRAN (R 4.0.1) #> reprex 2.0.0 2021-04-02 [1] CRAN (R 4.0.1) #> rlang 0.4.11 2021-04-30 [1] CRAN (R 4.0.1) #> rmarkdown 2.9 2021-06-15 [1] CRAN (R 4.0.1) #> rstudioapi 0.13 2020-11-12 [2] CRAN (R 4.0.1) #> rvest 1.0.0 2021-03-09 [2] CRAN (R 4.0.1) #> scales 1.1.1 2020-05-11 [2] CRAN (R 4.0.1) #> sessioninfo 1.1.1 2018-11-05 [2] CRAN (R 4.0.1) #> stringi 1.6.2 2021-05-17 [1] CRAN (R 4.0.1) #> stringr * 1.4.0 2019-02-10 [2] CRAN (R 4.0.1) #> styler 1.4.1 2021-03-30 [2] CRAN (R 4.0.1) #> tibble * 3.1.0 2021-02-25 [2] CRAN (R 4.0.1) #> tidyr * 1.1.3 2021-03-03 [2] CRAN (R 4.0.1) #> tidyselect 1.1.0 2020-05-11 [2] CRAN (R 4.0.1) #> tidyverse * 1.3.0 2019-11-21 [2] CRAN (R 4.0.1) #> utf8 1.2.1 2021-03-12 [2] CRAN (R 4.0.1) #> vctrs 0.3.7 2021-03-29 [2] CRAN (R 4.0.1) #> withr 2.4.2 2021-04-18 [1] CRAN (R 4.0.1) #> xfun 0.24 2021-06-15 [1] CRAN (R 4.0.1) #> xml2 1.3.2 2020-04-23 [2] CRAN (R 4.0.1) #> yaml 2.2.1 2020-02-01 [2] CRAN (R 4.0.1) #> #> [1] /home/anabellxu/R/x86_64-pc-linux-gnu-library/4.0 #> [2] /opt/R/4.0.1/lib/R/library ```
mjparness commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/181

library(tidyverse)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using forcats::fct_infreq() and using the raw data for plotting
mass_shootings %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = fct_infreq(race))) +
  geom_bar() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )

Created on 2021-07-13 by the reprex package (v2.0.0)

Session info ``` r sessioninfo::session_info() #> ─ Session info ─────────────────────────────────────────────────────────────── #> setting value #> version R version 4.0.1 (2020-06-06) #> os Red Hat Enterprise Linux 8.4 (Ootpa) #> system x86_64, linux-gnu #> ui X11 #> language (EN) #> collate en_US.UTF-8 #> ctype en_US.UTF-8 #> tz America/Chicago #> date 2021-07-13 #> #> ─ Packages ─────────────────────────────────────────────────────────────────── #> package * version date lib source #> assertthat 0.2.1 2019-03-21 [2] CRAN (R 4.0.1) #> backports 1.2.1 2020-12-09 [2] CRAN (R 4.0.1) #> broom 0.7.5 2021-02-19 [2] CRAN (R 4.0.1) #> cellranger 1.1.0 2016-07-27 [2] CRAN (R 4.0.1) #> cli 2.5.0 2021-04-26 [1] CRAN (R 4.0.1) #> colorspace 2.0-0 2020-11-11 [2] CRAN (R 4.0.1) #> crayon 1.4.1 2021-02-08 [2] CRAN (R 4.0.1) #> curl 4.3 2019-12-02 [2] CRAN (R 4.0.1) #> DBI 1.1.1 2021-01-15 [2] CRAN (R 4.0.1) #> dbplyr 2.1.0 2021-02-03 [2] CRAN (R 4.0.1) #> digest 0.6.27 2020-10-24 [2] CRAN (R 4.0.1) #> dplyr * 1.0.5 2021-03-05 [2] CRAN (R 4.0.1) #> ellipsis 0.3.1 2020-05-15 [2] CRAN (R 4.0.1) #> evaluate 0.14 2019-05-28 [2] CRAN (R 4.0.1) #> fansi 0.4.2 2021-01-15 [2] CRAN (R 4.0.1) #> farver 2.1.0 2021-02-28 [2] CRAN (R 4.0.1) #> forcats * 0.5.1 2021-01-27 [2] CRAN (R 4.0.1) #> fs 1.5.0 2020-07-31 [2] CRAN (R 4.0.1) #> generics 0.1.0 2020-10-31 [2] CRAN (R 4.0.1) #> ggplot2 * 3.3.3 2020-12-30 [2] CRAN (R 4.0.1) #> glue 1.4.2 2020-08-27 [2] CRAN (R 4.0.1) #> gtable 0.3.0 2019-03-25 [2] CRAN (R 4.0.1) #> haven 2.3.1 2020-06-01 [2] CRAN (R 4.0.1) #> highr 0.8 2019-03-20 [2] CRAN (R 4.0.1) #> hms 1.0.0 2021-01-13 [2] CRAN (R 4.0.1) #> htmltools 0.5.1.1 2021-01-22 [2] CRAN (R 4.0.1) #> httr 1.4.2 2020-07-20 [2] CRAN (R 4.0.1) #> jsonlite 1.7.2 2020-12-09 [2] CRAN (R 4.0.1) #> knitr 1.31 2021-01-27 [2] CRAN (R 4.0.1) #> labeling 0.4.2 2020-10-20 [2] CRAN (R 4.0.1) #> lifecycle 1.0.0 2021-02-15 [2] CRAN (R 4.0.1) #> lubridate 1.7.10 2021-02-26 [2] CRAN (R 4.0.1) #> magrittr 2.0.1 2020-11-17 [2] CRAN (R 4.0.1) #> mime 0.10 2021-02-13 [2] CRAN (R 4.0.1) #> modelr 0.1.8 2020-05-19 [2] CRAN (R 4.0.1) #> munsell 0.5.0 2018-06-12 [2] CRAN (R 4.0.1) #> pillar 1.5.1 2021-03-05 [2] CRAN (R 4.0.1) #> pkgconfig 2.0.3 2019-09-22 [2] CRAN (R 4.0.1) #> purrr * 0.3.4 2020-04-17 [2] CRAN (R 4.0.1) #> R6 2.5.0 2020-10-28 [2] CRAN (R 4.0.1) #> rcfss * 0.2.1 2021-06-23 [1] Github (uc-cfss/rcfss@5b60f61) #> Rcpp 1.0.6 2021-01-15 [2] CRAN (R 4.0.1) #> readr * 1.4.0 2020-10-05 [2] CRAN (R 4.0.1) #> readxl 1.3.1 2019-03-13 [2] CRAN (R 4.0.1) #> reprex 2.0.0 2021-04-02 [2] CRAN (R 4.0.1) #> rlang 0.4.11 2021-04-30 [1] CRAN (R 4.0.1) #> rmarkdown 2.7 2021-02-19 [2] CRAN (R 4.0.1) #> rstudioapi 0.13 2020-11-12 [2] CRAN (R 4.0.1) #> rvest 1.0.0 2021-03-09 [2] CRAN (R 4.0.1) #> scales 1.1.1 2020-05-11 [2] CRAN (R 4.0.1) #> sessioninfo 1.1.1 2018-11-05 [2] CRAN (R 4.0.1) #> stringi 1.5.3 2020-09-09 [2] CRAN (R 4.0.1) #> stringr * 1.4.0 2019-02-10 [2] CRAN (R 4.0.1) #> styler 1.4.1 2021-03-30 [2] CRAN (R 4.0.1) #> tibble * 3.1.0 2021-02-25 [2] CRAN (R 4.0.1) #> tidyr * 1.1.3 2021-03-03 [2] CRAN (R 4.0.1) #> tidyselect 1.1.0 2020-05-11 [2] CRAN (R 4.0.1) #> tidyverse * 1.3.0 2019-11-21 [2] CRAN (R 4.0.1) #> utf8 1.2.1 2021-03-12 [2] CRAN (R 4.0.1) #> vctrs 0.3.7 2021-03-29 [2] CRAN (R 4.0.1) #> withr 2.4.1 2021-01-26 [2] CRAN (R 4.0.1) #> xfun 0.22 2021-03-11 [2] CRAN (R 4.0.1) #> xml2 1.3.2 2020-04-23 [2] CRAN (R 4.0.1) #> yaml 2.2.1 2020-02-01 [2] CRAN (R 4.0.1) #> #> [1] /home/mjparness/R/x86_64-pc-linux-gnu-library/4.0 #> [2] /opt/R/4.0.1/lib/R/library ```
ktakaira commented 3 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/Discussion/issues/180

library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 x 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): could not find function "drop_na"

Created on 2021-07-13 by the reprex package (v2.0.0)

TingxiLiu commented 2 years ago
## Copy the code below to generate a reproducible example
## using the reprex package. Once you generate it, post it on
## https://github.com/uc-cfss/reproducible-examples-and-git/issues/1

library(dplyr)
#> 
#> Attaching package:'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)

# get data from rcfss package
# install latest version if not already installed
# devtools::install_github("uc-cfss/rcfss")
library(rcfss)

# load the data
data("mass_shootings")
mass_shootings
#> # A tibble: 114 × 14
#>    case     year month   day location summary   fatalities injured total_victims
#>    <chr>   <dbl> <chr> <int> <chr>    <chr>          <dbl>   <dbl>         <dbl>
#>  1 Dayton…  2019 Aug       4 Dayton,… "PENDING"          9      27            36
#>  2 El Pas…  2019 Aug       3 El Paso… "PENDING"         20      26            46
#>  3 Gilroy…  2019 Jul      28 Gilroy,… "Santino…          3      12            15
#>  4 Virgin…  2019 May      31 Virgini… "DeWayne…         12       4            16
#>  5 Harry …  2019 Feb      15 Aurora,… "Gary Ma…          5       6            11
#>  6 Pennsy…  2019 Jan      24 State C… "Jordan …          3       1             4
#>  7 SunTru…  2019 Jan      23 Sebring… "Zephen …          5       0             5
#>  8 Mercy …  2018 Nov      19 Chicago… "Juan Lo…          3       0             3
#>  9 Thousa…  2018 Nov       7 Thousan… "Ian Dav…         12      22            34
#> 10 Tree o…  2018 Oct      27 Pittsbu… "Robert …         11       6            17
#> # … with 104 more rows, and 5 more variables: location_type <chr>, male <lgl>,
#> #   age_of_shooter <dbl>, race <chr>, prior_mental_illness <chr>

# Generate a bar chart that identifies the number of mass shooters
# associated with each race category. The bars should be sorted
# from highest to lowest.

# using reorder() and aggregating the data before plotting
mass_shootings %>%
  count(race) %>%
  drop_na(race) %>%
  ggplot(mapping = aes(x = reorder(race, -n), y = n)) +
  geom_col() +
  labs(
    title = "Mass shootings in the United States (1982-2019)",
    x = "Race of perpetrator",
    y = "Number of incidents"
  )
#> Error in drop_na(., race): 没有"drop_na"这个函数

Created on 2021-11-04 by the reprex package (v2.0.1)