Barplot (errorbar)

Authors

[Editor] Hu Zheng;

[Contributors]

Modified

2026-01-17

Note

Hiplot website

This page is the tutorial for source code version of the Hiplot Barplot (errorbar) plugin. You can also use the Hiplot website to achieve no code ploting. For more information please see the following link:

https://hiplot.cn/basic/barplot-errorbar?lang=en

Bar plot with error-lines and groups.

Setup

  • System Requirements: Cross-platform (Linux/MacOS/Windows)

  • Programming language: R

  • Dependent packages: data.table; jsonlite; ggplot2; Rmisc; ggpubr

# Install packages
if (!requireNamespace("data.table", quietly = TRUE)) {
  install.packages("data.table")
}
if (!requireNamespace("jsonlite", quietly = TRUE)) {
  install.packages("jsonlite")
}
if (!requireNamespace("ggplot2", quietly = TRUE)) {
  install.packages("ggplot2")
}
if (!requireNamespace("Rmisc", quietly = TRUE)) {
  install.packages("Rmisc")
}
if (!requireNamespace("ggpubr", quietly = TRUE)) {
  install.packages("ggpubr")
}

# Load packages
library(data.table)
library(jsonlite)
library(ggplot2)
library(Rmisc)
library(ggpubr)
sessioninfo::session_info("attached")
─ Session info ───────────────────────────────────────────────────────────────
 setting  value
 version  R version 4.5.2 (2025-10-31)
 os       Ubuntu 24.04.3 LTS
 system   x86_64, linux-gnu
 ui       X11
 language (EN)
 collate  C.UTF-8
 ctype    C.UTF-8
 tz       UTC
 date     2026-01-17
 pandoc   3.1.3 @ /usr/bin/ (via rmarkdown)
 quarto   1.8.27 @ /usr/local/bin/quarto

─ Packages ───────────────────────────────────────────────────────────────────
 package    * version date (UTC) lib source
 data.table * 1.18.0  2025-12-24 [1] RSPM
 ggplot2    * 4.0.1   2025-11-14 [1] RSPM
 ggpubr     * 0.6.2   2025-10-17 [1] RSPM
 jsonlite   * 2.0.0   2025-03-27 [1] RSPM
 lattice    * 0.22-7  2025-04-02 [3] CRAN (R 4.5.2)
 plyr       * 1.8.9   2023-10-02 [1] RSPM
 Rmisc      * 1.5.1   2022-05-02 [1] RSPM

 [1] /home/runner/work/_temp/Library
 [2] /opt/R/4.5.2/lib/R/site-library
 [3] /opt/R/4.5.2/lib/R/library
 * ── Packages attached to the search path.

──────────────────────────────────────────────────────────────────────────────

Data Preparation

Data frame:

<1st-col>: (Numeric) values as Y-axis.

<2nd-col>: (Numeric or String) classes as X-axis.

# Load data
data <- data.table::fread(jsonlite::read_json("https://hiplot.cn/ui/basic/barplot-errorbar/data.json")$exampleData[[1]]$textarea[[1]])
data <- as.data.frame(data)

# convert data structure
data[, 2] <- factor(data[, 2], levels = unique(data[, 2]))
data_sd <- summarySE(data, measurevar = colnames(data)[1], groupvars = colnames(data)[2])

# View data
head(data_sd)
    class N score       sd       se       ci
1    math 4 63.50 3.109126 1.554563 4.947314
2 chinese 4 73.75 2.986079 1.493039 4.751518
3 english 4 83.25 2.362908 1.181454 3.759914

Visualization

# Barplot (errorbar)
p <- ggplot(data_sd, aes(x = data_sd[, 1], y = data_sd[, 3], fill = data_sd[, 1])) +
  geom_bar(stat = "identity", color = "black", 
           position = position_dodge(), alpha = 1) +
  geom_errorbar(aes(ymin = data_sd[, 3] - sd, ymax = data_sd[, 3] + sd),
                width = 0.2,
                position = position_dodge(0.9)) +
  labs(title = "Barplot (errorbar)", x = colnames(data_sd)[1], 
       y = colnames(data_sd)[3], fill = colnames(data_sd)[1]) +
  geom_jitter(data = data, aes(data[, 2], data[, 1], fill = data[, 2]), size = 2, fill = "black", pch = 19, width = 0.2) +
  stat_compare_means(data = data, aes(data[, 2], data[, 1], fill = data[, 2]),
                     label = "p.format", ref.group = ".all.", vjust = 1, 
                     method = "t.test") +
  scale_fill_manual(values = c("#E64B35FF","#4DBBD5FF","#00A087FF","#3C5488FF")) +
  theme_bw() +
  ylim(0,100) +
  theme(text = element_text(family = "Arial"),
        plot.title = element_text(size = 12,hjust = 0.5),
        axis.title = element_text(size = 12),
        axis.text = element_text(size = 10),
        axis.text.x = element_text(angle = 0, hjust = 0.5,vjust = 1),
        legend.position = "right",
        legend.direction = "vertical",
        legend.title = element_text(size = 10),
        legend.text = element_text(size = 10))

p
FigureΒ 1: Barplot (errorbar)