• 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 Highlight Select boxplot in ggplot2

datavizpyr · December 11, 2024 ·

In this tutorial, we will learn how to highlight a select boxplot using ggplot2 in R. By highlighting a select boxplot with specific color, we can easily divert the attention to it.

Let us first load the packages needed. We will use Palmer Penguins dataset to make boxplots.

library(tidyverse)
library(palmerpenguins)
theme_set(theme_bw(16))

Our data looks like this.

penguins |>
  head()

# A tibble: 6 × 8
  species island    bill_length_mm bill_depth_mm flipper_length_mm body_mass_g
  <fct>   <fct>              <dbl>         <dbl>             <int>       <int>
1 Adelie  Torgersen           39.1          18.7               181        3750
2 Adelie  Torgersen           39.5          17.4               186        3800
3 Adelie  Torgersen           40.3          18                 195        3250
4 Adelie  Torgersen           NA            NA                  NA          NA
5 Adelie  Torgersen           36.7          19.3               193        3450
6 Adelie  Torgersen           39.3          20.6               190        3650
# ℹ 2 more variables: sex <fct>, year <int>

Here is a simple boxplot with species on x-axis and body mass of penguins on y-axis.

penguins |>
  ggplot(aes(species, body_mass_g))+
  geom_boxplot()+
  labs(title="How to highlight select boxplot")
ggsave("how_to_highlight_select_boxplot.png")
How to highlight select boxplot in ggplot2
How to highlight select boxplot in ggplot2

How to highlight select boxplot in ggplot2

In order to highlight one of the boxplots, we first need to create a new variable that defines which boxplot needs to be highlighted. In the example below we aim to highlight Gentoo species as it has large body mass among the three penguin species.

By naively adding the highlight variable either as fill or color, we can add color to the boxplot.

penguins |>
  mutate(highlight = ifelse(species == "Gentoo", "Highlighted", "Normal")) %>%
  ggplot(aes(species, body_mass_g, fill=highlight))+
  geom_boxplot()+
  labs(title="How to highlight select boxplot")
ggsave("highlight_select_boxplot_1.png")

Unfortunately it also adds colors to other boxplots as well not just the one we wanted to highlight.

highlight select boxplot with color in ggplot2
highlight select boxplot with fill color in ggplot2

We can get around this problem by manually specifying the color we want to highlight the box plot. IN the example below, we manually set color using scale_fill_manual() function such that the box plot we want to highlight is blue and the rest are white.

penguins |>
  mutate(highlight = ifelse(species == "Gentoo", "Highlighted", "Normal")) %>%
  ggplot(aes(species, body_mass_g, fill=highlight))+
  geom_boxplot()+
  scale_fill_manual(values = c("steelblue", "white")) +
  theme(legend.position = "none")+
  labs(title="How to highlight select boxplot")
ggsave("highlight_select_boxplot_with_specific_color.png")
Highlight select boxplot with specific color
Highlight select boxplot with specific color

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: Highlight select boxplot

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