用R画煎鸡蛋🍳

作者: caokai001 | 来源:发表于2020-03-05 11:19 被阅读0次

    缘由

    看着潜哥发的一条动态,画煎蛋

    image.png

    参考资料

    【r<-ggplot2】cowplot添加注释
    R语言数据可视化教程(ggplot2)_输入图形以展示
    spot-matrix

    小结:

    1. 一定要先画鸡蛋,后画外面的圈圈,不然就成了这样。
      鸡蛋铺满锅了
      2.cowplot 来完成拼图:
      创建一个标题grid,再和鸡蛋拼在一起
      3.svglite 包:ggsave() 可以调用它,保存为svg, 有时候显示更好
      image.png
      4.画多个图,用到了lapply 进行批量画图,你上一个简文更简洁。
      plot_grid(plotlist = lapply(radius, DrawFriedEgg , outer_r=1),ncol = 6)
      5.cowplot 包:如果参数是ncol =6,它会按照行画图;如果参数是nrow=4,它会按照列画图

    实践

    ## 【r<-ggplot2】cowplot添加注释 :https://www.jianshu.com/p/0ec30fdffbd5
    ## R语言数据可视化教程(ggplot2)_输入图形以展示:https://blog.csdn.net/ARPOSPF/article/details/80544084
    
    ############################### 加载包
    
    library(ggplot2)
    library(cowplot) 
    library(svglite)
    
    ############################### 画鸡蛋函数
    
    DrawFriedEgg <- function(inner_r = 0.5, outer_r = 1,size=1.5){
      # inner_r : inner circle radius
      # outer_r : outer circle radius
      # size : circle line width
      
    CreatCircle <- function(r = 1,center = c(0,0),npoints = 10000){
    
      tt <- seq(0,2*pi,length.out = npoints)
      xx <- center[1] + r * cos(tt)
      yy <- center[2] + r * sin(tt)
      
      circle_df <- data.frame(x = xx, y = yy)
      
    }
    
    circle_df_inner <- CreatCircle(r =inner_r)
    circle_df_outer <- CreatCircle(r = outer_r)
    
    p <- ggplot()+
      geom_polygon(data=circle_df_inner,aes(x,y),colour="yellow",fill="yellow",size=size) +
      geom_path(data=circle_df_outer,aes(x,y),colour="black",size=size) +
      theme_void()
      
    }
    
    ################# 画单个鸡蛋例子
    
    title <- ggdraw() + draw_label("请大家吃鸡蛋", fontface='bold')
    plot_grid(DrawFriedEgg(inner_r = 1, outer_r = 1))
    plot_grid(title, p, ncol=1, rel_heights=c(0.1, 1)) # rel_heights values control title margins
    ggsave("friedegg.svg",width = 8,height = 8,units = "cm")
    
    ################# 画多个鸡蛋例子
    
    radius <- matrix(c(
      1,0.8,0.2,0.4,0.1,0.7,
      1,0.4,0.3,0.3,0.2,0.1,
      1,0.4,0.3,0.2,0.1,0,
      0.7,0.2,0.5,0.2,0.2,0.1),nrow = 4
    )
    radius <- as.vector(radius)
    
    
    plot_grid(plotlist = lapply(radius, DrawFriedEgg , outer_r=1),ncol = 6) +
      draw_label("Do you want to eat eggs?",0.5,0.5,alpha = 0.8,size = 40,angle = 30 ,colour = "orange" )
    ggsave("friedeggs.svg",width = 30,height = 20,units = "cm")
    
    
    
    
    image.png





    其他方法

    ####################################
    # https://s0cran0r-project0org.icopy.site/web/packages/ggplot2/vignettes/extending-ggplot2.html
    # https://rstudio-pubs-static.s3.amazonaws.com/294044_98c7dccab0ac4fdab2d3b0bb8eb97c5e.html
    # Y 叔叔:https://mp.weixin.qq.com/s/_XH6u2hPv_JJi_MbGbeoHA
    # https://weitinglin.com/2017/08/22/%E6%B7%B1%E5%85%A5r%E8%AA%9E%E8%A8%80object-oriented-programming%E4%BA%8C%EF%BC%9A%E9%96%8B%E7%99%BCggplot2%E5%BB%B6%E5%8D%87%E5%A5%97%E4%BB%B6%E6%89%80%E4%BD%BF%E7%94%A8%E7%9A%84ggproto%E7%B3%BB/
    
    
    #https://community.rstudio.com/t/circle-in-ggplot2/8543
    
    library(ggforce)
    radius <- matrix(c(
      1,0.8,0.2,0.4,0.1,0.7,
      1,0.4,0.3,0.3,0.2,0.1,
      1,0.4,0.3,0.2,0.1,0,
      0.7,0.2,0.5,0.2,0.2,0.1),nrow = 4
    )
    radius <-as.data.frame(radius)
    radius$Row <- paste0("r",1:4)
    radius <- gather(radius,key = Col,value = r,-Row)
    
    ### Part 1 ,geom_point
    ggplot(radius)+
      geom_point(aes(Row,Col,size=(20*r)),col="yellow")+
      geom_point(aes(Row,Col),size=20,pch=1)+
      theme_void()+
      theme(legend.position = "None")
     
    
    
     
    ### Part2 ,geom_circle
    # https://github.com/thomasp85/ggforce/issues/109
    radius2 <- radius
    radius2$Row <- as.numeric(str_sub(radius2$Row,2,2))
    radius2$Col <- as.numeric(str_sub(radius2$Col,2,2))
    radius2$r <- radius2$r*50
    
    ggplot(radius2)+
      geom_circle(aes(x0=Row,y0=Col,r=0.4),size=1,inherit.aes = FALSE)+
      geom_point(aes(Row,Col,size=r),col="yellow")+
      theme_void()+
      theme(legend.position = "None")
    
    
    

    代码水平有限,如果有好的建议欢迎评论留言~

    相关文章

      网友评论

        本文标题:用R画煎鸡蛋🍳

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