Skip to content

Instantly share code, notes, and snippets.

@reedacartwright
Created November 7, 2021 04:06
Show Gist options
  • Save reedacartwright/a0243b569ac92c1951fe93679d8b8e7b to your computer and use it in GitHub Desktop.
Save reedacartwright/a0243b569ac92c1951fe93679d8b8e7b to your computer and use it in GitHub Desktop.
Introduction to Analyzing a Minecraft World, LIA 194 - Class 4
## Today we are going to work on learning how to plot data with
## ggplot2, which is a part of tidyverse.
# Install the latest version of rbedrock
devtools::install_github("reedacartwright/rbedrock")
# Install tidyverse
install.packages("tidyverse")
# Load tidyverse and rbedrock
library(tidyverse)
library(rbedrock)
# Plotting Overworld Chunks ------------------------------------------
# Open World Database
dbpath <- "ClassSMP"
db <- bedrockdb(dbpath)
# Grab all the keys from the database
keys <- get_keys(db)
# Let's look at some of them.
head(keys, 20)
# Let's parse the chunk keys and look at the results
chunk_keys <- parse_chunk_keys(keys)
chunk_keys
# Apply a filter that will keep all rows from the overworld with
# tag "ChunkVersion".
dat <- filter(chunk_keys, dimension == 0 & tag == "ChunkVersion")
# This is the same as the previous line, but uses a pipe operator
dat <- chunk_keys %>% filter(dimension == 0 & tag == "ChunkVersion")
# Let's create a plot of the location of all chunks in the db
g <- ggplot(dat, aes(x=x,y=z)) + geom_point()
g <- g + scale_y_reverse()
g
# We can add color and update the coordinates
g <- ggplot(dat, aes(x=16*x,y=16*z)) + geom_point(color="blue")
g <- g + scale_y_reverse()
g
# Fix the labels
g <- ggplot(dat, aes(x=16*x,y=16*z)) + geom_point(color="blue")
g <- g + scale_y_reverse() + labs(x="x", y="z")
g
# Use a raster plot instead of points.
g <- ggplot(dat, aes(x=16*x+8,y=16*z+8)) + geom_raster(fill="blue")
g <- g + scale_y_reverse() + labs(x="x", y="z")
g
# FIN
close(db)
# Plotting Block Distributions ---------------------------------------
# Open our db
db <- bedrockdb(dbpath)
# Read blocks from a chunk
blocks <- get_chunk_blocks_value(db, x=0, z=0, dimension=0,
names_only=TRUE)
# Locate all lava blocks and view the result
dat <- locate_blocks(blocks, "lava")
dat
# Lets create a vertical histogram of lava locations
g <- ggplot(dat, aes(y=y)) + geom_histogram(binwidth=1)
g
# Challenge: Create a vertical histogram of another block
# Read lava locations in 441 chunks
xg <- expand_grid(x=-10:10, z=-10:10, dimension=0)
blocks <- get_chunk_blocks_data(db, x=xg$x, z=xg$z,
dimension=xg$dimension, names_only=TRUE)
dat <- blocks %>% map_dfr(locate_blocks, pattern="lava")
# Plot the histogram
g <- ggplot(dat, aes(y=y)) + geom_histogram(binwidth=1)
g
# what about different ores?
dat <- blocks %>% map_dfr(locate_blocks, pattern="ore")
g <- ggplot(dat, aes(y=y, fill=block)) + geom_histogram(binwidth=1)
g
# Let's clean up our data
dat <- blocks %>% map_dfr(locate_blocks, pattern="_ore") %>%
mutate(ore = str_remove_all(block, "minecraft:|lit_|deepslate_"))
# Let's make a plot with pretty colors
g <- ggplot(dat, aes(y=y,fill=ore)) + geom_histogram(binwidth=1)
g <- g + theme_linedraw() + scale_fill_brewer(palette = "Set1")
g
# Let's use facet to make multiple plots
g <- ggplot(dat, aes(y=y,fill=ore)) + geom_histogram(binwidth=1)
g <- g + theme_linedraw() + scale_fill_brewer(palette = "Set1")
g <- g + facet_wrap(vars(ore))
g
close(db)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment