美文网首页
R data science-2

R data science-2

作者: 医科研 | 来源:发表于2019-01-14 20:30 被阅读4次

    解决本人问题的不知名报错代码:Sys.setlocale(‘LC_ALL’,‘C’)
    继续R data science 学习记录
    PS:可找到新办法了 Rpubs
    以下内容可在Rpubs更舒服的看。

    library(tidyverse)
    #ggplot2易错点将+号位置错放,+号必须放在一行代码的末位
    
    #分面-添加变量的另一种方法,适用于添加分类变量
    #facet_wrap()函数,第一个参数是公式,即~后加一个变量(不是数学公式哈)
    #例如将class变量用于分面
    ggplot(data = mpg) + 
      geom_point(mapping = aes(x = displ, y = hwy)) + 
      facet_wrap(~ class, nrow = 2)##分面为两行
    
    image.png
    #对两个变量的分面,应用函数facet_grid(),第一个参数也是公式
    #以下对drv cyl两个变量进行分面
    ggplot(data = mpg) + 
      geom_point(mapping = aes(x = displ, y = hwy)) + 
      facet_grid(drv ~ cyl)
    
    image.png
    #drv变量在行 cyl变量在列
    ##一些简单的小练习,后面加小点是,尽按drv变量在行分面
    ggplot(data = mpg) + 
      geom_point(mapping = aes(x = displ, y = hwy)) +
      facet_grid(drv ~ .)
    
    image.png

    几何对象的概念

    ##如果点在前面,按列分面-cyl变量
    ggplot(data = mpg) + 
      geom_point(mapping = aes(x = displ, y = hwy)) +
      facet_grid(. ~ cyl)
    
    image.png
    # 上,这个几何对象是点图(点有图像属性,颜色,大小,形状)
    ggplot(data = mpg) + 
      geom_point(mapping = aes(x = displ, y = hwy))
    
    image.png
    # 下图,几何对象是平滑曲线拟合数据(线也有图像属性,没有形状,但又线的类型linetype)
    ggplot(data = mpg) + 
      geom_smooth(mapping = aes(x = displ, y = hwy))
    
    image.png
    ##drv变量映射到linetype属性
    ggplot(data = mpg) + 
      geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))
    ## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
    
    image.png
    ##drv变量映射到group属性分组
    ggplot(data = mpg) +
      geom_smooth(mapping = aes(x = displ, y = hwy, group = drv))
    
    image.png
    ##drv变量映射到颜色属性
    ggplot(data = mpg) +
      geom_smooth(
        mapping = aes(x = displ, y = hwy, color = drv),
        show.legend = FALSE
      )
    
    image.png
    ##将geom_plot和geom_smooth两个几何对象显示图片,进行两次映射
    ggplot(data = mpg) + 
      geom_point(mapping = aes(x = displ, y = hwy)) +
      geom_smooth(mapping = aes(x = displ, y = hwy))
    
    image.png
    ##更简便的方法是,将一组映射传递给ggplot函数
    ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
      geom_point() + 
      geom_smooth()
    
    image.png
    ##将映射放到几何对象函数中,ggplot2将其认为是这个图层的的局部映射
    ##下面将颜色属性映射到几何对象point的calss变量
    ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
      geom_point(mapping = aes(color = class)) + 
      geom_smooth()
    
    image.png
    ##以下只拟合曲线中的一小部分,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)
    
    image.png
    #注意到只在geom_smooth图层有效
    
    ##在ggplot函数传递映射时一次就够
    ggplot(data = mpg, mapping = aes(x = displ, y = hwy, color = drv)) + 
      geom_point() + 
      geom_smooth(se = FALSE)##se参数表示的是阴影带
    
    image.png
    ##以下两代码的作用是相同的
    ggplot(data = mpg, mapping = aes(x = displ, y = hwy)) + 
      geom_point() + 
      geom_smooth()
    
    image.png
    ggplot() + 
      geom_point(data = mpg, mapping = aes(x = displ, y = hwy)) + 
      geom_smooth(data = mpg, mapping = aes(x = displ, y = hwy))
    
    image.png
    ##实现几个图
    ggplot(data = mpg,mapping = aes(x=displ,y=hwy))+
       geom_point()+
       geom_smooth(se=FALSE)
    
    image.png
    #drv映射到曲线
    ggplot(data = mpg)+
       geom_point(mapping = aes(x=displ,y=hwy))+
       geom_smooth(mapping = aes(x=displ,y=hwy,group=drv),se=FALSE)
    
    image.png
    #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))
    
    image.png
    #drv映射到点图层颜色
    ggplot(data = mpg)+
       geom_point(mapping = aes(x=displ,y=hwy,color=drv))+
       geom_smooth(mapping = aes(x=displ,y=hwy,se=FALSE))
    
    image.png

    哎呀 每次都要复制粘贴,耗费大量时间,我该相办法换个地方发表了。

    相关文章

      网友评论

          本文标题:R data science-2

          本文链接:https://www.haomeiwen.com/subject/ciajdqtx.html