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 the scales Package for Nicely Formatted Values
This lesson is locked
This lesson is called Use the scales Package for Nicely Formatted Values, part of the Going Deeper with R course. This lesson is called Use the scales Package for Nicely Formatted Values, 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
Make a new variable called
percent_display
that shows thepercent_of_total_enrollment
variable as a nicely formatted percent (rounded to the nearest whole number)Make sure you save this as
highlight_district
(i.e. don’t just display the result)
Learn More
The best place to learn more about the scales
package is its documentation website. You’ll see that you can format a wide range of values using this package, including dollar values, dates, times, and more.
Dana Siedel gave a nice talk at rstudio::conf 2020 about the scales package that is well worth a 20-minute watch!
You need to be signed-in to comment on this post. Login.
Juan Clavijo
November 28, 2021
Hello,
I had already rounded my proportions and multiplied by 100 so I now have 39.4, for example. when running this I get 3940%. Is there a way to make it a 39.4% without undoing the previous rounding? Also, do I need to do this if I already have the 39.4 for graphing purposes?
David Keyes
November 28, 2021
Yup, take a look at the scale argument in the percent function. I don't use it much (see below for how I typically work), but I believe you can set the value to 100 to divide your value by 100 before converting it to a percent.
I normally keep any values that I know will be percents between 0 and 1. Then, I make a separate column for the formatted values. So, the numeric column would be called pct while the formatted column would be called pct_formatted. I use the pct for things like my y aesthetic property on my plots and the pct_formatted to add text labels. If I want to create y axis text that's formatted in percents, I use the
percent_format()
. I might add a line like:scale_y_continuous(labels = percent_format())
and that will make all y axis text show up as percents. Hope that helps!Matt M
December 5, 2021
When I run highlight_dataframe <- mutate(percent_display = percent('Percent of Total', accuracy = 1)) I get the error: Error in UseMethod("round_any") : no applicable method for 'round_any' applied to an object of class "character"
If i remove the ", accuracy = 1" I get the error: "Error in x * scale : non-numeric argument to binary operator"
but the 'Percent of Total' variable is numeric (when I mouseover, it says "numeric with range 0-1"
David Keyes
December 6, 2021
I think you're using the single quote here. As a result it's treating the text Percent of Total as text, not as a variable. Try using the ` (same key as tilde) instead and let me know if that works.
Mark Lewis
December 5, 2022
I work with a step like this pretty often, but always seem to run into some complication or other. I like the idea here of making a completely new column for the display value. I see that
scales::percent()
has been deprecated in favor ofscales::label_percent()
. Butlabel_percent()
says it's designed to be used in alabels
argument in a ggplot scale. Do you have a sense of what the consequences are (if any) of using it outside that context, like in a simplemutate()
of the sort you're doing here?David Keyes
December 5, 2022
It's a really good question and one I was talking about with a collaborator recently. I read the
percent()
docs, which say, the function will be "kept for backward compatibility." Given this, I'm comfortable using it. I think the tidyverse team sees scales only being used in ggplot, whereas I often use it in other contexts as well. Given this, I don't plan to change my usage, but you may have different needs so may want to choose your own path. Hope that helps!Mark Lewis
December 7, 2022
That makes sense, thanks!