第 1 章 Prerequisites

1.1 Packages

install.packages(
  c("showtext","extrafont", "remotes")
)
  • showtext and extrafont are for Chinese characters to show properly in the graph.
remotes::install_github("tpemartin/econDV2", force=T)

If your R/RStudio has trouble of installing packages, run the following:

remotes::install_github("tpemartin/econR2")

Then watch the following video to setup your R/RStudio properly.

1.2 Start a project

You should always organize documents with the same purpose inside a unified folder, which is called a project folder, and initiate an R project with it.

Start a new project for your ggplot environment:

Project > New Project… > New Directory > New Project

Initiate a project and setup environment:

  • Addins > (under econDV2) Setup environment

1.3 Setup environment

For each project, there are always some tools or packages you want to import each time. The easiest way to set them up is to create a .Rprofile file at the project folder (that is why we need to setup a project folder first). .Rprofile can contain any R scripts that you want to run each time your start your project.

For this course, this will be the script you want to run each time:

library(ggplot2)
library(showtext)
library(econDV2)
# add google font: Noto Sans TC for chinese characters
sysfonts::font_add_google('Noto Sans TC')
# turn on showing chinese text in graph device
showtext_auto()
# set our graph theme to use the google font and a classic white back ground
theme_set(
  theme(
    text=element_text(family = "Noto Sans TC")
  )+
    theme_classic()
)
# some handy tool to use later through out the class
ggenv=new.env()
ggenv$gg <- list(
  dash = econDV2::ggdash,
  geom = econDV2::ggbrowse,
  aes = econDV2::ggaes
)
attach(ggenv)
sysfonts::font_families()

can show you what fonts you can use in R. Any font not there but in your operating system can be added to it via:

sysfonts::font_add("QFangsong", regular="/Users/martinl/Library/Fonts/cwTeXQFangsong-Medium.ttf")

1.4 其他

  • 後製軟體: Choose your own image editor, but make sure it support SVG editing. Here I use Boxy SVG.

  • 程式排版:

install.packages("styler")