Base Histograms

Base Plot:

This plot is made using ggplot2 and viridis to give the plot a clean look. No real data is being used in the plot.

# Install packages if not already installed
# Load libraries
library(ggplot2)
Warning: package 'ggplot2' was built under R version 4.5.2
library(viridis)
Cargando paquete requerido: viridisLite
# Example data
set.seed(123)  # For reproducibility
data <- data.frame(values = rnorm(1000, mean = 50, sd = 10))

# Basic histogram
ggplot(data, aes(x = values)) +
  geom_histogram(binwidth = 5, fill = viridis(1), color = "black") +
  labs(
    title = "Basic Histogram",
    x = "Values",
    y = "Frequency"
  ) +
  theme_minimal()

Plot with Work Color Palette:

For work, I use the below color palette for my plots.

# Load libraries
library(ggplot2)

# Example data
set.seed(123)
data <- data.frame(values = rnorm(1000, mean = 50, sd = 10))

# Your custom palette
work_viridis <- colorRampPalette(c("#021C49","#1F3657","#3C5C7C",
                                   "#7FA0C0", "#E9EEF3", "#F3F7FF"))
colors <- work_viridis(256)

# Define background and line colors
bg_light <- "#F3F7FF"   # light background
line_dark <- "#021C49"  # dark line/text

# Histogram with custom palette and theme
ggplot(data, aes(x = values, fill = ..count..)) +
  geom_histogram(binwidth = 5, color = line_dark) +
  scale_fill_gradientn(colors = colors) +
  labs(
    title = "Histogram with Custom Viridis-Work Palette",
    x = "Values",
    y = "Frequency"
  ) +
  theme_minimal() +
  theme(
    plot.background = element_rect(fill = bg_light, color = NA),
    panel.background = element_rect(fill = bg_light, color = NA),
    panel.grid.major = element_line(color = line_dark, linewidth = 0.3),
    panel.grid.minor = element_line(color = line_dark, linewidth = 0.1),
    axis.title = element_text(color = line_dark, size = 12),
    axis.text = element_text(color = line_dark, size = 10),
    plot.title = element_text(color = line_dark, size = 14, face = "bold"),
    legend.title = element_text(color = line_dark),
    legend.text  = element_text(color = line_dark)
  )
Warning: The dot-dot notation (`..count..`) was deprecated in ggplot2 3.4.0.
ℹ Please use `after_stat(count)` instead.

Plot with Primary & Secondary Work Palettes to Create Contrasts:

library(ggplot2)

# Example data
set.seed(123)
data <- data.frame(values = rnorm(1000, mean = 50, sd = 10))

# Palette for theme
work_viridis <- colorRampPalette(c("#021C49","#1F3657","#3C5C7C",
                                   "#7FA0C0", "#E9EEF3", "#F3F7FF"))
theme_colors <- work_viridis(256)

# Palette for data
work2_viridis <- colorRampPalette(c("#55401C", "#7f602A", "#A98038","#D4B57F","#F1E6Df"))
data_colors <- work2_viridis(10)  # using 10 colors for bins

# Choose theme colors for background and lines
bg_light <- theme_colors[256]  # lightest color
line_dark <- theme_colors[1] # darkest color

# Histogram
ggplot(data, aes(x = values, fill = ..count..)) +
  geom_histogram(binwidth = 5, color = line_dark) +
  # Use the data palette
  scale_fill_gradientn(colors = data_colors) +
  labs(
    title = "Histogram with Separate Palettes",
    x = "Values",
    y = "Frequency"
  ) +
  # Theme uses work_viridis palette
  theme_minimal() +
  theme(
    plot.background = element_rect(fill = bg_light, color = NA),
    panel.background = element_rect(fill = bg_light, color = NA),
    panel.grid.major = element_line(color = line_dark, linewidth = 0.3),
    panel.grid.minor = element_line(color = line_dark, linewidth = 0.1),
    axis.title = element_text(color = line_dark, size = 12),
    axis.text = element_text(color = line_dark, size = 10),
    plot.title = element_text(color = line_dark, size = 14, face = "bold"),
    legend.title = element_text(color = line_dark),
    legend.text  = element_text(color = line_dark)
  )