Skip to content

Commit

Permalink
standardizing plot labels through episodes
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Jablonski committed Feb 26, 2025
1 parent eefe79d commit 7708c2f
Show file tree
Hide file tree
Showing 6 changed files with 229 additions and 99 deletions.
76 changes: 37 additions & 39 deletions scripts/ep_02.r
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,20 @@
rm(list=ls())

# set up objects

current_episode <- 2

campus_DEM <- rast("source_data/campus_DEM.tif")
campus_DEM_df <- as.data.frame(campus_DEM, xy = TRUE, na.rm=FALSE)

# make the name logical:
names(campus_DEM_df)[names(campus_DEM_df) == 'greatercampusDEM_1_1'] <- 'elevation'

campus_bath <- rast("source_data/SB_bath.tif")
campus_bath_df <- as.data.frame(campus_bath, xy = TRUE, na.rm=FALSE)
# make the name logical:
names(campus_bath_df)[names(campus_bath_df) == 'Bathymetry_2m_OffshoreCoalOilPoint'] <- 'depth'




# Plotting Data Using Breaks
#################################
# lesson example bins / highlights Harvard Forest pixels > 400m.
# for us, let's highlight our holes.
Expand All @@ -42,35 +40,25 @@ custom_bins <- c(-3, -.01, .01, 2, 3, 4, 5, 10, 40, 200)
campus_DEM_df <- campus_DEM_df %>%
mutate(binned_DEM = cut(elevation, breaks = custom_bins))

unique(campus_DEM_df$binned_DEM)

# there's sooooo few negative values that you can't see them.


# there are < 20 pixels under zero
summary(campus_DEM_df$binned_DEM)

# there's sooooo few negative values that you can't see them
# on the histogram
ggplot() +
geom_bar(data = campus_DEM_df, aes(binned_DEM)) +
ggtitle("Histogram")


# but think about landscapes. elevation tends to be
# a log. (I know this because I am a geographer)
# but think about landscapes. elevation tends to
# go up on a log scale from the coast
# log scale works better
# this shows that there's nothing at zero.
# and a little bit of negative


ggplot() +
geom_bar(data = campus_DEM_df, aes(binned_DEM)) +
scale_y_continuous(trans='log10') +
ggtitle("log10")



ggplot() +
geom_raster(data = campus_DEM_df, aes(x=x, y=y, fill = binned_DEM)) +
ggtitle("I dunno")

ggtitle("log10 histogram")


# let's go again with what we've learned
Expand All @@ -88,56 +76,62 @@ ggplot() +
ggtitle("Map")


# Challenge: Plot Using Custom Breaks
# ##################################

# challenge
# use custom bins to figure out a good place to put sea level
custom_bins <- c(-3, 4, 4.8, 5, 10, 25, 40, 70, 100, 150, 200)
custom_bins <- c(-3, 4.9, 5.1, 7.5, 10, 25, 40, 70, 100, 150, 200)
# what is really 'zero' around here?

custom_bins <- c(-3, 0, 4, 4.8, 5, 10, 25, 40, 70, 100, 150, 200)
custom_bins <- c(-3, 0, 4.9, 5.1, 7.5, 10, 25, 40, 70, 100, 150, 200)

campus_DEM_df <- campus_DEM_df %>%
mutate(binned_DEM = cut(elevation, breaks = custom_bins))

ggplot() +
geom_raster(data = campus_DEM_df, aes(x=x, y=y, fill = binned_DEM)) +
ggtitle("Map ?")


ggtitle("Where is sea level ?")


# More Plot Formatting
# #############################
# this isn't so nice


ggplot() +
geom_raster(data = campus_DEM_df, aes(x=x, y=y, fill = binned_DEM)) +
scale_fill_manual(values = terrain.colors(10)) +
ggtitle("Pretty ugly", subtitle = "boo")
scale_fill_manual(values = terrain.colors(11)) +
ggtitle("Where is sea level ?",
subtitle = "greens are too similar")

