Libraries

#install.packages("tidyverse")
#install.packages("ggplot2")
#install.packages("packcircles")
#install.packages("readxl")
#install.packages("data.table")
#install.packages("dplyr")
library(data.table)
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:data.table':
## 
##     between, first, last
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(readxl)
library(ggplot2)
library(packcircles)
library(ggrepel)
Craigs_GL2 <- read_excel("~/Vinifer Accounting/Examples/Craigs GL2.xlsx")
data <- Craigs_GL2
remove(Craigs_GL2)
data1 <- rename(data, "Acct" = "Account Type", "Detail" = "Account Detail", "Type" = "Transaction Type", "Memo" = "Memo/Description")
DT <- data.table(filter(data1, Acct == "Income"))
summary <- DT[, sum(Amount), by=Name]

Create a starting point plot & name it g

ggplot(summary, aes(x = Name, y = V1, size = V1)) + geom_point(shape = 21, colour = "black", fill = "cornsilk")

g <- ggplot(summary, aes(x = Name, y = V1, size = V1)) + geom_point(shape = 21, colour = "black", fill = "cornsilk")

make the bubbles bigger

g + scale_size_area(max_size = 20)

## add labels to circles

g + scale_size_area(max_size = 20) + geom_text_repel(aes(label = Name), size = 3)

## Create a dataframe with one line representing one of the bubbles. It has x & y coordinates which are the center of the bubble and a radius. It names this dataframe packing.

packing <- circleProgressiveLayout(summary$V1, sizetype = "area")
## Warning in circleProgressiveLayout(summary$V1, sizetype = "area"): missing and/
## or non-positive sizes will be ignored

Add the circle coordinate data to the original data set (summary). The summary table will not have 3 new columns: x & y coordinates for center of circle and radius of circle. After this is done, confirm that the radius is proportional to the summed income for each customer.

summary <- cbind(summary, packing)

Next create another table with each point that will “draw” the circle.

dat.gg <- circleLayoutVertices(packing, npoints = 50)

Create the circles & name it “i”

ggplot() +geom_polygon(data = dat.gg, aes(x, y, group = id, fill=as.factor(id)), colour = "black", alpha = 0.6)

i <- ggplot() +geom_polygon(data = dat.gg, aes(x, y, group = id, fill=as.factor(id)), colour = "black", alpha = 0.6)

Add the labels & name it “j”

i + geom_text(data = summary, aes(x, y, size = V1, label = Name))
## Warning: Removed 3 rows containing missing values (geom_text).

j <- i + geom_text(data = summary, aes(x, y, size = V1, label = Name))

Change the label size & name it k

j + scale_size_continuous(range = c(1,5))
## Warning: Removed 3 rows containing missing values (geom_text).

k <- j + scale_size_continuous(range = c(1,5))

Remove the legends & name “l”

k + theme_void() + theme(legend.position = "none")
## Warning: Removed 3 rows containing missing values (geom_text).

l <- k + theme_void() + theme(legend.position = "none")

Name “m”

l + coord_equal()
## Warning: Removed 3 rows containing missing values (geom_text).

m <- l + coord_equal()

install.packages(“viridis”)

Add the values to the dat.gg table. Now there’s a new column with the value of each customer bubble in that table.

dat.gg$value <- rep(summary$V1, each=51)

change color scheme & name “n”

ggplot() + geom_polygon(data = dat.gg, aes(x, y, group = id, fill = value), colour = "black", alpha = 0.6) + scale_fill_distiller(palette = "Greens", direction = 1)

n <- ggplot() + geom_polygon(data = dat.gg, aes(x, y, group = id, fill = value), colour = "black", alpha = 0.6) + scale_fill_distiller(palette = "Greens", direction = 1)

to change the color palette, change where it says “Greens”. Palette options in appendix.

add the rest of the formatting:

n + geom_text(data = summary, aes(x, y, size = V1, label = Name)) + scale_size_continuous(range = c(1,5)) + theme_void() + theme(legend.position = "none") + coord_equal()
## Warning: Removed 3 rows containing missing values (geom_text).

## Change the background(s) to black in the “plot.background” section of theme() ## Change geom_text to geom_label to change black text to visible

n + geom_label(data = summary, aes(x, y, size = V1, label = Name)) + scale_size_continuous(range = c(1,5)) + theme_void() + theme(legend.position = "none", plot.background = element_rect(fill = "black")) + coord_equal()
## Warning: Removed 3 rows containing missing values (geom_label).

## Add a title at the end using ggtitle()

n + geom_label(data = summary, aes(x, y, size = V1, label = Name)) + scale_size_continuous(range = c(1,5)) + theme_void() + theme(legend.position = "none", plot.background = element_rect(fill = "black"), plot.title = element_text(color = "white")) + coord_equal() + ggtitle("Craigs Landscaping Customers")
## Warning: Removed 3 rows containing missing values (geom_label).

## space the circles out to easier see the labels ## 1. reduce the radius

packing$radius <- 0.75*packing$radius
summary.9 <- cbind(summary, packing)

2. remove extra columns from summary.9 table. Make sure it removes the old values & keeps the smaller radius values

summary.9 <- subset(summary.9, select = -x)
summary.9 <- subset(summary.9, select = -y)
summary.9 <- subset(summary.9, select = -radius)

3. re-run same code as above, but replace summary table with new summary.9 table. NOTE - you could have just ammended the summary table, but I’m nervous while still learning this, so I’m not writing over my data.

n + geom_label(data = summary.9, aes(x, y, size = V1, label = Name)) + scale_size_continuous(range = c(1,5)) + theme_void() + theme(legend.position = "none", plot.background = element_rect(fill = "black"), plot.title = element_text(color = "white")) + coord_equal() + ggtitle("Craigs Landscaping Customers")
## Warning: Removed 3 rows containing missing values (geom_label).

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.