Your Turn
Use color in your title to highlight your main finding. You’ll need to:
- Add HTML in the
labs()
function to add the title - Change the
plot.title
argument in thetheme()
function so that it interprets the HTML correctly
Solutions
Learn More
If you want to read about everything the ggtext
package can do, here’s its documentation website. The package’s author, Claus Wilke, also gave a talk at rstudio::conf 2020 about it.
Hopefully I’ve convinced you that you don’t need to know much HTML in order to use the ggtext
package. If you do want to learn a bit more, the HTML tutorials on W3schools.com are a great place to start. Here’s the page on the span
tag if you just want to check that out.
If you want to see the the list of named colors in R, it’s here. All named colors in R should also work in HTML. So, for example, if you have a line that is blue, adding <span style='color: blue'>Title Text</span>
will make your title that same shade of blue.
Have any questions? Put them below. David or Charlie Hadley will help you out!
You must be logged in to post a comment.
Does this html code work to change colors/lines in PDF and Word output as well?
Yes, it should. All the HTML code does is change the figure produced by ggplot. That figure is then displayed as a PNG image in PDF/Word so it should work. Let me know if it doesn’t work for you for some reason!
Made the school name bold (as well as colored) by adding to the html.
Cool! I know only there are only a few html tags that do anything, but sounds like bolding is one!
HI David should this plot.title be this: plot.title = element_markdown(face = “bold”)) + , we need to change it to element_markdown and also put the second brackets after bold.
On your solutions code you have this: plot.title = element_text(face = “bold”) +
Please confirm – unless ofcourse it worked on your machine.
Sorry, that was a typo! Fixed now.
Could you do this in a different theme, and how would you adjust accordingly? I’m trying to get it to work in theme_ipsum, but I’m not sure where to put the element_markdown(), or if it’s a different thing entirely. As my code currently stands, the html is displaying as part of the title.
enrollment_by_race_ethnicity %>%
filter(race_ethnicity==”Hispanic/Latine”) %>%
ggplot(aes(x=year,
y=prop_of_school_population,
group=district))+
geom_line(color=”lightgrey”)+
geom_line(data=highlight_district,
inherit.aes=TRUE,
color=”deeppink1″)+
geom_text(data=highlight_district,
inherit.aes=TRUE,
aes(label=percent_display),
color=”deeppink1″,
hjust=c(1,0))+
theme_ipsum(base_family=”Arial”,
axis_title_size=0,
grid=”XY”)+
scale_y_continuous(label=percent_format(),
limits=c(0,1))+
labs(title = “Douglas ESD showed significant growth in\nHispanic/Latine student population”)
I figured it out! I added the theme() function under theme_ipsum(), and it seems to be working well.
Yep! I usually use a default theme like theme_ipsum() and add some customizations using the theme() function.