Trying to pipe data into ggplot

  • Trying to pipe data into ggplot

     David updated 6 months ago 2 Members · 5 Posts
  • stacy.kim

    Member
    October 16, 2020 at 12:42 pm

    I’m stuck on “your turn” for topic 3 lesson 3. I can’t figure out why I get a blank chart–no bars. I copied the solution into my rmd file. I’ve looked at the data. I can’t figure it out? I’m probably overlooking something simple…. Help!

  • David

    Administrator
    October 16, 2020 at 3:38 pm

    Do you mind posting your code here? That should help me to help you!

    • stacy.kim

      Member
      October 16, 2020 at 4:02 pm

      Thanks David. I should have thought to do that.

      This is what is in my RMD file:

      {r}</p><p>library(tidyverse)</p><p>

      {r}</p><p>enrollment_by_race_ethnicity <- </p><p> read_rds(path = "data/enrollment-by-race-ethnicity.rds") </p><p>

      {r}</p><p>enrollment_by_race_ethnicity %>% </p><p> filter(District == "Beaverton SD 48J") %>% </p><p> filter(year == "2018-2019") %>% </p><p> ggplot(aes(x = percent_of_total_at_school,</p><p> y = race_ethnicity)) +</p><p> geom_col()</p><p>

      This is what is in my R script from the data wrangling section that created the RDS file.

      library(tidyverse)

      library(readxl)

      library(janitor)

      enrollment_17_18 <- read_excel(“data-raw/enrollment-17-18.xlsx”)

      enrollment_18_19 <- read_excel(“data-raw/enrollment-18-19.xlsx”)

      oregon_districts <- read_excel(“data-raw/oregon-districts.xlsx”)

      clean_enrollment_data <- function(raw_data, data_year, remove_text) {

      raw_data %>%

      select(-contains(“grade”)) %>%

      select(-contains(“percent”)) %>%

      select(-contains(“kindergarten”)) %>%

      pivot_longer(cols = -district_id,

      names_to = “race_ethnicity”,

      values_to = “number_of_students”) %>%

      mutate(number_of_students = na_if(number_of_students,”-“)) %>%

      mutate(number_of_students = replace_na(number_of_students,0)) %>%

      mutate(number_of_students = as.numeric(number_of_students)) %>%

      mutate(race_ethnicity=str_remove(race_ethnicity,remove_text)) %>%

      mutate(race_ethnicity= case_when(

      race_ethnicity==”american_indian_alaska_native”~”Native”,

      race_ethnicity==”asian”~”Asian”,

      race_ethnicity==”native_hawaiian_pacific_islander”~”Pacific Islander”,

      race_ethnicity==”black_african_american”~ “Black”,

      race_ethnicity==”white”~”White”,

      race_ethnicity==”hispanic_latino”~”Hispanic”,

      race_ethnicity==”multiracial”~”Multi-Racial”

      )) %>%

      group_by (district_id) %>%

      mutate(pct=number_of_students/sum(number_of_students)) %>%

      ungroup() %>%

      mutate(year = data_year)

      }

      enrollment_by_race_ethnicity_17_18 <- clean_enrollment_data(enrollment_17_18,

      “2017-18”,

      “x2017_18_”)

      enrollment_by_race_ethnicity_18_19 <- clean_enrollment_data(enrollment_18_19,

      “2018-19”,

      “x2018_19_”)

      enrollment_by_race_ethnicity <- bind_rows(enrollment_by_race_ethnicity_17_18,

      enrollment_by_race_ethnicity_18_19) %>%

      left_join(oregon_districts,

      by = c(“district_id” = “Attending District Institutional ID”)) %>%

      rename(percent_of_total_at_school = pct) %>%

      select(district_id, District, everything())

      write_rds(enrollment_by_race_ethnicity,

      path = “data/enrollment-by-race-ethnicity.rds”)

  • stacy.kim

    Member
    October 21, 2020 at 2:47 pm

    I figured it out. I needed to change the second filter from “2018-2019” to “2018-19”. I guess my dataset is a little different from yours.

    • David

      Administrator
      October 23, 2020 at 9:57 am

      Sorry I didn’t respond (I’m still figuring out the notifications), but glad you got it worked out!

Log in to reply.

Original Post
0 of 0 posts June 2018
Now