美文网首页好色之徒
2018-09-18 用ggplot2画一朵花吧🌺之二

2018-09-18 用ggplot2画一朵花吧🌺之二

作者: iColors | 来源:发表于2018-09-18 11:47 被阅读28次

这次画的是彩色的曼荼罗图

原文见https://github.com/aschinchon/mandalas-colored

image.png

绘图过程同前面基本一样,参数设置一下就是彩色的了。

(1)加载各种包,没有的自行下载

library(ggplot2)
library(dplyr)
library(deldir)
library(colourlovers)
library(rlist)

(2)设置绘图参数(可自行更改)

iter=3 # 迭代次数(深度)
points=14 # 绘图点的数目
radius=1.14 # 膨胀/压缩系数

(3)距离中心点的角度

angles=seq(0, 2*pi*(1-1/points), length.out = points)+pi/2

(4)设置起始绘图的中心

df=data.frame(x=0, y=0)

(5)围着中心反复迭代

for (k in 1:iter)
  {
    temp=data.frame()
    for (i in 1:nrow(df))
    {
      data.frame(x=df[i,"x"]+radius^(k-1)*cos(angles), 
                 y=df[i,"y"]+radius^(k-1)*sin(angles)) %>% rbind(temp) -> temp
    }
    df=temp
  }

(6) 建立提取每个多边形id、坐标和面积的函数

crea = function(tile) {tile %>% list.match("ptNum|x|y|area") %>% as.data.frame()}

(7)用结果数据创建镶嵌、多变性和数据框

df %>% 
  deldir(sort = TRUE)  %>% 
  tile.list() %>% 
  list.filter(sum(bp)==0) %>% 
  list.filter(length(intersect(which(x==0), which(y==0)))==0) %>% 
  lapply(crea) %>% 
  list.rbind() ->  df_polygon

(8)从colourLovers随机选一个绘图板

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

(9)用ggplot2的 geom_geom_polygon绘图

ggplot(df_polygon, aes(x = x, y = y)) +
  geom_polygon(aes(fill = area, color=area, group = ptNum), 
               show.legend = FALSE, size=0)+
  scale_fill_gradientn(colors=sample(palette, length(palette))) + 
  scale_color_gradientn(colors="gray30") +   
  theme_void()

(10) 保存图片

ggsave("mandala.png", height=5, width=5, units='in', dpi=600)

这是我画出的图,和上面原文给出的有区别,可能是随机选择的调色板的原因😓

my_mandala.png

原文还给出了很多漂亮的图,通过设置不同的参数可以得到👍


屏幕快照 2018-09-18 上午11.45.46.png
屏幕快照 2018-09-18 上午11.46.52.png

相关文章

  • 2018-09-18 用ggplot2画一朵花吧🌺之二

    这次画的是彩色的曼荼罗图 原文见https://github.com/aschinchon/mandalas-co...

  • 2018-09-18 用ggplot2画一朵花吧🌺

    最近浏览,发现有人用R画出了漂亮的曼荼罗图【印度教和佛教中象征宇宙圆形的图】,非常有意思。整理如下 原文见http...

  • 彩铅练习:花束

    春天到了,不能尽情的出去观赏美丽的花朵,就画一朵花束吧。

  • 彩铅手绘:紫花

    画一朵花儿,练练手吧。这次的花朵涂色有点儿惨,?。 线稿: 上色:

  • 孩子,是你让我改掉一个不好的习惯!

    珵珵:妈妈,我好无聊,你陪我画一幅思维导图吧。 我:好啊!你想画什么主题? 珵珵:我的中心图画一朵花吧。 我:好。...

  • 水彩

    一直喜欢画人物,今天休息就画朵花儿吧。 一边画一边学习 加油!

  • 用R画一朵花

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

  • Python从新手到大师——01

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

  • 一丿蓝

    北京 2018-09-18

  • 水彩练习

    在纸上随手画着玩,这画一朵花那画一片叶子最后加个杯子吧,就这样打了个草稿。杯子在网上搜了图片,是个别致秀气的杯子,...

网友评论

    本文标题:2018-09-18 用ggplot2画一朵花吧🌺之二

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