Skip to content
R for the Rest of Us Logo

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.

FYI, as you go through this lesson, I only begin to touch on working with the theme() function to customize plots, as I only really want to focus on teaching you how to remove things using element_blank(). However, there is a ton to learn here so definitely check out the Learn More section below!

Your Turn

Use some combination of:

Do the following:

  1. Remove gray background

  2. Remove axis titles

  3. Remove or minimize grid lines

Learn More

Editing ggplot theme elements can be confusing. You might consider checking out the ggeasy package, which aims to make doing common things much simpler.

Finally, if you’re confused by the idea of “complete” themes, the best description of them comes from the ggplot website:

These are complete themes which control all non-data display. Use theme() if you just need to tweak the display of an existing theme.

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

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

Jodi Fender

Jodi Fender

May 11, 2021

I have a font I use for a project of mine (various from Whitney family), but it was stating font family not found in Windows font database. But...now every time I run the code for the new graph (not specifying any font) but using hrbrthemes/theme_ipsum I get, Warning (yellow triangle) font family not found in Windows font database font family not found in Windows font database font family not found in Windows font database font family not found in Windows font database font family not found in Windows font database font family not found in Windows font database font family not found in Windows font database font family not found in Windows font database

David Keyes

David Keyes

May 11, 2021

Does this happen if you use theme_ipsum(base_family = "Whitney")? theme_ipsum() uses Arial Narrow by default so if you don't have that installed on your system, it will throw an error. Also, when you say "various from Whitney family" are those fonts named something other than "Whitney" (e.g. "Whitney Bold")? If so, you'll need to call them by their exact name.

Can you also try the approach mentioned in this video I made on recent updates to working with custom fonts and let me know if it works? I think installing the ragg and systemfonts package should help immensely, but I've only tested this on my machine so let me know if it works for you.

Jody Oconnor

Jody Oconnor

May 11, 2021

Missing font families came up in a previous lesson, but was put off for later. Now that we're back making charts pretty, I'd like to know how to access specific fonts (maybe this is covered in a later lesson...?)

When I run: theme(axis.title.x = element_blank(), axis.text = element_text(family = "ariel", size = 15),

within ggplot, I get a chart but the font seems to be the default still, and the error message shows at the top (regardless of the font name I include): Warning font family not found in Windows font database font family not found in Windows font database font family not found in Windows font database font family not found in Windows font database

David Keyes

David Keyes

May 11, 2021

Definitely! A couple things for you:

  1. You've misspelled Arial so R won't be able to find it. Also, don't forget the R is case sensitive so arial won't work while Arial will.

  2. Can you try the approach mentioned in this video I made on recent updates to working with custom fonts and let me know if it works? I think installing the ragg and systemfonts package should help immensely, but I've only tested this on my machine so let me know if it works for you.

Jody Oconnor

Jody Oconnor

May 12, 2021

Thanks David. I installed ragg and systemfonts (and spelled Arial correctly with a capital A ;))

