用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画一朵花

    Prerequisites You will need to install the following pack...

  • Python从新手到大师——01

    python之禅 python的第一个程序 用python画朵花给媳妇 在画一个小猪佩奇给儿子

  • 铅笔素描 | 一支笔画一朵花

    今天, 我们继续来画一朵花! 一张纸, 一支笔, 一朵花, 简简单单, 干干净净, 一如初爱的感觉! 本期教程:用...

  • 一朵小花

    第一次自己独立画一朵花

  • 用R包(circlize)画circos图

    打算用R包画一个circos图来表示每条染色体上的snp位点的大致情况,虽然走了很多弯路,最后还是用最笨的方...

  • 师者德心,德者慈念,感动中国,感动七中!

    “用第一抹光线的纯净,为世界画一双眼睛; 用第一朵花开的声音,为世界唱一首歌曲,用所有春天的消息 为你写下...

  • 初次

    用第一朵花开的声音,给你唱一首歌曲 用清晨第一抹光线的纯净,给你画一双眼睛 用不断向前奔跑的步伐,为你写下传奇 用...

  • 教你使用python画一朵花送女朋友

    本文实例为大家分享了用python画一朵花的具体代码,供大家参考,具体内容如下 第一种,画法 from turtl...

  • 开学第一课——承载满满的爱,感动你我他

    N1230代永涛 用第一抹光线的纯净 为世界画一双眼睛 用第一朵花开的声音 为世界唱一首歌曲 用所有春天的消息 为...

  • 美丽心灵

    用第一抹光线的纯净 为世界画一双眼睛用第一朵花开的声音 为世界唱一首歌曲用所有春天的消息 为你写下传奇 这是感动中...

网友评论

    本文标题:用R画一朵花

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