• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

Direct Labeling on line plots with geomtextpath

datavizpyr · December 17, 2021 ·

Labeling a plot greatly help understand the gist of a plot easily. ggplot2 offers a number of ways to add text labels to a plot. Often directly adding the labels on a plot instead of having a legend is a better option.

The R package, geomtextpath, a ggplot2 extension package offers a nice solution and it enables

Direct and automatic text labels that adhere to their associated line can also provide a neat alternative to legends, without the need for specifying exact label positions, and with a lower risk of overplotting.

Directly Labeling on ggplot2
Directly Labeling on ggplot2

Installing and loading packages

# install.packages("remotes")
remotes::install_github("AllanCameron/geomtextpath")
library(geomtextpath)
library(tidyverse)
theme_set(theme_bw(16))

Line plot with text label

with geom_textline() function in geomtextpath package, we can directly label on a line plot by specifying the label we want.

pressure %>% 
  ggplot(aes(temperature, pressure)) +
  geom_textline(label = "Mercury vapor pressure", 
                color = "deepskyblue4",
                linewidth=2) 
ggsave("directly_labeling_on_line_with_geomtextpath_ggplot.png")

geomtextpath nicely adds the label on the line plot.

Directly labelling on a line plot
Directly labelling on a line plot

Density plot with legends on the side

Typically, we highlight a third variable on a line plot or density plot using color with legends. ggplot2 easily enables with color argument.

Here is a simple example using Palmer penguin datasets, where the density lines are colored by species variable with color legends on the side of the plot.

library(palmerpenguins)
  drop_na() %>%
  ggplot(aes(x = body_mass_g, colour = species)) +
  geom_density(size=1.5)
Density plot with legend in ggplot2
Density plot with legend in ggplot2

Density plot with direct labels on density lines with geom_textpath()

One of the biggest challenges with having color legend on the side is depending on the number of variables we plot and how they behave on the plot, it can get difficult to identify which line correspond to which variable.

With geom_textdensity() function in geomtextpath package, we can directly add labels, curvy text replacing a portion of lines on the plot.

penguins %>%
  drop_na() %>%
  ggplot(aes(x = body_mass_g, colour = species, label = species)) +
  geom_textdensity(fontface = 2,
                   linewidth=2,
                   hjust = 0.5, 
                   vjust = 0.9)+
  theme(legend.position = "none")

Such direct labelling can make some plots really easy to read and understand.

Directly Labelling on Density plot
Directly Labelling on Density plot

Related posts:

Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Scatterplot with marginal multi-histogram with ggExtraHow To Make Scatterplot with Marginal Histograms in R? ggforce geom_circle(): Annotate with a circleHow To Annotate a Plot with Circle in R Default ThumbnailHow to Make Axis Text Bold in ggplot2

Filed Under: ggplot2, R Tagged With: direct labeling ggplot2, ggplot2 extension

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version