Skip to content

Commit

Permalink
link today's code
Browse files Browse the repository at this point in the history
  • Loading branch information
dushoff committed Jan 26, 2024
1 parent 1887678 commit ba96976
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 14 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ homScatter.Rout: code/homScatter.R homMerge.Rout
homBox.Rout: code/homBox.R homMerge.Rout
$(pipeR)

## Crib page; some usable stuff but not very different from the rmd
## code/homDump.R

######################################################################

## Dushoff lectures that live elsewhere
Expand Down
82 changes: 82 additions & 0 deletions docs/code/homDump.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@

library(readr)
library(dplyr)
library(tidyr)

homicides <- read_csv("data/CA_homicide.csv", col_types=cols())
pops <- read_csv("data/CA_popdat.csv", col_types=cols())
regions <- read_csv("data/canadaRegions.csv", col_types=cols())

summary(homicides)
summary(pops)
summary(regions)

longdat <- (homicides
|> pivot_longer(cols=-Place, names_to="year", values_to="homicides")
|> mutate(year=as.numeric(year))
)

places <- full_join(pops, regions, by="Place")

save("longdat", "places", file="tmp/homDB.rda")

library(forcats)
library(dplyr)

load("tmp/homDB.rda")

combdat <- (full_join(longdat, places, by="Place")
|> filter(Place != "Canada")
|> rename(Pop=Pop_2011)
|> mutate(
Place=fct_reorder(Place,Pop)
, Region=as.factor(Region)
)
)

summary(combdat)

saveRDS(combdat, "tmp/hom2.rds")
library(ggplot2)
theme_set(theme_bw()) ## black-and-white theme
library(directlabels)

pdf(width=10)

dat <- readRDS("tmp/hom2.rds")

base <- (ggplot(dat)
+ aes(x=year, y=homicides, color=Place)
)

## print(base + geom_line())

nice <- (base + geom_line()
+ scale_y_continuous(trans="log1p")
+ labs(y="Homicides per 100,000 population")
)

print(nice
+ geom_dl(aes(label=Place), method="last.points")
+ expand_limits(x=2013) ## add a little more space
+ theme(legend.position="none") ## don't need the legend any more
)

print(nice
+ aes(linewidth=Pop)
+ scale_size_continuous(trans="log10")
+ facet_wrap(~Region)
)
library(ggplot2)
theme_set(theme_bw()) ## black-and-white theme

dat <- readRDS("tmp/hom2.rds")

boxp <- (ggplot(dat)
+ aes(x=Place, y=homicides, color=Region)
+ geom_boxplot()
)

print(boxp) + geom_boxplot(outlier.colour=NULL) ## set outlier points to same colour as boxes
print(boxp + coord_flip())

3 changes: 2 additions & 1 deletion docs/code/homRead.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ rates <- (widedat
summary(rates)

## rates and places represent my current relational DB (tidy, non-repetitive)
## Do another merge both to check, and to make a file for a particular use (visualization_
## Do another merge both to check, and to make a file for a particular use (visualization, in this case)

## save creates an environment, typically uses rda
## We put it in a tmp/ subdirectory because we don't want to save these files; we want to regenerate them (and check that they can be generated on different platforms). You should ignore tmp/ in git.
save(places, rates, file="tmp/homRead.rda")
23 changes: 10 additions & 13 deletions docs/topics/Visualization.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
<title>Visualization</title>
<style>
html {
line-height: 1.5;
font-family: Georgia, serif;
font-size: 20px;
color: #1a1a1a;
background-color: #fdfdfd;
}
Expand All @@ -25,16 +28,13 @@
@media (max-width: 600px) {
body {
font-size: 0.9em;
padding: 12px;
padding: 1em;
}
h1 {
font-size: 1.8em;
}
}
@media print {
html {
background-color: white;
}
body {
background-color: transparent;
color: black;
Expand All @@ -60,10 +60,6 @@
img {
max-width: 100%;
}
svg {
height: auto;
max-width: 100%;
}
h1, h2, h3, h4, h5, h6 {
margin-top: 1.4em;
}
Expand All @@ -88,10 +84,9 @@
color: #606060;
}
code {
font-family: Menlo, Monaco, Consolas, 'Lucida Console', monospace;
font-family: Menlo, Monaco, 'Lucida Console', Consolas, monospace;
font-size: 85%;
margin: 0;
hyphens: manual;
}
pre {
margin: 1em 0;
Expand Down Expand Up @@ -156,15 +151,16 @@
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}

ul.task-list[class]{list-style: none;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
font-size: inherit;
width: 0.8em;
margin: 0 0.8em 0.2em -1.6em;
vertical-align: middle;
}
</style>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<header class="site-header">
Expand Down Expand Up @@ -210,6 +206,7 @@ <h1 id="resources">Resources</h1>
<ul>
<li><a href="../code/viz24.R">JD version of Tuesday live lecture
material</a></li>
<li>JD Thursday scripts: <a href="../code/homRead.R">read</a>; <a href="../code/homMerge.R">merge</a>; <a href="../code/homScatter.R">scatter plots</a></li>
</ul></li>
</ul>
<h1 id="exercise">Exercise</h1>
Expand Down

0 comments on commit ba96976

Please sign in to comment.