美文网首页
第2章 ggplot2 入门

第2章 ggplot2 入门

作者: iBioinformatics | 来源:发表于2023-03-08 09:43 被阅读0次

    1. geom_point() 函数中多了一个aes的效果

    > ggplot(mpg,aes(displ,hwy))+geom_point(aes(colour="blue"))
    > ggplot(mpg,aes(displ,hwy))+geom_point(colour="blue")
    
    > library(ggplot2)
    Warning message:
    程辑包‘ggplot2’是用R版本4.1.3 来建造的 
    
    > economics
    # A tibble: 574 x 6
       date         pce    pop psavert uempmed unemploy
       <date>     <dbl>  <dbl>   <dbl>   <dbl>    <dbl>
     1 1967-07-01  507. 198712    12.6     4.5     2944
     2 1967-08-01  510. 198911    12.6     4.7     2945
     3 1967-09-01  516. 199113    11.9     4.6     2958
     4 1967-10-01  512. 199311    12.9     4.9     3143
     5 1967-11-01  517. 199498    12.8     4.7     3066
     6 1967-12-01  525. 199657    11.8     4.8     3018
     7 1968-01-01  531. 199808    11.7     5.1     2878
     8 1968-02-01  534. 199920    12.3     4.5     3001
     9 1968-03-01  544. 200056    11.7     4.1     2877
    10 1968-04-01  544  200208    12.3     4.6     2709
    # ... with 564 more rows
    
    
    
    > presidential <- subset(presidential,start>economics$date[1])
    > presidential
    # A tibble: 8 x 4
      name    start      end        party     
      <chr>   <date>     <date>     <chr>     
    1 Nixon   1969-01-20 1974-08-09 Republican
    2 Ford    1974-08-09 1977-01-20 Republican
    3 Carter  1977-01-20 1981-01-20 Democratic
    4 Reagan  1981-01-20 1989-01-20 Republican
    5 Bush    1989-01-20 1993-01-20 Republican
    6 Clinton 1993-01-20 2001-01-20 Democratic
    7 Bush    2001-01-20 2009-01-20 Republican
    8 Obama   2009-01-20 2017-01-20 Democratic
    
    
    > ggplot(economics) + 
        geom_rect(aes(xmin=start,xmax=end, fill=party),
                  ymin=-Inf,ymax=Inf,alpha=0.2,data=presidential)+
        geom_vline(aes(xintercept=as.numeric(start)),
                    data=presidential,colour="grey50",alpha=0.5)+
        geom_text(aes(x=start,y=2500,label=name),
                  data=presidential,size=3,vjust=0,hjust=0,nudge_x=50)+
        geom_line(aes(date,unemploy))+
        scale_fill_manual(values=c("blue","red"))
    
     ggplot(diamonds,aes(log10(carat),log10(price)))+
        geom_bin2d()+
        facet_wrap(~cut,nrow=1)
    

    3.5.3修改默认分组

    添加个体轨迹

    data(Oxboys,package="nlme")
    ggplot(Oxboys,aes(Occasion,height))+
        geom_boxplot()+
        geom_line(aes(group=Subject),colour="#3366FF",alpha=0.5)
    

    3.5.4 匹配图形属性与图形对象

    df <- data.frame(x=1:3,y=1:3,colour=c(1,3,5))
    
    ggplot(df,aes(x,y,colour=factor(colour)))+
        geom_line(aes(group=1),size=2)+
        geom_point(size=5)
    
    
    ggplot(df,aes(x,y,colour=colour))+
        geom_line(aes(group=1),size=2)+
        geom_point(size=5)
    
    
    ggplot(df,aes(x,y))+
        geom_line(aes(group=1),size=2)+
        geom_point(size=5)
    

    相关文章

      网友评论

          本文标题:第2章 ggplot2 入门

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