How to Create Bee Swarm Plots in Tableau Public via R

I used the following tutorial to create bee swarm plots via RStudio: https://canonicalized.com/beeswarm-chart-in-tableau-via-r/amp/

Namely, the steps I took were as follows:

(Please note: replace FILE_NAME, NEW_FILE_NAME, and MEASURE_NAME with your relevant information.)

Step 1: Import your dataset into RStudio, or use the following code:

For an Excel dataset:

library(readxl)
FILE_NAME <- read_excel("FILE_NAME .xlsx")
View(FILE_NAME)

OR, for .a CSV dataset:

library(readr)
FILE_NAME <- read_csv("~/ FILE_NAME .csv")
View(FILE_NAME)

Step 2: Install the ggbeeswarm package:

Tools -> Install Packages -> Type in ggbeeswarm

Step 3: Draw the bee swarm plot:

library(ggplot2)
library(ggbeeswarm)
ggplot(FILENAME,aes(1,MEASURE_NAME)) + geom_beeswarm()

Step 4: Write the plot to a new file:

For Excel:

p <- ggplot(FILE_NAME,aes(1,MEASURE_NAME)) + geom_beeswarm()
pg <- ggplot_build(p)
NEW_FILE_NAME <- cbind(FILE_NAME,pg$data[[1]])
write.xlsx(NEW_FILE_NAME,file = "NEW_FILE_NAME.xlsx")

For CSV:

p <- ggplot(FILENAME,aes(1,`MEASURE_NAME`)) + geom_beeswarm()
pg <- ggplot_build(p)
NEW_FILE_NAME <- cbind(FILE_NAME,pg$data[[1]])
write.csv(NEW_FILE_NAME,file = "NEW_FILE_NAME.csv")

Step 5: Load the new dataset in Tableau, placing “y” in the Sections tab and “x” in the Rows section. Make sure to change the type to “Dimension.” Under “Marks,” drop the measure names in “Detail” and the measure values in “Colour.”

Check out my workbook on Tableau Public: https://public.tableau.com/profile/ninakjones#!/vizhome/FIFA20AttackingandDefendingStats/Dashboard

Leave a Reply

Your email address will not be published. Required fields are marked *