# 第 5 章 整體設計

## 5.1 標題與座標

``````knitr::opts_chunk\$set(fig.showtext=TRUE)
library(showtext)
showtext_auto()
ggplot(mpg)+
geom_point(aes(x=cty,y=hwy,color=class))+
labs(
x="市區每加侖距離",
y="國道每加侖距離",
title="市區與國道燃油效率",
subtitle="單位：每加侖公里數",
caption="資料來源"
)+
theme(text=element_text(size=10,  family="wqy-microhei"))``````

## 5.3 主題套用

``fig04_ans1 + theme_classic()``

### xkcd theme

1. 先下載xkcd字體
``````library(extrafont)
dest="xkcd.ttf", mode="wb")``````

``````library(showtext)
##### xkcd範例
``````library(xkcd)
volunteers <- data.frame(year=c(2007:2011),
number=c(56470, 56998, 59686, 61783, 64251))
xrange <- range(volunteers\$year)
yrange <- range(volunteers\$number)
ratioxy <-  diff(xrange) / diff(yrange)

datalines <- data.frame(xbegin=c(2008.3,2010.5),ybegin=c(63000,59600),
xend=c(2008.5,2010.3), yend=c(63400,59000))

mapping <- aes(x, y, scale, ratioxy, angleofspine,
anglerighthumerus, anglelefthumerus,
anglerightleg, angleleftleg, angleofneck)

dataman <- data.frame( x= c(2008,2010), y=c(63000, 58850),
scale = 1000 ,
ratioxy = ratioxy,
angleofspine =  -pi/2  ,
anglerighthumerus = c(-pi/6, -pi/6),
anglelefthumerus = c(-pi/2 - pi/6, -pi/2 - pi/6),
angleleftleg = 3*pi/2  + pi / 12 ,
anglerightleg = 3*pi/2  - pi / 12,
angleofneck = runif(1, 3*pi/2-pi/10, 3*pi/2+pi/10))

p <- ggplot() + geom_smooth(mapping=aes(x=year, y =number),
data =volunteers, method="loess") +
xkcdaxis(xrange,yrange) +
ylab("Volunteers at Caritas Spain") +
xkcdman(mapping, dataman) +
annotate("text", x=2008.7, y = 63700,
label = "We Need\nVolunteers!", family="xkcd" ) +
annotate("text", x=2010.5, y = 60000,
label = "Sure\nI can!", family="xkcd" ) +
xkcdline(aes(xbegin=xbegin,ybegin=ybegin,xend=xend,yend=yend),
datalines, xjitteramount = 0.12)
p # Figure 5.a``````