Barplot (errorbar2)

Authors

[Editor] Hu Zheng;

[Contributors]

Bar plot with error-lines and groups.

Setup

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

  • Programming language: R

  • Dependent packages: ggplot2; grafify; ggpubr

# Install packages
if (!requireNamespace("ggplot2", quietly = TRUE)) {
  install.packages("ggplot2")
}
if (!requireNamespace("grafify", quietly = TRUE)) {
  install.packages("grafify")
}
if (!requireNamespace("ggpubr", quietly = TRUE)) {
  install.packages("ggpubr")
}

# Load packages
library(ggplot2)
library(grafify)
library(ggpubr)

Data Preparation

Data frame:

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

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

# Load data
data <- read.table("files/Hiplot/006-barplot-errorbar2-data.txt", header = T)

# convert data structure
data[, 2] <- factor(data[, 2], levels = unique(data[, 2]))

# View data
head(data)
  score   class
1    60    math
2    62    math
3    70 chinese
4    73 chinese
5    80 english
6    85 english

Visualization

# Barplot (errorbar2)
p <- plot_scatterbar_sd(
  data, ycol = get(colnames(data)[1]), xcol = get(colnames(data)[2]),
  b_alpha = 1, ewid = 0.2, jitter = 0.1) +
  stat_compare_means(data = data, aes(data[, 2], data[, 1], fill = data[, 2]),
                     label = "p.format", ref.group = ".all.", vjust = -2, 
                     method = "t.test") +
  guides(fill=guide_legend(title=colnames(data)[2])) +
  scale_y_continuous(expand = expansion(mult = c(0, 0.2))) +
  labs(x="class", y="score") +
  scale_fill_manual(values = c("#E64B35FF","#4DBBD5FF","#00A087FF","#3C5488FF")) +
  theme_classic2() +
  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 (errorbar2)