library(ggplot2)
library(tidyverse)
## ── Attaching packages ───────────────────────────────────── tidyverse 1.3.0 ──
## ✓ tibble 2.1.3 ✓ dplyr 0.8.3
## ✓ tidyr 1.0.0 ✓ stringr 1.4.0
## ✓ readr 1.3.1 ✓ forcats 0.4.0
## ✓ purrr 0.3.3
## ── Conflicts ──────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
data(mpg)
Q1. 請畫出排氣量(displ)與高速公路每加崙英哩(hwy)的關係,以及用顏色區分車種(class)。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))
Q2. 根據Q1, 再加上線性趨勢。
ggplot(data = mpg, aes(x = displ, y =hwy)) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))+
stat_smooth(method = 'lm')
Q3
ggplot(data = mpg) +
geom_point(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) +
geom_smooth(
mapping = aes(x = displ, y = hwy, linetype = drv),
show.legend = FALSE
)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'