(解决本人问题的代码)Sys.setlocale(‘LC_ALL’,‘C’) 继续R data science 学习记录

library(tidyverse)
## -- Attaching packages -------------------------------------------------- tidyverse 1.2.1 --
## √ ggplot2 3.1.0     √ purrr   0.2.5
## √ tibble  1.4.2     √ dplyr   0.7.8
## √ tidyr   0.8.2     √ stringr 1.3.1
## √ readr   1.3.1     √ forcats 0.3.0
## -- Conflicts ----------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
#ggplot2易错点将+号位置错放,+号必须放在一行代码的末位

#分面-添加变量的另一种方法,适用于添加分类变量
#facet_wrap()函数,第一个参数是公式,即~后加一个变量(不是数学公式哈)
#例如将class变量用于分面
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_wrap(~ class, nrow = 2)##分面为两行

#对两个变量的分面,应用函数facet_grid(),第一个参数也是公式
#以下对drv cyl两个变量进行分面
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) + 
  facet_grid(drv ~ cyl)

#drv变量在行 cyl变量在列
##一些简单的小练习,后面加小点是,尽按drv变量在行分面
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) +
  facet_grid(drv ~ .)

##如果点在前面,按列分面-cyl变量
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) +
  facet_grid(. ~ cyl)

几何对象的概念

# 上,这个几何对象是点图(点有图像属性,颜色,大小,形状)
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy))

# 下图,几何对象是平滑曲线拟合数据(线也有图像属性,没有形状,但又线的类型linetype)
ggplot(data = mpg) + 
  geom_smooth(mapping = aes(x = displ, y = hwy))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

##drv变量映射到linetype属性
ggplot(data = mpg) + 
  geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

##drv变量映射到group属性分组
ggplot(data = mpg) +
  geom_smooth(mapping = aes(x = displ, y = hwy, group = drv))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

##drv变量映射到颜色属性
ggplot(data = mpg) +
  geom_smooth(
    mapping = aes(x = displ, y = hwy, color = drv),
    show.legend = FALSE
  )
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

##将geom_plot和geom_smooth两个几何对象显示图片,进行两次映射
ggplot(data = mpg) + 
  geom_point(mapping = aes(x = displ, y = hwy)) +
  geom_smooth(mapping = aes(x = displ, y = hwy))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

##更简便的方法是,将一组映射传递给ggplot函数
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point() + 
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

##将映射放到几何对象函数中,ggplot2将其认为是这个图层的的局部映射
##下面将颜色属性映射到几何对象point的calss变量
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point(mapping = aes(color = class)) + 
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

##以下只拟合曲线中的一小部分,calss变量中的一个子集
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point(mapping = aes(color = class)) + 
  geom_smooth(data = filter(mpg, class == "subcompact"), se = FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#注意到只在geom_smooth图层有效

##在ggplot函数传递映射时一次就够
ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + 
  geom_point() + 
  geom_smooth(se = FALSE)##se参数表示的是阴影带
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

##以下两代码的作用是相同的
ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_point() + 
  geom_smooth()
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

ggplot() + 
  geom_point(data = mpg, mapping = aes(x = displ, y = hwy)) + 
  geom_smooth(data = mpg, mapping = aes(x = displ, y = hwy))
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

##实现几个图
ggplot(data = mpg,mapping = aes(x=displ,y=hwy))+
   geom_point()+
   geom_smooth(se=FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#drv映射到曲线
ggplot(data = mpg)+
   geom_point(mapping = aes(x=displ,y=hwy))+
   geom_smooth(mapping = aes(x=displ,y=hwy,group=drv),se=FALSE)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#drv映射到曲线分组和颜色,映射到点图层的颜色
ggplot(data = mpg)+
   geom_point(mapping = aes(x=displ,y=hwy,color=drv))+
   geom_smooth(mapping = aes(x=displ,y=hwy,color=drv,group=drv,se=FALSE))
## Warning: Ignoring unknown aesthetics: se
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'

#drv映射到点图层颜色
ggplot(data = mpg)+
   geom_point(mapping = aes(x=displ,y=hwy,color=drv))+
   geom_smooth(mapping = aes(x=displ,y=hwy,se=FALSE))
## Warning: Ignoring unknown aesthetics: se
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'