美文网首页作图
第一章 数据探索

第一章 数据探索

作者: 芋圆学徒 | 来源:发表于2021-02-06 16:25 被阅读0次

    author: "Yuyuan Zhang"
    date: "2021/2/6"

    library(ggplot2)
    library(ggpubr)
    

    1点图-----

    qplot(mtcars$wt,mtcars$mpg)
    qplot(wt,mpg,data = mtcars)
    ggplot(mtcars,aes(wt,mpg))+geom_point()
    dev.off()
    

    2折线图----

    plot(pressure$temperature,pressure$pressure,type = "l")
    points(pressure$temperature,pressure$pressure)
    
    lines(pressure$temperature,pressure$pressure/2,col="red")
    points(pressure$temperature,pressure$pressure/2,col="red")
    dev.off()
    #
    qplot(pressure$temperature,pressure$pressure,geom = "line")
    #等价于下面命令
    ggplot(pressure,aes(temperature,pressure))+geom_line()
    #添加点
    qplot(pressure$temperature,pressure$pressure,geom = c("line","point"))
    ggplot(pressure,aes(temperature,pressure))+geom_point()+geom_line()
    
    

    3条图

    table(mtcars$cyl)
    barplot(table(mtcars$cyl))
    str(BOD)
    qplot(factor(BOD$Time),BOD$demand,geom = "bar")#绘制不出来
    qplot(Time,demand,data = BOD,geom = "bar",stat = "identity")#绘制不出来
    ggplot(BOD,aes(Time,demand))+geom_bar(stat = "identity")
    qplot(mtcars$cyl)
    qplot(factor(mtcars$cyl))
    

    4绘制直方图

    quantile(mtcars$mpg)
    hist(mtcars$mpg)
    hist(mtcars$mpg,breaks = 10)
    qplot(mtcars$mpg,binwidth=4)
    ggplot(mtcars,aes(mpg))+geom_histogram(binwidth = 4)
    dev.off()
    

    5绘制箱线图

    plot(ToothGrowth$supp,ToothGrowth$len)
    boxplot(len~supp,data = ToothGrowth)
    boxplot(len~supp+dose,data = ToothGrowth)
    
    qplot(ToothGrowth$supp,ToothGrowth$len,geom = "boxplot")
    ggplot(ToothGrowth,aes(supp,len))+geom_boxplot()
    #使用interaction组合变量
    qplot(interaction(ToothGrowth$supp,ToothGrowth$dose),ToothGrowth$len,geom = "boxplot")
    ggplot(ToothGrowth,aes(interaction(supp,dose),len))+geom_boxplot()
    
    

    6绘制函数图像-----

    curve(x^3-5*x,from = -4,to=4)
    myfun <- function(xvar){
      1/(1+exp(-xvar+10))
    }
    curve(myfun(x),from = 0,to=20)
    curve(1-myfun(x),add = T,col="red")
    
    

    相关文章

      网友评论

        本文标题:第一章 数据探索

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