 If you study figures in the literature long enough, you start to notice some telltale signs of how people made those figures. If you notice that something is made by ggplot2, well that's probably because they've got that ugly gray gridded background or they've got the same three default colors that everyone uses or they're using that aerial font that is pretty standard across all of figures generated in R. How do we change that font so it's a little bit more distinctive and perhaps says a little bit about your own personal style? Well keep watching and I'll show you into this episode of Code Club. If you've been following along in recent episodes of Code Club, you know the story that I found a report from Ipsos back last winter where they were looking at receptivity of people to receiving the COVID-19 vaccine. This was before the vaccine was actually available so the data is a little bit dated but I thought the figure was pretty cool. So the first step was to generate this dumbbell chart to stylize after what I saw in Ipsos but as I mentioned I actually originally saw this figure in a newsletter that came to me from Chart R so although again it's called Chart R, I don't think they use R. We are stepping through different ways of modifying my Ipsos version to the Chart R version. The next step is to modify the font on this figure. You'll notice that the title and the x-axis label where it says Chart R are serif fonts so they have those little ticks at kind of the top of the H and little lines at the bottom of the A's. Those are serifs whereas the y-axis labels those country names are sans serifs. They don't have those tick marks nor do the numbers so those are not the typical Times New Roman or aerial fonts that you usually see in an R-generated figure. How do we do that? Well you're in the right place. Before we get going too far in discussing how we can import fonts from outside of R and perhaps outside of your computer, I want to introduce a concept to you of the font family. So there are actually four different font families that come pre-installed with R and I'm going to come down in my code which by the way you can get the code I'm starting with. Add a link down below in the description. I strongly encourage you to follow along so you can make all the same mistakes I make and also have a version that you can play with to experiment with and certainly learn far more. So in the theme function I'm going to add an argument I'll call text and this is element text and I'll do family equals serif and then add a comma to that and what this should do is make my entire figure have serif fonts. Now what we see is that all of the fonts in this figure except for these number labels which we'll talk about later have a serif font. This is actually Times New Roman and you can see like the I again has that hat and the V has hats and the A at the bottom. Oh like in Australia has lines at the bottom. Those are the serifs. The default is actually Sans and again that is what we've been working with is in the world of a sans serif font. The other family to know about is mono. So this then gives us a mono spaced font which is actually the courier font and it's mono spaced because each character uses the same amount of space. Think about like a typewriter which I know you all are so young that you never typed on a typewriter but I did back in seventh grade anyway. So this is a mono spaced font and it's courier and it looks a lot like what we were typing up here in our code. This is also a mono spaced font. There's a fourth family which is a symbol but we don't have any symbols and if we were to plug in family equals symbol and run it then it would just complain and complain and complain. So these three fonts are they're okay but right they're pretty boring and they're pretty standard and they don't really allow us to express our personality perhaps as much as we like. What we're going to use instead is a package called show text. Show text will allow us to import into our fonts that we have installed on our computer or fonts that we want to grab off of the internet say from Google fonts and that's what we're going to do in the rest of today's episode. To install the show text package I'm going to come to the lower right corner in my RStudio window. Click on that packages tab and then go to install in that packages middle window. I'll go ahead and type in show text. It will also install that show text DB package that we also saw and so we'll say yes. Now we've got show text installed and at the top of my script where I have all my other library function calls I'll do library show text. Make sure I've got that loaded and it also loads two other packages sys fonts and show text DB. So as a professor I spend a lot of time writing in aerial 11 point font that's when I'm writing grant proposals or papers that's just what we seem to use but if you were to click that down arrow next to the aerial which again I never seem to do you'll see just a bajillion different fonts in here right and so all of these fonts I guess except for the ones with the cloud and the download sign are already installed on my computer and so maybe it would be nice to go ahead and use one of these fonts in our figure and so one that always seems a little bit interesting to me is this copper plate. So let's go ahead and use copper plate in our figure and see how we can use the show text package to do that. So I'm going to do a little bit of work down here in the console window so I'm going to make this a little bit bigger. I'll go ahead and clear the screen with control l so we have more real estate to work with here and so if I type font paths as a function this is a function coming to us from show text this then shows all of the paths the directories that include all of the font files that are on my computer. If you're using windows this might look a little bit different and again it'll look a little bit different on your computer but that doesn't really matter because we just need to know where the fonts are actually we don't even need to know that but show text does and so this is saying basically that show text knows where the fonts are a more useful function though is to do font underscore files open close parentheses again as another function this then outputs a data frame that has the names of all of our different fonts which is pretty cool. Okay this is a pretty big data frame with about 350 different fonts loaded on my computer I'm going to go ahead and take this and pipe it into tibble so I can see it in a little bit more attractive format so I can see the path the file the family the face the version and ps name I guess maybe that's post script what I'm going to do is I'm going to take this and I'm going to filter because I want to get the the fonts that have the word copper in it and so I'll filter using str detect and I'll do on family and what I'm going to look for is copper and again this then tells me the path the file name and the family and all that stuff for copper plate so what's important to me is that I know that the file name is copper plate ttc so to load that font I can then do font add and the family I'm going to say is copper so instead of like mono or sands or serif I'm going to say copper but then I need to tell it the name of the the regular font and so I can then say regular equals copper plate ttc now I've got copper plate loaded as the copper family and copy it and then come back up to my script and add that family and then I can do show text underscore auto and what show text auto does is it makes that copper family visible to ggplot and then I can come back down to where I had that theme function again family equals mono and let's go ahead and put in copper we now have that copper plate font for all of the fonts in our figure I know except for the numbers next to the dumbbells hold on we'll get there but what we did was that again we loaded that family copper and we're now applying it to text in theme and that's kind of the parent class of the figure of all kind of the non data elements of the figure if I wanted to make say the plot dot title to be family copper I could take that family copper and then plop that in here with my plot dot title in element text and let me go ahead and comment out the text for right now and now what we see is the title is copper plate right so again we can use this family argument in element text to modify that part of the theme of our figure now as I mentioned the number next to the dumbbell is not being rendered in copper plate and that's because this is part of the data of the of the of the number right and so the theme function changes the non data elements of the figure if we want to change this number to be of the correct font the copper plate font we can come back up to geome text and again I can do family equals copper and now our numbers next to those points are also in copper plate now I am on a mission to take the ipsos version of the chart and to convert it to this chart our version of the chart and as you'll notice the title is a serif font and the the labels and the numbers are of a sans serif font they don't exactly look like aerial like we've been using so I now want to go about showing you how I plan on matching the font in this figure as well as helping you to identify strategies to find fonts that you might use in your own work where I'm going to start is with the title and so again this is a serif font and there's a handy dandy website you may have heard of before called google and if you go to fonts.google.com this brings you to a website that has something like 1100 different families of fonts that are free and clear for us to use in our work I'm going to take this title vaccine skepticism by country and I can plug that in here in the sentence so vaccine and now I see what vaccine skepticism by country looks like in these 1100 different fonts I can filter this down a little bit by coming to this categories tab and I want serif fonts I'm going to turn off all these others and now I've got my fonts there's 228 serif fonts sorted by trending and what I'm going to do is I kind of look through here for the font that kind of matches what we saw in the chart our version of the of the plot and let's see this ah this looks very familiar let me bring up our version and kind of put it next to it this does look pretty similar right you notice the a has a little tail on the bottom and looks pretty pretty similar the e um is that a slope all these parts of the fonts I know have technical names but I'm just the slope in the e the cross piece in the e matches there so Josephine slab I think that's the font we want so again we can click on that and then I can download the whole family I can download and install this font onto my computer this now is the directory Josephine underscore slab and I can go ahead and I'll take this Josephine slab variable font and go ahead and install that font I'll go ahead and also get the italic font I don't know that I really need it so now Josephine slab is installed for our use with our figure but the first thing we need to do way back up here was install Josephine slab as its own family so I'm going to call it Josephine I want to make sure I know what the file was that Josephine slab came to us in we'll take font files pipe that to tibble the tibble isn't really necessary but it makes things look nice and I'll do filter and then str detect and we will then say on the family column I'll do Josephine and I see now that I've got not enough space I'll go ahead and take that line again and do a poll on the file column and I don't want the function I just want the name file and so now I see I've got Josephine slab variable font wgh t I'm pretty sure that's what I want I don't want the italic version and again I can now plop that into here where I have regular load that font and let's then come down to our title and I'll do Josephine great so we now have that Josephine font let's go ahead and change our title we'll do vaccine skepticism by country so we got our title in the Josephine slab font it's a little bit thin and small but we'll worry about that later let's go ahead and get that chart or at the bottom to also be in Josephine slab and that was down here in axis title axed so we'll do family equals Josephine and again that also gets us that nice styling again the size and the thickness of the lines are a little bit different between the two cases I'm not going to worry about that right now we got the fonts in we're winning next I want to turn our attention to these other fonts on the y axis label and also our numbers so if I come back to the chart our website this font looks very similar to what I'm seeing in this in this text and so I think it's close enough right so the g you'll notice kind of has that little squiggle to the downstroke whereas the g here also has that I know I'm not using like the right jargon but whatever so what I'm going to do is I'm going to go ahead and highlight that text and then come down to inspect and what this will allow me to do is to look at the html and css code behind this statement I'm going to look through the styling information down here in the inspection tab for some indicator of what font it's using and so it's going to be something an attribute called font hyphen family that I'm looking for so you can see there's font family here aerial helvetica sans serif but that's all stricken out down here we have brandon grotesque as a font now what I'm going to do is I'm going to take brandon grotesque and I'm going to google for that font so what we see is that brandon grotesque is provided by adobe fonts adobe fonts is going to charge us no doubt to get that and it's going to be a lot more rigor moral than I really want to deal with at this point what I'll do instead is brandon grotesque google font and I'm looking through here for something like this which google web font is the closest to brandon grotesque and so this brings us to a kora page and so you know it's kind of this guy is saying there isn't one that really comes close monserat is good but as I'm kind of looking at this top line which is the brandon grotesque and this is the others that he's suggesting they don't really they don't really do the trick for me right they don't look all that great maybe open sans kind of does but not really I think it just doesn't look great so if we kind of scroll down for something else they found something as cabin from google fonts I don't know that doesn't look so great for me but they gave an update although it's thinner josephine sans oh my gosh so we're using josephine slab there's also a josephine sans so coming back to google fonts I'm going to go ahead and do the sans serif and let's search for josephine and so we get our sands and our slab let's go ahead and work with the josephine sands now what we're going to do in this case is a little bit different than what we did with josephine slab the problem with what we did before where we downloaded and installed the font and then loaded the font into our figure is that that works great for my computer where now I have josephine slab installed but you would also have to go about installing josephine slab that is not reproducible right I don't want to have a list of dependencies that includes fonts that's just it's a pain in the butt what we can do instead is install a font directly from google fonts and so that's what we're going to do here with josephine sans so we'll go ahead and do font add google family equals josephine sans and then we will then plop in josephine sans so now that went to google fonts and it then imported into this session the josephine sans font as the josephine hyphen sans family the problem with this approach is that you have to be connected to the internet for this to work so as long as you're working connected to the internet or whoever uses this down the road they will be able to add the font without having to install it directly on their computer and I think that is a little bit more reproducible I'm going to come back down to my theme I'll replace family copper with josephine sans and let's see what this looks like yeah I think this looks pretty good I'm happy with that appearance the india the a in the india is clearly in china is different um you know whatever the goal here really is to show how to import fonts not to get a hundred percent faithful if you wanted to go ahead and pay or go through the rigmarole with adobe fonts at that point though I think you are going to be installing the font on your computer and then loading it like we did before with copper plate as well as the josephine slab font anyway I think this is pretty good and it's a it's a pretty close font I think you kind of have to look really closely to notice that difference in the a but otherwise I think it looks pretty good and I'm going to be happy I'm going to call it good so the other thing we need to change then are the numbers in the gg text and we saw that we could do that back up here in the gg text and so I'll say josephine sans and so that then gives us the josephine sans font for the numbers next to our dumbbells that looks really good so it looks great I'm going to come back up to where we installed the josephine slab and I'm going to do a font add google here as well and then we'll do josephine hyphen slab and I will then call this josephine slab and then down here where we use josephine we also need to have slab and so that was on the x axis and I think also the title so that got us a bolder title I'm not sure what exactly was happening when we downloaded installed at the font but I think that looks a lot better it could of course be bigger but we're going to be manipulating the size of this plot in a future episode so I won't touch that too much I do notice the chart are at the bottom could be bolded so let's go ahead and bold that while we're thinking about it and so we can then do face equals bold and that gets as a bolded chart are down at the bottom and of course we will want to go back through and kind of tidy things up with the size and whatnot but we'll do that in a future episode for right now I'm really thrilled that we're able to bring in some non-default fonts to make this look quite a bit different than what you might expect looking at a figure generated by ggplot2 we saw that we can use the show text package with the font ad and the font ad google again the differences font ad is taking fonts that are installed on your computer and then loading them into your r-session whereas font ad google is pulling them down as it runs the code and loading it of course another important port to remember is that up here we need to use show text auto to tell everything that follows to use these special fonts that we've brought into our r script so I think that looks great again there's more tweaking that we'll do down the road but for now we'll leave it here and we'll save all that you know poking and prodding for another episode in the next episode we are going to do some more of that poking and prodding we are going to move the legend to be in the top right corner as it was in the chart r version and we'll probably play with some margins and kind of formatting to get the plot looking about as close to the chart r version as we can anyway I hope you found this useful let me know down below in the comments what is your favorite font and and can you go about finding that online either to install it directly under a computer or using google fonts and so again there's about 1100 fonts up there on google fonts and it's a kind of unique way to express your individuality and what you're trying to say about your data keep practicing and we'll see you next time for another episode of code club