#coord_quickmap()

# let's seize control of our bins
coast_palette <- terrain.colors(10)
# let's seize more control of our bins
# like my_col in the lesson
coast_palette <- terrain.colors(11)

# set 4.9-5 ft a nice sea blue
coast_palette[2] <- "#1d95b3"
coast_palette[3] <- "#1c9aed"
coast_palette

# where's my nice blue?
# and make the negative numbers "red"
coast_palette[1] <- "red"

# but we can't see any underwaters.

ggplot() +
geom_raster(data = campus_DEM_df, aes(x=x, y=y, fill = binned_DEM)) +
scale_fill_manual(values = coast_palette)+
ggtitle("Last manual title?")+
ggtitle("Last manual title?", subtitle="Can't see red")+
coord_quickmap()


sum(campus_DEM_df$elevation < 0)



# hillshade layer
#ok we have to do something here to make a hillshade
#since one doesn't exist
# ok we have to do something here to make a hillshade
# since one doesn't exist

# insert script from map 7 here.

Expand Down Expand Up @@ -242,10 +236,14 @@ campus_DEM_df %>%
group_by(elevation) %>%
count()

str(campus_DEM_df)

# that's still too many? It's very few on Feb 4. this is part
# of why bins are handy
plot(campus_DEM_df$binned_DEM)
# ggplot(campus_DEM_df) +
# geom_histogram(aes(binned_DEM))



# Challenge: Make a 2-layer overlay for a 2nd set of rasters
# Challenge: Make a 2-layer overlay for a 2nd set of rasters
# try the bathymetry (if we have a hillshade)
28 changes: 24 additions & 4 deletions scripts/ep_04.r
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,29 @@
# or is it the DEM that has sea level at 4ft?


# Libraries
library(tidyverse)
library(terra)

# clean the environment and hidden objects
rm(list=ls())

current_episode <- 4
# make our ggtitles automagically #######
# set ggplot counter
current_ggplot <- 0

gg_labelmaker <- function(plot_num){
gg_title <- c("Episode:", current_episode, " ggplot:", plot_num)
plot_text <- paste(gg_title, collapse=" " )
print(plot_text)
current_ggplot <<- plot_num
return(plot_text)
}
# every ggtitle should be:
# ggtitle(gg_labelmaker(current_ggplot+1))
# end automagic ggtitle #######

# Libraries
library(tidyverse)
library(terra)

# reload rasters
# from output folder
Expand Down Expand Up @@ -64,6 +79,7 @@ campus_DEM_df <- campus_DEM_df %>%
# Let's have a look
ggplot() +
geom_raster(data = campus_DEM_df, aes(x=x, y=y, fill = binned)) +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf() # to keep map's proportions

# this sea level doesn't make much sense,
Expand All @@ -87,6 +103,7 @@ sea_level_df <- as.data.frame(sea_level_0, xy=TRUE) %>%
# how would I do this with overlay?
ggplot() +
geom_raster(data = sea_level_df, aes(x=x, y=y, fill = binned)) +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf() # to keep map's proportions

summary(sea_level_df)
Expand All @@ -101,6 +118,7 @@ length(custom_sea_bins)
# now sea level is zero.
ggplot() +
geom_raster(data = sea_level_df, aes(x=x, y=y, fill = binned)) +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf()


Expand All @@ -110,10 +128,12 @@ ggplot() +
geom_raster(data = campus_DEM_df, aes(x=x, y=y, fill = elevation)) +
geom_raster(data = campus_bath_df, aes(x=x, y=y, fill = bathymetry)) +
scale_fill_viridis_c(na.value="NA") +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf()

# end of ep. 4:
# write a new geoTIFF with the new
# how write a new geoTIFF
# with the new
# sea level = 0 version of the data

