Get access to all lessons in this course.
-
Advanced Data Wrangling and Analysis
- Overview
- Importing Data
- Tidy Data
- Reshaping Data
- Dealing with Missing Data
- Changing Variable Types
- Advanced Variable Creation
- Advanced Summarizing
- Binding Data Frames
- Functions
- Merging Data
- Renaming Variables
- Quick Interlude to Reorganize our Code
- Exporting Data
-
Advanced Data Visualization
- Data Visualization Best Practices
- Tidy Data
- Pipe Data Into ggplot
- Reorder Plots to Highlight Findings
- Line Charts
- Use Color to Highlight Findings
- Declutter
- Use the scales Package for Nicely Formatted Values
- Use Direct Labeling
- Use Axis Text Wisely
- Use Titles to Highlight Findings
- Use Color in Titles to Highlight Findings
- Use Annotations to Explain
- Tweak Spacing
- Customize Your Theme
- Customize Your Fonts
- Try New Plot Types
-
Advanced RMarkdown
- Advanced Markdown Text Formatting
- Tables
- Advanced YAML
- Inline R Code
- Making Your Reports Shine: Word Edition
- Making Your Reports Shine: HTML Edition
- Making Your Reports Shine: PDF Edition
- Presentations
- Dashboards
- Other Formats
-
Wrapping Up
- You Did It!
Going Deeper with R
Use Direct Labeling
This lesson is locked
This lesson is called Use Direct Labeling, part of the Going Deeper with R course. This lesson is called Use Direct Labeling, part of the Going Deeper with R 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
Add text to display the percentage of Hispanic/Latino students in Douglas ESD in 2017-2018 and 2018-2019.
You’ll want to use case_when()
and str_glue()
. If you want some guidance with str_glue()
, check out its help page. When using this function, put everything you want to glue together in quotes, with curly brackets around variables.
You need to be signed-in to comment on this post. Login.
Vuk Sekicki
May 12, 2021
Hello David, This works well if we use one line as you said. However I was experimenting with top 3 schools. It actually does not move middle one for whatever reason. Let me share the code:
highlight_district % filter(race_ethnicity == "Hispanic/Latino") %>% filter(district == "Douglas ESD" | district == "Troy SD 54" | district == "InterMountain ESD") %>% mutate(percent_display = percent(percentage, accuracy = 1)) %>% mutate(percent_display = case_when( year == "2018_2019" ~ str_glue("{percent_display} of students"), TRUE ~ percent_display ))
total_enrolement %>% filter(race_ethnicity == "Hispanic/Latino") %>% ggplot(aes(x= year, y= percentage, group = district)) + geom_line(color = "light gray") + geom_line(data = highlight_district, aes(x= year, y= percentage, group = district, color = district)) + theme_minimal() + geom_text(data = highlight_district, inherit.aes = TRUE, aes(label = percent_display), nudge_x = c(-0.1,0.1) )
Vuk Sekicki
May 12, 2021
Actually I figured out. Error is in nudge_x = c(-0.1,0.1) It should be nudge_x = c(-0.05,-0.05,-0.05,0.25,0.25,0.25) because it has 3 pairs of 2 values. First value set for left adjustment and second for right. Neat because allows more customization.
Harold Stanislaw
May 20, 2021
I experimented with fig.width to prevent my label from getting truncated. This displayed the entire line, but it also rescaled the text so it became quite small and was no longer directly adjacent to the line it was labeling. I tried using ggfittext to wrap the text, but couldn't get it to work :(
David Keyes
May 21, 2021
Yes, size of text does scale with plot size. I've used this code in the past to get my text to be an exact size in points.
Juan Clavijo
November 30, 2021
Hello, you mentioned in the live session a package to help put graphs together, next to one another in the layout. Could you please re-share that here?
David Keyes
November 30, 2021
It's now published as a blog post. The first video is the one about those packages (patchwork and cowplot).
David Keyes
December 7, 2021
I'm not quite sure I follow your question. Can you expand a bit please?
Tarisirai Zengeni
December 23, 2021
Hi Davi,
I have been stuck for weeks now on getting this code to work.
highlight_district% filter(race_ethnicity=="Hispanic/Latino") %>% filter(district=="Douglas ESD")%>% mutate(percent_display = percent(percent_of_total_at_school, accuracy = 1))%>% mutate(percent_display = case_when( year=="2018-2019" ~ str_glue("{percent_display} Latino students"), TRUE ~ percent_display ))
I keep getting the error message:
Error: Problem with
mutate()
columnpercent_display
. ipercent_display = case_when(...)
. x unused argument (accuracy = 1) Runrlang::last_error()
to see where the error occurred.I even tried reinstalling the R suite and no change. When I try to open the highlight_district dataframe to view, I also get an error message (r error 4 (R code execution error)) with the option to retry which doesnt work - i get the same r error 4 message. i even tried starting a new markdown file but still same result. Counsel says:
Error in formatC(c(0, 13.6363636363636), format = "f", digits = 2L, accuracy = 1) : unused argument (accuracy = 1)
I am at a loss.
Tarisirai Zengeni
December 23, 2021
"Console" not "counsel"
David Keyes
December 23, 2021
Sorry you're having trouble! Can you please post your full code (all the way from where you import the Excel file) so I can see exactly where the error is occuring.
Hatem Kotb
January 15, 2023
For some reason this code throws an error, but when I switch the order of the lines in the case_when it doesn't 🙄
Code: enrollment_by_race_ethnicity % mutate(percent_display = percent(percentage, accuracy = 1)) %>% mutate(percent_display = case_when( year == "2017" ~ percent_display, year == "2018" ~ str_glue("{percent_display} Latino students")))
Error: Error in
mutate()
: ! Problem while computingpercent_display = case_when(...)
. Caused by error incase_when()
:Backtrace:
David Keyes
January 16, 2023
For some reason (I genuinely have no idea why), I have trouble with case_when() unless I put the close parentheses on its own line. Can you try that and let me know if it helps?