美文网首页好色之徒数据-R语言-图表-决策-Linux-Python
2018-09-07用R画的几个有趣而无用的东西

2018-09-07用R画的几个有趣而无用的东西

作者: iColors | 来源:发表于2018-09-07 09:39 被阅读38次

是不是以后绘画也可以用R来画了。就像日本的一位老人用Excel画的画也可以一鸣惊人!😝😝

原文见https://fronkonstin.com/2018/09/06/tweetable-mathematical-art-with-r/

I love messing around with R to generate mathematical patterns. I always get surprised doing it and gives me lot of satisfaction. I also learn lot of things doing it: not only about R, but also about mathematics. It is one of my favourite hobbies. Some time ago, I published this post showing some drawings, each of them generated with less than 280 characters of code, to be shared on Twitter. This post came to appear in Hacker News, which provoked an incredible peak on visits to my blog. Some comments in the Hacker News entry are very interesting.

This Summer I delved into this concept of Tweetable Art publishing several drawings together with the R code to generate them. In this post I will show some.

Vertiginous Spiral

I came up with this image inspired by this nice pattern. It is a turtle graphic inspired pattern but instead of drawing lines I use geom_polygon to colour the resulting image in black and white:

image.png

Code:

library(tidyverse)
df <- data.frame(x=0, y=0)
for (i in 2:500){
  df[i,1] <- df[i-1,1]+((0.98)^i)*cos(i)
  df[i,2] <- df[i-1,2]+((0.98)^i)*sin(i)   
}
ggplot(df, aes(x,y)) + 
  geom_polygon(color="white")+
  theme_void()

Slight modifications of the code can generate appealing patterns like this:


image.png

Marine Creature

A combination of sines and cosines. It reminds me a jellyfish:

image.png

Code:

library(tidyverse)
seq(from=-10, to=10, by = 0.05) %>%
  expand.grid(x=., y=.) %>%
  ggplot(aes(x=(x^2+pi*cos(y)^2), y=(y+pi*sin(x)))) +
  geom_point(alpha=.1, shape=20, size=1, color="black")+
  theme_void()+coord_fixed()

summoning Cthulhu

The name is inspired in an answer from Mara Averick to this tweet. It is a modification of the marine creature in polar coordinates:

image.png

Code:

library(tidyverse)
seq(-3,3,by=.01) %>%
  expand.grid(x=., y=.) %>%
  ggplot(aes(x=(x^3-sin(y^2)), y=(y^3-cos(x^2)))) +
  geom_point(alpha=.1, shape=20, size=0, color="white")+
  theme_void()+
  coord_fixed()+
  theme(panel.background = element_rect(fill="black"))+
  coord_polar()

Naive Sunflower

Sunflowers arrange their seeds according a mathematical pattern called phyllotaxis, whic inspires this image. If you want to create your own flowers, you can do this Datacamp’s project. It’s free and will introduce you to the amazing world of ggplot2, my favourite package to create images:

image.png

Code:

library(ggplot2)
a=pi*(3-sqrt(5))
n=500
ggplot(data.frame(r=sqrt(1:n),t=(1:n)*a),
       aes(x=r*cos(t),y=r*sin(t)))+
  geom_point(aes(x=0,y=0),
             size=190,
             colour="violetred")+
  geom_point(aes(size=(n-r)),
             shape=21,fill="gold",
             colour="gray90")+
  theme_void()+theme(legend.position="none")

Silk Knitting

It is inspired by this other pattern. A lot of almost transparent white points ondulating according to sines and cosines on a dark coloured background:

image.png

Code:

library(tidyverse)
seq(-10, 10, by = .05) %>%
  expand.grid(x=., y=.) %>%
  ggplot(aes(x=(x+sin(y)), y=(y+cos(x)))) +
  geom_point(alpha=.1, shape=20, size=0, color="white")+
  theme_void()+
  coord_fixed()+
  theme(panel.background = element_rect(fill="violetred4"))

Try to modify them and generate your own patterns: it is a very funny way to learn R.

Note: in order to make them better readable, some of the pieces of code below may have more than 280 characters but removing unnecessary characters (blanks or carriage return) you can reduce them to make them tweetable.

相关文章

  • 2018-09-07用R画的几个有趣而无用的东西

    是不是以后绘画也可以用R来画了。就像日本的一位老人用Excel画的画也可以一鸣惊人!?? 原文见https://f...

  • 无用的东西,有趣的人。

    生活在现代大都市的我们,每天好像有一万件事情要做。忙于社交应酬,忙于赚钱养家,忙于家庭琐事,忙于学习进修。哪怕给自...

  • 为什么要做个有用的人,而不是有趣的人

    无论是做人还是做事,如果用有用无用,有趣无趣划分的话,大抵可分为四类: 有用又有趣、有用但无趣、有趣但无用、无用又...

  • 大家都有病 (漫画)

    整理以前的东西,发现多年前画的漫画。有趣的几个小故事。当年没有网络,没有手机,没有足够的金钱和资源,我都是用手抄用...

  • 2020-01-06 用R画等高线图(有趣而无用😝)

    来自http://a-blog-from-sydney.blogspot.com/2020/01/complex-...

  • 偶得

    有时候,那些看似无用的东西,恰恰有趣了我们的生活。

  • 中国式审美

    一切东西因无用而美,这确实是我们容易走进的一个误区,我们确实不会买一些无用的东西,哪怕是装饰画,它也是有用的。这让...

  • 【r<-高级|分享】用R画热图的几个参考

    刚学画热图,找了波度娘。发现几个网址不错,推荐下: 5个画热图的R包,你都知道吗? 介绍了pheatmap包、he...

  • 无用的有趣终归无用

    这短促的空闲给我带来了难以想象的安心,暂时抛却一年来的烦恼,抛却自己心里的计较,这一刻的安宁,让我觉得生活还是属于...

  • 无用的东西

    高中的时候,在书本上写着这样的话:“一切东西如果没用,等于没有”。当时写这句话的时候,纯属是要激励自己多看书,多看...

网友评论

    本文标题:2018-09-07用R画的几个有趣而无用的东西

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