Erin M. Buchanan
2023-04-15
install.packages(c("rmarkdown", "knitr", "flextable", "dplyr",
"rio", "ggplot2", "ggthemes", "treemapify"))
papaja
is great for APA journal articles, and
rticles
has many journal article templatesbookdown
and blogdown
for building open
source text books and websites with markdownofficedown
and officer
for Word document
formatting---
title: "Untitled"
author: "Erin M. Buchanan"
date: "2023-04-15"
output: html_document
---
View()
in a markdownoutput:
html_document:
toc: true
toc_depth: 2
output:
html_document:
toc: true
toc_float: true
output:
html_document:
toc: true
toc_float:
collapsed: false
smooth_scroll: false
output:
html_document:
number_sections: true
theme
: here are the options default, bootstrap,
cerulean, cosmo, darkly, flatly, journal, lumen, paper, readable,
sandstone, simplex, spacelab, united, and yetihighlight
: default
, tango
,
pygments
, kate
, monochrome
,
espresso
, zenburn
, haddock
,
breezedark
, and textmate
code_folding: hide
or code_folding: show
allow you to show your code but enable people to hide itoutput:
html_document:
theme: united
highlight: tango
output:
word_document:
reference_docx: my-styles.docx
## R Markdown
This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see <http://rmarkdown.rstudio.com>.
When you click the **Knit** button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:
_text_
or *text
*text*
M~Group~
R^2^
[text](link)
![alt text or image title](path/to/image)
knitr
package:
include_graphics("path/to/image")
^[This is a footnote.]
- one item
- one item
- one item
- one more item
- one more item
- one more item
@citationname
@citationname
is found stored in a separate
.bib
. file that might have an entry like this:@Manual{R-base,
title = {R: A Language and Environment for Statistical
Computing},
author = {{R Core Team}},
organization = {R Foundation for Statistical Computing},
address = {Vienna, Austria},
year = {2017},
url = {https://www.R-project.org/},
}
Visual
editor.csl
file---
output: html_document
bibliography: references.bib
csl: biomed-central.csl
---
{.tabset}
class, you can organize output
into tabs that fall under that subheader## Quarterly Results {.tabset}
### By Product
(tab content)
### By Region
(tab content)
{r chunkname}
_
in a chunk name, use
-
instead{}
knitr::opts_chunk$set(echo = FALSE)
option = TRUE
or option = FALSE
eval
: Whether to evaluate a code chunk.
echo
: Whether to print the source code in the output
documentinclude
: Whether to include anything from a code chunk
in the output document.
include = FALSE
, this whole code chunk is excluded in
the outputcollapse
: Whether to merge text output and source code
into a single code block in the output.
collapse = TRUE
makes the output more compactcollapse = FALSE
means R expressions and their
text output are separated into different blockswarning
, message
, and error
:
Whether to show warnings, messages, and errors in the output
document.cache
: Whether to enable caching. (useful but be
careful)results
:
hide
- text output is hiddenasis
- text output is written “as is”fig.width
and fig.height
: The size of R
plots in inches.out.width
and out.height
: The output size
of R plots in the output document - you can use percentages herefig.align
: The alignment of plots. It can be
'left'
, 'center'
, or
'right'
.fig.cap
: The figure caption.child
: You can include a Rmd document in the main
document.ggplot2
figures after tables and show
you cowplot
to help organize multiple figures togetherkable()
in knitr
and the
kableExtra
package are awesome for tablesflextable
is a very flexible table maker that prints
nicely across formats as wellmpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb | |
---|---|---|---|---|---|---|---|---|---|---|---|
Mazda RX4 | 21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
Mazda RX4 Wag | 21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
Datsun 710 | 22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
Hornet 4 Drive | 21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
Hornet Sportabout | 18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
Valiant | 18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
mpg | cyl | disp | hp | drat | wt | qsec | vs | am | gear | carb |
---|---|---|---|---|---|---|---|---|---|---|
21.0 | 6 | 160 | 110 | 3.90 | 2.620 | 16.46 | 0 | 1 | 4 | 4 |
21.0 | 6 | 160 | 110 | 3.90 | 2.875 | 17.02 | 0 | 1 | 4 | 4 |
22.8 | 4 | 108 | 93 | 3.85 | 2.320 | 18.61 | 1 | 1 | 4 | 1 |
21.4 | 6 | 258 | 110 | 3.08 | 3.215 | 19.44 | 1 | 0 | 3 | 1 |
18.7 | 8 | 360 | 175 | 3.15 | 3.440 | 17.02 | 0 | 0 | 3 | 2 |
18.1 | 6 | 225 | 105 | 2.76 | 3.460 | 20.22 | 1 | 0 | 3 | 1 |
\@ref(type:label)
where label is tab, fig, or eq## [1] 20.09062
.Rprojects
as well to make handling
files easier# library to import all the things
library(rio)
# data with times and counts
DF <- import("trap_data_08-11-2022.csv")
# what's in the data
flextable(head(DF))
Grower | Farm | Crop | Variety | Trap | Pest | Latitude | Longitude | 6/17/2022 | 6/18/2022 | 6/19/2022 | 6/20/2022 | 6/21/2022 | 6/22/2022 | 6/23/2022 | 6/24/2022 | 6/25/2022 | 6/26/2022 | 6/27/2022 | 6/28/2022 | 6/29/2022 | 6/30/2022 | 7/1/2022 | 7/2/2022 | 7/3/2022 | 7/4/2022 | 7/5/2022 | 7/6/2022 | 7/7/2022 | 7/8/2022 | 7/9/2022 | 7/10/2022 | 7/11/2022 | 7/12/2022 | 7/13/2022 | 7/14/2022 | 7/15/2022 | 7/16/2022 | 7/17/2022 | 7/18/2022 | 7/19/2022 | 7/20/2022 | 7/21/2022 | 7/22/2022 | 7/23/2022 | 7/24/2022 | 7/25/2022 | 7/26/2022 | 7/27/2022 | 7/28/2022 | 7/29/2022 | 7/30/2022 | 7/31/2022 | 8/1/2022 | 8/2/2022 | 8/3/2022 | 8/4/2022 | 8/5/2022 | 8/6/2022 | 8/7/2022 | 8/8/2022 | 8/9/2022 | 8/10/2022 | 8/11/2022 | 8/12/2022 | 8/13/2022 | 8/14/2022 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A. Groskopf | AG | Dry Bean | GN | 1 | WBC | 41.96022 | 103.6856 | - | - | - | 0 | - | 0 | - | 0 | - | - | - | 0 | - | 2 | - | - | - | - | 1 | - | 1 | - | - | - | 18 | - | 48 | - | 52 | - | - | - | 678 | - | 325 | - | 266 | 95 | 92 | 64 | 57 | 58 | 60 | - | 76 | 15 | 24 | 12 | 6 | 9 | 4 | 3 | 3 | 1 | 1 | 2 | |||
A. Groskopf | AG | Dry Bean | GN | 2 | WBC | 41.95665 | 103.6844 | - | - | - | 0 | - | 0 | - | 0 | - | - | - | 0 | - | 0 | - | - | - | - | 1 | - | 1 | - | - | - | 18 | - | 29 | - | 29 | - | - | - | 321 | - | 248 | - | 153 | 99 | 62 | 31 | 44 | 44 | 42 | - | 98 | 24 | 29 | 6 | 8 | 4 | 5 | 1 | 12 | 6 | 2 | 7 | |||
A. Groskopf | AG | Dry Bean | GN | 3 | WBC | 41.95672 | 103.6780 | - | - | - | 0 | - | 0 | - | 0 | - | - | - | 0 | - | 0 | - | - | - | - | 0 | - | 0 | - | - | - | 36 | - | 26 | - | 93 | - | - | - | 469 | - | 221 | - | 226 | 45 | 46 | 19 | 26 | 46 | 12 | - | 25 | 11 | 6 | 9 | 8 | 12 | 6 | 5 | 4 | 2 | 2 | 3 | |||
A. Groskopf | AG | Dry Bean | GN | 4 | WBC | 41.96020 | 103.6781 | - | - | - | 0 | - | 0 | - | 0 | - | - | - | 0 | - | 1 | - | - | - | - | 2 | - | 5 | - | - | - | 60 | - | 110 | - | 86 | - | - | - | 464 | - | 518 | - | 448 | 169 | 221 | 239 | 165 | 214 | 117 | - | 195 | 82 | 52 | 31 | 60 | 31 | 26 | 15 | 5 | 12 | 9 | 6 | |||
J. Jenkins | JJ | Dry Bean | Pinto | 1 | WBC | 41.99753 | 103.7723 | - | - | - | 0 | - | 0 | - | 0 | - | - | - | 0 | - | 0 | - | - | - | - | 0 | - | 0 | - | - | - | 2 | - | 5 | - | 6 | - | - | - | 333 | - | 709 | - | 213 | 236 | 107 | 84 | 41 | 63 | 53 | - | 30 | 20 | 33 | 20 | 16 | 4 | 7 | 2 | 11 | 9 | 2 | 7 | |||
J. Jenkins | JJ | Dry Bean | Pinto | 2 | WBC | 41.99753 | 103.7754 | - | - | - | 0 | - | 0 | - | 0 | - | - | - | 0 | - | 0 | - | - | - | - | 0 | - | 0 | - | - | - | 4 | - | 10 | - | 13 | - | - | - | 609 | - | 581 | - | 360 | 162 | 101 | 47 | 28 | 94 | 22 | - | 44 | 23 | 31 | 6 | 18 | 4 | 10 | 8 | 14 | 7 | 5 | 8 |
ggplot2
can be overwhelming!
ggplot2
worksggplot2
worksggplot2
generally wants data in long format
# library to pivot
library(tidyr)
# cleaning up the data into long format
# cleaning up columns to graph
DF_long <- DF %>%
select(Farm, Variety, Trap, Latitude:`8/14/2022`) %>%
mutate(across(`6/17/2022`:`8/14/2022`, as.character)) %>%
pivot_longer(cols = -c(Farm, Variety, Trap, Latitude, Longitude),
names_to = "Date",
values_to = "Count") %>%
mutate(Count = as.numeric(Count)) %>%
filter(!is.na(Count)) %>%
mutate(Month = substr(Date, 1, 1)) %>%
mutate(Month = factor(Month,
levels = c(6,7,8),
labels = c("June", "July", "August"))) %>%
mutate(Date = as.Date(Date, format = "%m/%d/%Y"))
## Warning: There was 1 warning in `mutate()`.
## ℹ In argument: `Count = as.numeric(Count)`.
## Caused by warning:
## ! NAs introduced by coercion
Farm | Variety | Trap | Latitude | Longitude | Date | Count | Month |
---|---|---|---|---|---|---|---|
AG | GN | 1 | 41.96022 | 103.6856 | 2022-06-20 | 0 | June |
AG | GN | 1 | 41.96022 | 103.6856 | 2022-06-22 | 0 | June |
AG | GN | 1 | 41.96022 | 103.6856 | 2022-06-24 | 0 | June |
AG | GN | 1 | 41.96022 | 103.6856 | 2022-06-28 | 0 | June |
AG | GN | 1 | 41.96022 | 103.6856 | 2022-06-30 | 2 | June |
AG | GN | 1 | 41.96022 | 103.6856 | 2022-07-05 | 1 | July |
# first layer is what's x, y, color
ggplot(DF_long, # data
aes(x = Trap, # x axis
y = Count, # y axis
fill = Farm)) + # fill, color, shape
# calculate the averages from the data and add bars
stat_summary(fun = mean, # what function to do you want to calculate
geom = "bar", # what "geom" do you want to graph
position = "dodge") + # don't let the bars overlap
# calculate the confidence intervals for the means
stat_summary(fun.data = mean_cl_normal, # confidence limits
geom = "errorbar", # error bars or whiskers
position = position_dodge(width = .9), # don't overlap
width = .2) + # make them smaller than the bar
# make it not an ugly gray graph
theme_classic() +
# x axis label
xlab("Trap Number") +
# y axis label
ylab("Average Count of Pest") +
# put the legend on the bottom
theme(legend.position = "bottom")
# cool library of themes
library(ggthemes)
# first layer of data
ggplot(DF_long, # data
aes(x = Date, # x axis
y = Count, # y axis
color = Farm,
fill = Farm)) + # both color AND fill for this graph
# add a line that averages across time by Farm
geom_smooth(method = "loess") +
# add the points to see the actual data
geom_point() +
# add a silly theme
theme_wsj() +
# notice how this is ignored
xlab("Month") +
ylab("Number of Pests")
## `geom_smooth()` using formula = 'y ~ x'
# first layer of data
ggplot(DF_long %>%
group_by(Farm, Month) %>%
mutate(Count = sum(Count)) %>%
select(Month, Count, Farm) %>%
unique(), # manipulate the data by summing across months
aes(x = Month, # x axis
y = Count, # y axis
fill = Farm)) + # fill by Farm
# different silly theme
theme_fivethirtyeight() +
# position = fill makes this a percent chart
geom_bar(position = "fill",
stat = "identity") +
# notice again, these are ignored
xlab("Month") +
ylab("Total Number of Pests")
# first layer of data
ggplot(DF_long %>%
group_by(Farm, Month) %>%
mutate(Count = sum(Count)) %>%
select(Month, Count, Farm) %>%
unique(), # manipulate the data by summing across months
aes(x = Month, # x axis
y = Count, # y axis
fill = Farm)) + # fill by Farm
# different silly theme
theme_fivethirtyeight() +
# position = stack makes this a raw count
geom_bar(position = "stack",
stat = "identity") +
# notice again, these are ignored
xlab("Month") +
ylab("Total Number of Pests")
ggplot2
treemaps
or mosiac plots
# add on library to ggplot
library(treemapify)
# get the data
DF_pts <- import("processed_data/pts_summary.csv")
# view the data
flextable(head(DF_pts))
country_map | n | n_binned | un_region_sub | un_region |
---|---|---|---|---|
AD | 1 | < 100 | Southern Europe | Europe |
AE | 20 | < 100 | Western Asia | Asia |
AL | 3 | < 100 | Southern Europe | Europe |
AM | 1,020 | 1000-1999 | Western Asia | Asia |
AR | 285 | 100-999 | Latin America and the Caribbean | Americas |
AT | 1,048 | 1000-1999 | Western Europe | Europe |
# make the blank plot
ggplot(DF_pts, # data frame
aes(area = n, # how big should the box be
fill = n_binned, # what should we color by
# note mine are separate because binning helped visually
# usually those are the same
label = country_map, # label the boxes
subgroup = un_region_sub)) + # group by second variable
geom_treemap() + # make a treemap
# add a border by subgroup
geom_treemap_subgroup_border(colour = "white", size = 5) +
# where to put the text and what color
geom_treemap_text(colour = "white", place = "centre",
size = 15, grow = FALSE) +
# make it journal friendly by picking grays
scale_fill_manual(name = "Sample Size",
values = c("#c8c8c8", "#969696", "#646464", "#323232"))
ggplot2
addons I likeRcolorbrewer
GGally
ggmap
gganimate
ggthemes
ggpubr
This is a footnote↩︎