But I get the same error and the same default font :(

David Keyes

David Keyes

May 13, 2021

Hmm, that's very odd. Can you try these two things:

  1. Load ragg and systemfonts (e.g. library(ragg)) and try again.
  2. Try a different font and see if that makes any difference.

Jody Oconnor

Jody Oconnor

May 13, 2021

I restarted R Studio, then loaded ragg and systemfonts and ran the code again with the same error (I tried 3 different fonts -> same error).

These are the packages I loaded, and the font related messages that resulted: library(tidyverse) library(hrbrthemes) library(systemfonts) library(ragg) library(scales)

package �hrbrthemes� was built under R version 4.0.4Registered S3 methods overwritten by 'htmltools': method from
print.html tools:rstudio print.shiny.tag tools:rstudio print.shiny.tag.list tools:rstudio Registering Windows fonts with R NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes. Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow package �systemfonts� was built under R version 4.0.4package �ragg� was built under R version 4.0.5

So I ran the following: hrbrthemes::import_roboto_condensed()

resulting in the following message: You will likely need to install these fonts on your system as well. You can find them in [C:/Users/Ryan/Documents/R/win-library/4.0/hrbrthemes/fonts/roboto-condensed]

Jody Oconnor

Jody Oconnor

May 13, 2021

I installed Roboto Condensed onto my computer, re-started the R Studio session. Then ran the code using Roboto Condensed, and also with Arial and with Arial Narrow, and Georgia. The error remains the same :(

Jody Oconnor

Jody Oconnor

May 15, 2021

ok, something worked (wahoo!)

notes:

  • library(extrafonts) didn't work, but I discovered the 'extrafont' package which did work (I assume the 's' was a typo)

  • this seems to have worked: font_import(pattern = "Roboto") windowsFonts(Roboto Condensed = windowsFont("Roboto Condensed"))

using theme_ipsum_rc() worked with the following code: theme_ipsum_rc() + theme(axis.title.x = element_blank(), axis.text = element_text(family = "Roboto Condensed", size = 15) The 'font family not found in Windows font database' error no longer appears. I was able to import Georgia as well using: font_import(pattern = "Georgia") windowsFonts(Georgia = windowsFont("Georgia"))

using both theme_ipsum_rc() and theme_ipsum() the axis text will show up as 'Georgia' now, but with theme_ipsum() I still get the error messages and with theme_ipsum_rc() there is no font error message above the plot. Wierd.

Oindrila Bhattacharyya

Oindrila Bhattacharyya

May 13, 2021

When I try running library(ragg), it says "there is no package called 'ragg' ". Can you tell me why? Also I am getting the same error messages for font like others.

Oindrila Bhattacharyya

Oindrila Bhattacharyya

May 13, 2021

Btw the installing ragg worked! But I still get the font issue

David Keyes

David Keyes

May 14, 2021

Ok, one more idea for you. Can you try this please (this is a Windows-specific solution; if you're not on Windows, please let me know):

library(extrafont)
font_import(pattern = "Roboto")
windowsFonts(`Roboto Condensed` = windowsFont("Roboto Condensed"))

Then, try to use the theme_ipsum_rc() function and let me know if it works.

I was playing with hrbrthemes and was trying to manipulate the justification of the axis titles in theme_ipsum.
in the ?theme_ipsum Help file it returns: "axis_title_just axis title font justification, one of [blmcrt]"

?blmcrt said there’s not documentation. Googling blmcrt didn’t reveal anything. Setting axis_title_just = FALSE made it centered, not right justified. but the way [blmcrt] is shared in the help file, it seems like that refers to something fundamental that I should know.

David Keyes

David Keyes

December 6, 2021

Yeah, it's not the clearest. Here's what it means:

b: bottom l: left m: middle (vertical middle) c: center (horizontal center) r: right t: top

Hope that helps!

I haven't been able to resolve the font issue yet. I have a Mac. I installed the Whitney font that someone mentioned above to test. I installed ragg and systemfont. I made sure to install Whitney to the computer and not just the user (which at first I thought was the problem). I can switch between fonts that were already installed like Georgia, Gill Sans, etc. But if i try theme_ipsum(base_family = "Whitney") I get the error:

Warning in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : no font could be found for family "Whitney" Error in grid.Call(C_textBounds, as.graphicsAnnot(x$label), x$x, x$y, : polygon edge not found

Charlie Hadley

Charlie Hadley

February 7, 2022

Hi Sara, sorry you're still having issues.

Can I check if you've tried the steps in this video from David?

It's an 8min video I've condensed to these five steps:

  1. RStudio menubar: Tools > Global Options > Graphics > Backend: RAGG
  2. Download Whitney font from here
  3. Install the font by dragging into Fontbook and completing font validation
  4. Restart your RStudio session
  5. Load the {systemfont} package before your code, as in this example.

Let me know if this doesn't work and we can explore other options.

Cheers, Charlie

Hi - yes I did the steps as outlined. I removed the Whitney I had installed and used the one you linked. On first install, it worked but the "regular" or default Whitney was some kind of handwritten font and it did not pass validation. The rest did but I removed the "regular" one, ran again, and it stopped working. Tried another website but couldn't find the same family or the file didn't validate. I'm beginning to wonder if it's a font issue. I downloaded Roboto Condensed and it works fine but another one I tried (Quicksand) did not.

Charlie Hadley

Charlie Hadley

February 8, 2022

My understanding of the way fonts works in macOS is that the whole family needs to be installed for them to work correctly - so removing the "Regular" font will explain why it stopped working. I'm not sure why the font looks handwritten on your machine - is that only inside of RStudio? If you use ggsave() do you get the same issue?

Charlie and I have been working on figuring this out. It does sound like a specific font issue. I recorded a quick video to show you what I think you should try.

The code I used in the video is systemfonts::system_fonts() %>% view()

Let me know if that helps at all!

Hey guys - thanks for sticking with me on this. With the help of your code above, I think I have figured out the issue(s). I downloaded and installed several other test fonts and then ran the code above to see what R thinks I have for fonts. I made a couple discoveries. Again, this is on a Mac so I don't know what Windows will do. First, the font has to be installed as a system font, not just for the user. So they are in /system/library/fonts or /system/library/fonts/supplemental or just /library/fonts. I have a couple that are in my user profile from Adobe Cloud I think, and those don't work in ggplot. I guess since we ran the systemfonts library it can't see any others?

Secondly, I think only .ttf fonts will work and not .otf. The random fonts I downloaded happened to be .ttf and they worked. I downloaded Whitney from two places and both came as .otf and neither worked. The one Whitney "regular" file came as a .ttf but it did not validate in the Font Book which is why it looked weird as also wasn't picked up by systemfonts (didn't show up in the list). Maybe you guys can test this some more but I think for now it's mostly resolved for me so at least I know what will work!

** But yes, the font names are case sensitive which I found out as well

Hi could you please let me know whether my understanding here is correct?

  • We could use a combination of themes (eg: minimal, ipsum & custom/theme()). There is no limit. In that case, could this somehow cause conflict in some elements?
  • Standard themes like 'minimal' or 'classic' are not customizable in the function itself (eg: theme_minimal(axis.title = element_blank()) would throw an error.
  • Additional customization should be done in the 'custom' theme() function.
  • We can customize specific elements within the theme_ipsum() function, because it's not from the ggplot standard themes.

David Keyes

David Keyes

January 17, 2023

Yes, that's exactly right. A few notes:

  • If you use multiple themes, there won't be conflicts, but the last item you list will take override other customizations.
  • You can't add theme elements like axis.title within complete themes like theme_minimal(). However, complete themes do have some arguments (e.g. base size). Use ?theme_minimal() to see those arguments.
  • You can customize theme_ipsum() within that function because, behind the scenes, it actually applies theme_minimal() and then any theme() elements.

Let me know if all of that makes sense!

Jeremy Danz

Jeremy Danz

February 6, 2023

Hmm, any idea why when using theme_ipsum, and setting the axis_title_size = 0, it looks like the axis titles are still there, but just very very small?

When using theme_minimal and theme(axis.title = element_blank()) the axis titles appear gone completely.

Here's a screenshot of what I'm getting with theme_ipsum https://imgur.com/eZufC8N

I appear to be getting the same font issue that others are getting, which looks fixable, but also not particularly awful if it isn't fixed? everything seems to work still, it just throws an unpleasant looking error message?

thanks!

David Keyes

David Keyes

February 6, 2023

Yeah, I've had this same issue. I'm not quite sure why, but I usually just use the theme code you showed to remove them.

On the fonts issue, take a look at the Customizing Fonts lesson. There have been some changes to how fonts work in ggplot. Once you are using the new ragg and systemfonts packages, you can try the hrbrragg package. Alternatively, you can just use theme_minimal() and customize it further (I mostly do this).

Jeremy Danz

Jeremy Danz

February 7, 2023

ok, thanks very much!! :)

Zain Asaf

Zain Asaf

June 12, 2023

I have attempted to remove grid lines using the following line of code: pannel.grid.minor.y = element_blank()) However, I get the following error message: Error in mapply(): ! The pannel.grid.minor.y theme element is not defined in the element hierarchy.

David Keyes

David Keyes

June 12, 2023

Can you share your full code please?

Zain Asaf

Zain Asaf

June 12, 2023

enrollment_by_race_ethnicity %>% filter(ethnicity=="latino") %>% ggplot(aes(x=year, y =pct, group=district_id)) + geom_point() + geom_line () + theme_minimal() + theme(axis.title = element_blank(), pannel.grid.minor.y = element_blank())

David Keyes

David Keyes

June 13, 2023

I think you misspelled panel (you have two n's). :)

Zain Asaf

Zain Asaf

June 13, 2023

That worked, thanks!