writeRaster(campus_DEM, "output_data/ep_4_campus_sea_level_DEM.tif",
Expand Down
21 changes: 19 additions & 2 deletions scripts/ep_05.r
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,31 @@
# clean the environment and hidden objects
rm(list=ls())

current_episode <- 5


library(terra)
library(tidyverse)
# library(sf)
library(raster)

current_episode <- 5

# make our ggtitles automagically #######
# set ggplot counter
current_ggplot <- 0

gg_labelmaker <- function(plot_num){
gg_title <- c("Episode:", current_episode, " ggplot:", plot_num)
plot_text <- paste(gg_title, collapse=" " )
print(plot_text)
current_ggplot <<- plot_num
return(plot_text)
}
# every ggtitle should be:
# ggtitle(gg_labelmaker(current_ggplot+1))
# end automagic ggtitle #######





# do we still need raster in this lesson?
Expand Down
33 changes: 30 additions & 3 deletions scripts/ep_06.r
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ rm(list=ls())

current_episode <- 6

# make our ggtitles automagically #######
# set ggplot counter
current_ggplot <- 0

gg_labelmaker <- function(plot_num){
gg_title <- c("Episode:", current_episode, " ggplot:", plot_num)
plot_text <- paste(gg_title, collapse=" " )
print(plot_text)
current_ggplot <<- plot_num
return(plot_text)
}
# every ggtitle should be:
# ggtitle(gg_labelmaker(current_ggplot+1))
# end automagic ggtitle #######



# compare extents of 2 bike path files
# these will give a warning message about projections
Expand Down Expand Up @@ -48,17 +64,20 @@ str(birds_points)
# were new west campus bike paths
ggplot() +
geom_sf(data=bikes_icm, color = "red") +
geom_sf(data=birds_points)
geom_sf(data=birds_points) +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf()

ggplot() +
geom_sf(data=bikes_icm, color = "red", size = .75) +
geom_sf(data=bikes_library, color = "blue", size = 1.5) +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf()

ggplot() +
geom_sf(data=bikes_library, color = "blue", size = 1.5) +
geom_sf(data=bikes_icm, color = "red", size = .75) +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf()


Expand All @@ -72,12 +91,14 @@ buildings <- st_read("source_data/Campus_Buildings/Campus_Buildings.shp")

ggplot() +
geom_sf(data=buildings, color = "red") +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf()

# campus Areas of Interest (AOIs) as geojson
greatercampus <- st_read("source_data/planet/planet/greater_UCSB-campus-aoi.geojson")
ggplot() +
geom_sf(data=greatercampus, color = "red") +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf()

# if I tell you these are a zoom, how would you confirm?
Expand All @@ -89,13 +110,15 @@ ggplot() +
geom_sf(data=greatercampus, color = "red") +
geom_sf(data=maincampus, color = "green") +
geom_sf(data=westcampus, color = "blue") +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf()


# POINTS
# bird habitat polygons
ggplot() +
geom_sf(data=birds_habitat, color = "red") +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf()


Expand All @@ -107,7 +130,9 @@ ggplot() +
geom_sf(data=buildings, color = "gray") +
geom_sf(data=greatercampus, color = "red") +
geom_sf(data=maincampus, color = "green") +
geom_sf(data=westcampus, color = "blue")
geom_sf(data=westcampus, color = "blue") +
ggtitle(gg_labelmaker(current_ggplot+1), subtitle="Error!")



# filled polygons need to go on the bottom
Expand All @@ -118,6 +143,7 @@ ggplot() +
geom_sf(data=bikes_icm, color = "blue", size = 1.5) +
geom_sf(data=bikes_library, color = "red", size = .75) +
geom_sf(data=buildings, color = "gray") +
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf()


Expand All @@ -133,4 +159,5 @@ ggplot() +
geom_sf(data=buildings, color = "gray") +
geom_sf(data=birds_points, color = "purple") +
geom_sf(data=birds_habitat, color = "lawngreen", size = 2) +
coord_sf()
ggtitle(gg_labelmaker(current_ggplot+1)) +
coord_sf()
Loading

0 comments on commit 7708c2f

Please sign in to comment.