My journey to a neat and tidy ggplot2 chart with R #bitcoin

By April 29, 2021Bitcoin Business
Click here to view original web page at medium.com
First plot

At the moment I am working on improving my skills with R. Therefore I wanted my ggplot2 charts to be neat and tidy. The goal was to have a template for reproducing the charts easily and that they all have the same appearance. Something like a ggplot2 corporate identity. On the lefthand side, you can see my starting point with a plot about the bitcoin hash rate. I think it already looks quite good, but as you are reading further you will see that there is still some room for improvement. Also at the final plots there are nevertheless things that can be discussed. I am quite happy with the result so far. Thank you again for the feedback Redditors. You can see the full source code at GitHub.

Final plots

In this medium post, I will walk you through the first plot: #Bitcoin Hash Rate (TH/s).

Summary:

  • Plotting different data layers
  • Add labels and customize x- and y-axis
  • Add a theme and customize it
  • Add your own logo to the plot
  • Export the plot with the logo included
library(ggplot2)
library(hrbrthemes)
library(grid)
library
(magick)
library(cairo)

The basis of the plot contains the line and the moving average as well as the circle to mark the last data point.

Plotting data layers
  geom_line(color="black", size=0.4, alpha=0.72) +

geom_line(aes(Date, ma),
color="#f7931b",
size=1.5,
alpha=0.85) +

geom_point(data=data[1:1, ]
,aes(x=Date, y=Value)
,colour="orange"
,size=4.5
,stroke=2.1
,shape=1
,alpha=0.85)

In this part of the code, you can already define the appearance of the data displayed. For the lines, I only adjusted color, size, and alpha (opacity). To make the circle at the end, I customized the shape and stroke of the point shape.

After the basic structure was set I started adding labels and caption as well as x- and y-axis looks:

I also formatted the y-axis to start at zero and cropped the data points accordingly:

Plotting labels and formatting axis

To crop axis with date or datetime you can use:

For the allover appearance, I used the theme theme_ipsum() as a starting point and customized some colors as well as added a border to the chart:

Making the plot look neat and tidy
plot <- plot + theme_ipsum() +
theme(
legend.position = "none",
axis.title.x = element_blank(),
plot.title = element_text(color = "#f7931b"),
plot.subtitle = element_text(color = "#3b3b3b"),
plot.caption = element_text(color = "#646464", face = 'bold'),
panel.border = element_rect(
colour = "grey",
fill = NA,
size = 1
)
)

Finally to add the logo you need the package called grid:

Plotting the logo
logo <- image_read("../pics/logo_twitter-account.jpg")grid::grid.raster(logo, x = 0.07, y = 0.03, just = c('left', 'bottom'), width = unit(0.5, 'inches'))

To export the chart with the plotted logo I used the cairo package:

Cairo::Cairo(
24, #length
16, #width
file = paste("plotname", ".png", sep = ""),
type = "png", #tiff
bg = "white", #white or transparent depending on your requirement
dpi = 300,
units = "cm" #you can change to pixels etc
)grid::grid.raster(
logo,
x = 0.07,
y = 0.03,
just = c('left', 'bottom'),
width = unit(0.5, 'inches')
)dev.off()

Sources:

Also, Read

Leave a Reply