Skip to content
New course: Interactive Dashboards with Shiny. Get 50% off with coupon SHINYLAUNCH.
R for the Rest of Us Logo

Pipe Data Into ggplot

This lesson is locked

Get access to all lessons in this course.

Transcript

Click on the transcript to go to that point in the video. Please note that transcripts are auto generated and may contain minor inaccuracies.

Your Turn

  1. Create a new RMarkdown document

  2. Create a data frame called enrollment_by_race_ethnicity by reading in your race/ethnicity data from the data wrangling and analysis section using the read_rds() function

  3. Pipe your data into a bar chart that shows the breakdown of race/ethnicity among students in Beaverton SD 48J in 2018-2019

Your chart should look like this:

Have any questions? Put them below and we will help you out!

You need to be signed-in to comment on this post. Login.

Allison Brenner

Allison Brenner

May 7, 2021

I can't seem to load the rds file and I'm not sure why. The data and RMarkdown file are saved in the Oregon Schools project. Here is my code and error message:

enrollment_data <- 
  read_rds("/data/enrollment_race_ethnicity.rds")

Error in gzfile(file, "rb") : cannot open the connection In addition: Warning message: In gzfile(file, "rb") : cannot open compressed file '/data/enrollment_race_ethnicity.rds', probable reason 'No such file or directory'

Allison Brenner

Allison Brenner

May 7, 2021

Also - I did load tidyverse package.

David Keyes

David Keyes

May 8, 2021

Try removing the / before data:

enrollment_data <- read_rds("data/enrollment_race_ethnicity.rds")

Let me know if that works!

Atlang Mompe

Atlang Mompe

June 23, 2021

I run this code and get this error message, what am I doing wrong?

enrollment_by_race_ethnicity %&gt;% 
  filter(district == "Beaverton SD 48J") %&gt;% 
  filter(year == "2018-2019") %&gt;% 
  ggplot(aes(x = percent_of_total_at_school,
             y = race_ethnicity)) +
  geom_col()

Error in filter(., district == "Beaverton SD 48J") : object 'enrollment_by_race_ethnicity' not found

Please advise - I was able to read the rds data and I can see it in the environment pane, but when I use the same code that you shared, I get an error, it says object not found, even though I can see the data on the environment frame.

Atlang Mompe

Atlang Mompe

June 23, 2021

Hi David - please ignore, I had misspelled ethnicity before when reading the rds file, yes I reviewed the code again and I am happy I figured the issue out. Thank you, Atty.

Atlang Mompe

Atlang Mompe

June 23, 2021

Hi David, actually I need your help again, I was able to run the data last night, but I just tried again this morning and this is the error message that I get: Show in New Window Error in gzfile(file, "rb") : cannot open the connection 3. gzfile(file, "rb") 2. readRDS(file) 1. read_rds("data/enrollment-by-race-ethnicity.rds")

  1. I loaded the tidyverse data: library(tidyverse)
  2. I tried to create the object use this code: enrollment_by_race_ethnicity <- read_rds("data/enrollment-by-race-ethnicity.rds")

So I am not sure what I am doing wrong.

David Keyes

David Keyes

June 23, 2021

This almost certainly means that there isn't a file in the location you're attempting to access one in. Are you sure you spelled everything correctly? Also, are you working in an R script file or RMarkdown? If it's RMarkdown, is your file in the root directory of your project? If it's in a subfolder, that may be causing the issue.

Atlang Mompe

Atlang Mompe

June 23, 2021

Hi David, it turns out I had saved the RMarkdown file in a different folder in my external drive, thank you. It works now.

Mychal Davis

Mychal Davis

May 19, 2022

Hello,

Which week did we make the RDS file that we are supposed to use for this assignment?

Thank you, Mychal

David Keyes

David Keyes

May 19, 2022

It was in the Exporting Data lesson.

Hi -

I get the x-y axis (labeled appropriately) but no data plotted......and no error messages.....

Charlie Hadley

Charlie Hadley

November 16, 2022

Hi Amy,

Sorry it's not working. Could you copy and paste your code?

Thanks. Charlie

Kirstin O'Dell

Kirstin O'Dell

November 22, 2022

I'm having the same issue. I see a graph area with the x and y labels but no bars. Here is the code I'm using enrollment_by_race_ethnicity %>% filter(district == "Beaverton SD 48J") %>% filter(year == "2018-2019") %>% ggplot(aes(x = percent_student_body, y = race_ethnicity)) + geom_col()

I believe you need the x variable to be percent_of_total_at_school, not percent_student_body. Let me know if that helps!

Kirstin O'Dell

Kirstin O'Dell

November 22, 2022

I think that's the same variable. I remember noticing that I had named that one differently than what was in the assignment.

Kirstin O'Dell

Kirstin O'Dell

November 22, 2022

I tried running just the first filter and got this error message (I only have tidyverse loaded): Error in enrollment_by_race_ethnicity %>% filter(year == "2018-2019") : could not find function "%>%"

Kirstin O'Dell

Kirstin O'Dell

November 22, 2022

Got the filters to work! I must have done something that required a reload of tidyverse. Still having same issue with the graph not showing though.

Ruimin Xu

Ruimin Xu

May 30, 2023

Hi David, I got blank chart when running below code... Not sure what is wrong enrollment_by_race_ethinicity %>% filter(year=="2018_2019") %>% filter(district=="Beaverton SD 48J") %>% mutate(percent_of_total_at_school=percentage_of_students) %>%

view()

ggplot(aes(x=percent_of_total_at_school,y=race_ethinicity)) geom_col()

David Keyes

David Keyes

May 30, 2023

Can you please post your full code file as a gist and share the link so I can debug?