用R画一朵花

作者: OmicsAcademy | 来源:发表于2020-11-05 09:09 被阅读0次

    Prerequisites

    You will need to install the following packages (if you don’t have them already):

    install.packages("tidyverse")
    install.packages("colourlovers")
    

    Load packages

    library(tidyverse)
    
    ## Warning: package 'tidyverse' was built under R version 3.6.3
    
    ## -- Attaching packages --------------------------------------- tidyverse 1.3.0 --
    
    ## v ggplot2 3.3.2     v purrr   0.3.3
    ## v tibble  3.0.1     v dplyr   0.8.3
    ## v tidyr   1.0.0     v stringr 1.4.0
    ## v readr   1.3.1     v forcats 0.4.0
    
    ## Warning: package 'ggplot2' was built under R version 3.6.3
    
    ## Warning: package 'tibble' was built under R version 3.6.3
    
    ## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
    ## x dplyr::filter() masks stats::filter()
    ## x dplyr::lag()    masks stats::lag()
    
    library(colourlovers)
    
    ## Warning: package 'colourlovers' was built under R version 3.6.3
    

    Choose parameters for your flower

    exp <- 5     # exponent of the function
    ind <- 0.45  # independent term of the function
    ite <- 8     # number of iterations
    

    The function

    f <- function(x, y) x^exp + ind
    

    Reduce approach to iterate

    julia <- function (z, n) Reduce(f, rep(1,n), accumulate = FALSE, init = z)
    

    Set the grid of complex: 3000x3000 between -2 and 2

    complex_grid <- outer(seq(-2, 2, length.out = 3000), 1i*seq(-2, 2, length.out = 3000),'+') %>% as.vector()
    

    Iteration over grid of complex

    complex_grid %>% sapply(function(z) julia(z, n=ite)) -> datos
    

    Pick a top random palette from COLOURLovers

    palette <- sample(clpalettes('top'), 1)[[1]] %>% swatch %>% .[[1]] %>% unique() %>% colorRampPalette()
    

    Build the data frame to do the plt (removing complex with INF modulus)

    df <- data_frame(x=Re(complex_grid), 
                     y=Im(complex_grid), 
                     z=Mod(datos)) %>% 
      filter(is.finite(z)) %>% 
      mutate(col=cut(z,quantile(z, probs = seq(0, 1, 1/10)), include.lowest = TRUE))
    
    ## Warning: `data_frame()` is deprecated as of tibble 1.1.0.
    ## Please use `tibble()` instead.
    ## This warning is displayed once every 8 hours.
    ## Call `lifecycle::last_warnings()` to see where this warning was generated.
    
    # Limits of the data to frame the drawing
    Mx=max(df$x)+0.2
    mx=min(df$x)-0.2
    My=max(df$y)+0.2
    my=min(df$y)-0.2
    
    # Here comes the magic of ggplot
    df %>% 
      ggplot() + 
      geom_tile(aes(x=x, y=y, fill=col, colour = col)) + 
      scale_x_continuous(limits = c(mx, Mx), expand=c(0,0))+
      scale_y_continuous(limits = c(my, My), expand=c(0,0))+
      scale_colour_manual(values=palette(10)) +
      theme_void()+
      coord_fixed()+
      theme(legend.position = "none") -> plot
    
    plot
    
    flower.png
    # Do you like the drawing? Save it!
    ggsave("choose_a_name.png", plot, height=4, width=4, units='in', dpi=1200)
    

    Reference

    https://github.com/aschinchon/julia-flowers/blob/master/julia_flowers.R

    相关文章

      网友评论

        本文标题:用R画一朵花

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