R语言 生成艺术,类似太阳的图案
# Thu Jan 21 10:25:17 2021 -
# https://mp.weixin.qq.com/s/lB4E624lGAXj85-zOXkyPQ
# 字符编码:UTF-8
# R 版本:R x64 4.0.3 for window 10
# [email protected]
# 个人笔记不负责任
# —— 拎了个梨
.rs.restartR()
rm(list=ls());gc()
# 生成艺术2
library(tidyverse)
library(ggforce)
library(patchwork)
apply_pattern_theme = function(bg_hex, caption_hex){
theme(
plot.background = element_rect(fill = bg_hex),
panel.background = element_rect(fill = bg_hex),
panel.grid = element_blank(),
plot.caption = element_text(family = "Open Sans",size = 6,color = caption_hex),
legend.position = "none",
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank()
)}
num_lines = 100
r = 1
circle = tibble(
len = seq(0, 2*pi, length.out = num_lines),
x = r*sin(len),
y = r*cos(len),
type = 1:100)
circle_test = circle[sample(1:100, 80),]
(p1 <- ggplot() +
geom_path(data = circle_test,
aes(x = x, y = y,
color = type),
size = 0.8,
alpha = 0.8) +
geom_path(data = circle_test,
aes(x = x, y = y,
color = type),
size = 0.5) +
scale_colour_gradient(low = "#f7f7f7",
high = "#6b7f8c") +
coord_fixed()+
apply_pattern_theme("black","white"))
dev.copy(png, "1.png");dev.off()
# Thu Jan 21 11:27:38 2021 --
#
num_lines = 4000
r_vals = seq(0.5, 1.5, by = .01)
circles_list = list()
for(i in 1:length(r_vals)){
r = r_vals[i]
circle = tibble(
len = seq(0, 2*pi, length.out = num_lines),
x = r*sin(len),
y = r*cos(len))
circles_list[[i]] = circle[sample(1:num_lines,
num_lines/i,replace = FALSE),]}
final_circle = bind_rows(circles_list)
(p2 <- ggplot() +
geom_point(data = final_circle,
aes(x = x, y = y),
color = "#e4cdc3",
size = 0.1,
alpha = 0.5) +
coord_fixed() +
apply_pattern_theme("black", "white"))
dev.copy(png, "2.png");dev.off()
# Thu Jan 21 11:28:07 2021 --
#
rings = tibble(
x = 1,xend = 5,y = c(2, 5),yend = c(2, 5))
points = tibble(
x = c(2, 4, 1.5, 3.5),y = c(2, 2, 5, 5))
(p3 <- ggplot() +
geom_segment(data = rings,
aes(x = x, xend = xend,
y = y, yend = yend),
color = "white") +
geom_point(aes(x = 3, y = 0),
size = 4,
color = "#c74a00") +
geom_point(data = points,
aes(x = x, y = y),
size = 3,
color = "#f39200") +
scale_y_continuous(limits = c(0, 5)) +
coord_polar() +
apply_pattern_theme(bg_hex = "#2e2137",
caption_hex = "white"))
dev.copy(png, "3.png");dev.off()
# Thu Jan 21 11:28:26 2021 --
#
num_lines = 400
circle = tibble(
len = seq(0, 2*pi, length.out = num_lines),
x = sin(len),
y = cos(len))
triangle_left = circle %>%
mutate(xend = x,
x = -1,
yend = y,
y = 0)
p4 <- ggplot() +
geom_polygon(data = circle,
aes(x = x, y = y),
fill = "#ab3a3a") +
geom_segment(data = triangle_left,
aes(x = x, xend = xend,
y = y, yend = yend),
color = "white",
size = 0.3,
lineend = "round") +
coord_fixed() +
apply_pattern_theme("white", "black")
p4
dev.copy(png, "4.png");dev.off()
p1+p2+p3+p4
dev.copy(png, "全图.png");dev.off()
```R