3  geom()

3.1 geom_point()

── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
✔ ggplot2 3.3.6      ✔ purrr   0.3.5 
✔ tibble  3.1.8      ✔ dplyr   1.0.10
✔ tidyr   1.2.1      ✔ stringr 1.4.1 
✔ readr   2.1.3      ✔ forcats 0.5.2 
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::arrange()   masks plyr::arrange()
✖ purrr::compact()   masks plyr::compact()
✖ dplyr::count()     masks plyr::count()
✖ dplyr::failwith()  masks plyr::failwith()
✖ dplyr::filter()    masks stats::filter()
✖ dplyr::id()        masks plyr::id()
✖ dplyr::lag()       masks stats::lag()
✖ dplyr::mutate()    masks plyr::mutate()
✖ dplyr::rename()    masks plyr::rename()
✖ dplyr::summarise() masks plyr::summarise()
✖ dplyr::summarize() masks plyr::summarize()
ggplot(mpg, aes(cty, hwy))+geom_point() 

3.2 geom_smooth()

ggplot(mpg, aes(cty, hwy))+geom_smooth() 
`geom_smooth()` using method = 'loess' and formula 'y ~ x'

3.3 geom_bar()

ggplot()+geom_bar(data=mpg,mapping=aes(manufacturer))

ggplot()+geom_bar(data=mpg,mapping=aes(class))

3.4 geom_line()

df <- data.frame(x = 1:10, y = c(4, 1, 9, 5, 8, 7, 3, 8, 3, 5))
df
    x y
1   1 4
2   2 1
3   3 9
4   4 5
5   5 8
6   6 7
7   7 3
8   8 8
9   9 3
10 10 5
ggplot(df, aes(x, y))+
  geom_line(size = 10)

ggplot(df, aes(x, y))+
  geom_step(size = 10)

3.5 实例

group_cyl<-group_by(mpg,cyl)
data_sumr<- summarise(group_cyl,avg=mean(hwy),se=sd(hwy)/sqrt(n()))
data_sumr
# A tibble: 4 × 3
    cyl   avg    se
  <int> <dbl> <dbl>
1     4  28.8 0.502
2     5  28.8 0.25 
3     6  22.8 0.415
4     8  17.6 0.390
p<-ggplot(data_sumr,aes(cyl, avg)) + geom_line()+geom_point(size=4) 
p+geom_errorbar(aes(ymin=avg-se, ymax=avg+se), width=0.1)