• 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

How To Make Boxplots with Text as Points in R using ggplot2?

datavizpyr · April 13, 2020 ·

Boxplots with overlayed data points is a great way visualize multiple distributions. One of the biggest benefits of adding data points over the boxplot is that we can actually see the underlying data instead of just the summary stat level data visualization. Sometimes using text labels instead of data points can be helpful as it can quickly identify the samples that are outliers.

In this post, we will learn how to make boxplots with text describing the data points instead of data points.

library(tidyverse)

Let us create some data from scratch. Here we generate three groups of different sizes and numerical values for each of them.

# create dataframe with simulated data
df <- data.frame(group=c(rep("A",9), 
                         rep("B",10),
                         rep("C",15)),
                 value=c(rnorm(9),
                         rnorm(10), 
                         rnorm(15)),
                 stringsAsFactors = FALSE )

Let us also add sample ID number for each group’s data points. Later, we will use this sample identity to plot instead of data points.

df <- df %>% 
  group_by(group) %>%
  mutate(id = row_number()) %>%
  unite("ID", c(group,id), sep="",remove=FALSE)

We can look at the data and see that it has sample ID variable in addition to group and value variables.

head(df)

## # A tibble: 6 x 4
## # Groups:   group [1]
##   ID    group   value    id
##   <chr> <chr>   <dbl> <int>
## 1 A1    A     -0.136      1
## 2 A2    A      0.832      2
## 3 A3    A      0.442      3
## 4 A4    A      0.0640     4
## 5 A5    A     -0.402      5
## 6 A6    A     -0.762      6

Let us first make a simple boxplots with data points overlayed on boxplot. To do that we will first make boxplot with geom_boxplot() and then add jittered data points using geom_jitter(). We have also removed the legend for the boxplot as it is redundant.

df %>% 
  ggplot(aes(x=group, y=value, color=group))+
  geom_boxplot(width=.5)+
  geom_jitter(width=0.15)+
  theme(legend.position="none")
Boxplots with Jittered Data Points in R
Boxplots with Jittered Data Points in R

Boxplots with Text as Points in R using ggplot2 using geom_text()

One of the simplest ways to make boxplot with text label instead of data points is to use geom_text(). We use geom_text() instead of geom_point() or geom_jitter() and here we add jitter to text using “position_jitter”.

df %>% 
  ggplot(aes(x=group,y=value, label = ID, color=group))+
  geom_boxplot(width=.5)+
  # jittered text with geom_text
  geom_text(check_overlap = TRUE,
            position=position_jitter(width=0.15))+
  theme(legend.position="none")

We get a nice boxplot with text lables as points. Clearly boxplot with small labels are great, but with this simple approach bigger text labels can be problematic.

Boxplot with jittered text labels as points in R
Boxplot with jittered text labels as points in R

It is often useful in scenarios where we only need to highlight a few samples of interest with text as data points instead of all the points.

Related posts:

Customizing Labels on Bars in Side by side Stacked BarplotHow To Add Labels to Grouped Barplot with Bars Side-By-Side in R? Customizing Legend Inside Scatter Plot ggplot2How To Place Legend Inside the Plot with ggplot2? Scree plot: barplot with geom_col()How To Make Scree Plot in R with ggplot2 Sinaplot and ViolinplotSinaplot vs Violin plot: Why Sinaplot is better than Violinplot

Filed Under: ggplot2, R Tagged With: boxplot with jiitered text labels

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