

We can use the text geom to write text onto our visual, rect will let us draw a rectangle, and the hline and vline geoms will draw horizontal and vertical lines, respectively. The reason I skipped them is because they fit better here, with labeling and annotating data. I skipped four geometric objects early on: text, rect, hline, and vline. Finally, I want to point out that the x and y values are at the same scale as on our graph-it’s not pixels over or anything weird like that. Also, notice that the text geom uses x and y parameters, whereas the rectangle uses xmin+xmax and ymin+ymax.
GGPLOT ANNOTATE TEXT CODE
We’ve added about 15 lines of code (because I’m keeping things nice and readable for the annotations) but it’s still just two function calls. Label = "High-GDP countries with\nunexpectedly low mean\nlife expectancy.",Īdding an annotation to call out a section of our visual. In this case, I’m going to create a text annotation as well as a rectangle annotation so you can see exactly the points I mean. To do this, I use the annotate() function. I’d like to call out that section of the visual and will use an annotation to do so. For example, going back to our wealth and longevity chart, there was a group of Asian countries with extremely high GDP but relatively low average life expectancy. AnnotationsĪnnotations are useful for marking out important comments in your visual. But at least we now have the ability to create nicer-looking labels. There’s plenty you can do with themes, and we’ll cover that in detail in an upcoming post. There isn’t much else that you can do with the label itself. I also changed the Y axis label to be something better than “avg_lifeExp.” It’s clear that we’re showing continents, so I do not need to tell you that. Ggplot(data = lifeExp_by_continent_1952, mapping = aes(x = reorder(continent, avg_lifeExp), y = avg_lifeExp)) + This makes sense when laying out a bar or column chart, like our chart of data by continent: So what else can we do? If you set x = NULL or y = NULL, then the X or Y axis, respectively, will no longer have a label. Subtitle = "Charting the relationship between a country's prosperity and its residents' life expectancy.",Ĭaption = "Source: Gapminder data set, 2010",Ī chart with title, subtitle, labeled axes, and labeled legend.Īnd just like that, we have a nicer-looking visual. Ggplot(data = gapminder, mapping = aes(x = gdpPercap, y = lifeExp)) + We can do pretty much all of this in one extra function call. We should also create a title so people know what this visual represents, and I’d like to reference that the source is the gapminder data set. I guess the term “continent” is okay but I’d prefer it be capitalized. Scale_color_brewer(type = "qual", palette = "Dark2") +Īn example of an image that we’ve created.Īside from our scale problem (that we’ll fix…again), I don’t like having “lifeExp” and “log(gdpPercap)” be the X and Y axis labels. Geom_point(alpha = 0.5, mapping = aes(color = continent)) +

Ggplot(data = gapminder, mapping = aes(x = log(gdpPercap), y = lifeExp)) + Install.packages("gapminder", repos = "") Install.packages("tidyverse", repos = "") Let’s start with an image that we’ve already seen before: By labels, I’m including labeling the X and Y axis, creating subtitles and titles, creating captions, and the header for a legend. In ggplot2, we use the labs function to modify labels. Today, we’re going to dig into labels and annotations, two vital parts of creating aesthetically pleasing graphs.
GGPLOT ANNOTATE TEXT HOW TO
Last time around, we looked at how to use scales and coordinates to clean up charts.
GGPLOT ANNOTATE TEXT SERIES
This is part four of a series on ggplot2.
