美文网首页
ggplot2绘制高端艺术图

ggplot2绘制高端艺术图

作者: R语言数据分析指南 | 来源:发表于2021-01-21 09:25 被阅读0次

    ggplot2绘制艺术感满满的图,喜欢的小伙伴请关注个人公众号啊啊R语言数据分析指南,喜欢请分享转发,在此先行拜谢了!!!

    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 = 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"))
    
    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"))
    
    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"))
    
    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"))
    
    p1+p2+p3+p4
    

    全文链接:https://mp.weixin.qq.com/s/lB4E624lGAXj85-zOXkyPQ

    相关文章

      网友评论

          本文标题:ggplot2绘制高端艺术图

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