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:再加上線性趨勢
ggplot(data = mpg, aes(x = displ, y =hwy)) +
geom_point(mapping = aes(x = displ, y = hwy, color = class))+
stat_smooth(method = 'lm')
## `geom_smooth()` using formula 'y ~ x'
#Q3:畫出下列圖形(提示:geom_smooth(aes(x = displ, y = hwy, linetype = drv)))
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'