Graphs Fun with plots

Fun with plots

When life gets dull, make some plots !

# load libraries
library(ggplot2)
library(hexbin)
library(scico)
library(cowplot)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.0     ✔ readr     2.1.4
✔ forcats   1.0.0     ✔ stringr   1.5.0
✔ lubridate 1.9.2     ✔ tibble    3.1.8
✔ purrr     1.0.1     ✔ tidyr     1.3.0
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter()    masks stats::filter()
✖ dplyr::lag()       masks stats::lag()
✖ lubridate::stamp() masks cowplot::stamp()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

I like the scico package for plotting, it is color blind friendly and looks elegant.

Scico color palettes you can choose from

scico_palette_show()

Scatter plots

# create data
set.seed(123)
x <- rnorm(5000)
y <- rnorm(5000)
df <- data.frame(x = x, y = y)
 
plot(x, y) 

Base R scatter plot is extremely useful, it’s quick and easy to understand, unless you have large number of data points, like in this example. It is quite difficult to follow what is going on here. Looks ugly as well !

Let’s try our very own ggplot way, and see how it looks like. But instead we plot the density of the data points, and we can see that the data is clustered around the center.

# plot the 2D density using ggplot2
ggplot(df, aes(x = x, y = y)) + 
  stat_density_2d_filled(bins = 20) +
  scale_fill_scico_d(palette = "lajolla", direction = -1) +
  theme_minimal() + 
  theme(legend.position = "none")

You can also experiment with different bin sizes, to explore any patterns, write some function to do it.

make_den_plot <- function(nbins) {
    ggplot(df, aes(x = x, y = y)) + 
        stat_density_2d_filled(bins = nbins) +
        scale_fill_scico_d(palette = "lajolla", direction = -1) +
        theme_minimal() + 
        theme(legend.position = "none")
}

ps <- map(c(5, 10, 80), make_den_plot)

cowplot::plot_grid(
    plotlist = ps, nrow = 1, ncol = 3, labels = c("Bins: 5", "Bins: 10", "Bins: 80"))

How about a hexbin way !!

bin <-hexbin(x, y, xbins = 40)
my_colors <- colorRampPalette(scico(n = 12, palette = "lajolla", direction = -1))
plot(bin, main = " " , colramp = my_colors , legend=F ) 

Scatter plot with hexbin is a good way to visualize the data density.

That’s all for now, I will keep updating this notebook, when I feel bored.