or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
2022 UC Carpentries Fall Workshop, R Day 3, Notes and Cheatsheet
plotting and report building with
ggplot2
andknitr
Lesson Data
We're using a teaching version of the gapminder dataset. There are several ways to obtain this. Just choose one.
gapminder <- read.csv("copied-link")
, orinstall.packages("gapminder")
(as with all packages, you only need to do this once)library("gapminder")
Gapminder Documentation
Intro - A Quick look at rstudio.cloud
Just an introduction, how to check for installed libraries.
Plotting with
ggplot2
The first session of Day 3 is Episode 8 from the Software Carpentry R for Reproducible Scientific Analysis Lesson.
ggplot
Function BasicsSyntax of a Basic Call
Putting
geom_point()
on its own line aids in readability, which is useful for a plot with many layers.ggplot
Elementsggplot()
creates a new ggplot.aes()
is how aesthetic mappings are constructed and associated with the data.aes()
function will map to all data points.geom_()
are geometric objects. These are added to the plot in layers, which is why aggplot
call includes+
something (geom/stat)geom_...()
calls can include their own aesthetic mappings, both using theaes()
function, and directly (remember, aesthetic arguments assigned outside theaes()
function will apply to all variables/data points)Challenge #1
Part A
In the gapminder example we've been using,
use the column "year" to show how life expectancy has changed over time.
Part B
We’ve been using the
aes
function to tell the scatterplot geom about the x and y locations of each point. Another aesthetic property we can modify is the pointcolor
. Modify the code from the Part A to color the points by the “continent” column. Is it easier to detect trends?Challenge #1 Solutions
Part A
Part B
About Layers
add
by=
:add points:
move color mapping:
Challenge #2 (2-minute challenge)
Using the previous example:
Switch the order of the point and line layers from the previous example. What happened?
Answer: the layers are drawn in a different order, so the lines now cover the points
Transformations and statistics
change scale:
add smoothing function,
lm
stands for linear model:About the Tilde
~
The tilde symbol
~
is often used as an operator to describe a statistical model formula.The left side is optional, and denotes the target or dependent variable. The right side is the predictor or independent variable(s).
~
Challenge #3
Part A
Given
Modify the color and size of the points on the point layer, but don't use the
aes()
function in that layer.Part B
Modify your solution to Part A so that the points are now a different shape and are colored by continent with new trendlines. Hint: The color argument can be used inside the
aes()
function.This cheatsheet helps with syntax on assigning arguments. A PDF version is at RSudio Cheatsheets.
Challenge #3 Solutions
Part A
Part B
Multipaneled Figures
Exporting Plots
Cheatsheets, et al
ggplot
function reference on tidyverse.Create Reports with
knitr
The second session of Day 3 is Episode 15 from the Software Carpentry R for Reproducible Scientific Analysis Lesson.
knitr
library reference.Creating a Markdown file
Within RStudio, click File → New File → R Markdown.
You might need to install packages
Fill out as much info as you want, and it will prepopulate the document header.
Markdown Basics
backticks
Title
Main section
Sub-section
Sub-sub section
with even smaller type
how small can it go?
When you knit the document, notice how RStudio jumps between the console and the render tab. Error messages, warnings, and other output involving creating the document will appear in the Render tab
Challenge 1
Create a new R Markdown document. Delete all of the R code chunks and write a bit of Markdown (some sections, some italicized text, and an itemized list).
Convert the document to a webpage.
More Markdown
[Carpentries Home Page](https://carpentries.org/)

F^2^
F~2~
$$y = \mu + \sum_{i=1}^p \beta_i x_i + \epsilon$$
R Code Chunks
How things are compiled
knitr
uses Pandoc, which is a really cool tool for document conversion!Chunk Options
Inline R