ixxmu / mp_duty

抓取网络文章到github issues保存
https://archives.duty-machine.now.sh/
110 stars 30 forks source link

用R绘制五星红旗,加在微信头像上,祝祖国母亲节日快乐!!! #5674

Closed ixxmu closed 2 hours ago

ixxmu commented 2 hours ago

https://mp.weixin.qq.com/s/mfU4EfqOQTWm1p-Ii-p0tg

ixxmu commented 2 hours ago

用R绘制五星红旗,加在微信头像上,祝祖国母亲节日快乐!!! by Investigator进修录


前言

想着借助ChatGPT和Kimi,用R画一面五星红旗,和ChatGPT硬刚了两小时,最后还是放弃了。写代码还是得靠自己啊,好好学R吧。后面附上Biomamba大神的代码,超赞(用R语言画一面五星红旗https://mp.weixin.qq.com/s/ivVDcp_QWpE_uREawZwXkA)!!!

1.R语言画一面五星红旗

ChatGPT:写代码还是弱精确调整五星红旗的代码:

# 安装并加载必要的包
if (!requireNamespace("ggplot2", quietly = TRUE)) install.packages("ggplot2")
if (!requireNamespace("ggstar", quietly = TRUE)) install.packages("ggstar")

library(ggplot2)
library(ggstar)

# 定义五星红旗的比例和颜色
flag_width <- 30  # 宽度为30单位,保持3:2的比例
flag_height <- 20  # 高度为20单位
flag_color <- "red"
star_color <- "yellow"

# 大星星的中心坐标和大小
big_star <- data.frame(
  x = 5,    # 大星星的x坐标
  y = 15,   # 大星星的y坐标
  size = 3  # 大星星的大小,这是一个根据比例调整的值
)

# 小星星的中心坐标、大小和旋转角度
small_stars <- data.frame(
  x = c(10, 11.5, 12.8, 11.5),   # 小星星的x坐标
  y = c(17, 15.5, 14, 12.5),     # 小星星的y坐标
  size = c(1, 1, 1, 1),          # 小星星的大小
  angle = c(30, 30, 0, -30)      # 小星星的旋转角度
)

# 绘制五星红旗
ggplot() +
  # 绘制红色背景
  geom_rect(aes(xmin = 0, xmax = flag_width, ymin = 0, ymax = flag_height), fill = flag_color) +
  # 绘制大星星
  geom_star(data = big_star, aes(x = x, y = y, size = size), fill = star_color, starshape = 5) +
  # 绘制小星星
  geom_star(data = small_stars, aes(x = x, y = y, size = size, angle = angle), fill = star_color, starshape = 5) +
  # 保持坐标比例为3:2
  coord_fixed(ratio = flag_width / flag_height) +
  # 移除坐标轴和背景装饰
  theme_void() +
  # 隐藏图例
  theme(legend.position = "none")

Biomamba大神的代码:

if(!require(ggplot2))install.packages("ggplot2")
if(!require(ggstar))install.packages("ggstar")
if(!require(ggthemes))install.packages("ggthemes")

library("ggplot2")
library("ggstar")

df<- data.frame(  x=c(0.6,1.2,1.5,1.5,1.2),                                
                  score=c(4.5,3.4,4.1,4.9,5.4),                                 
                  mysize=c('big',rep('small',4)),                                  
                  group=rep('yellow',5))
ggplot(data=df, aes(x=x,y=score)) + #设定横纵轴  
  geom_star(aes(fill=group,color='yellow'#去掉黑色描边    
                size=mysize,#设定分组与大小 
                angle=c(0,35,0,45,180)))+ #设定点的角度
  scale_starshape_manual(values = 'pentagram')+ #设置点的形状    
  scale_fill_manual(values=c("yellow")) + #黄色的五角星    
  scale_size_manual(values = c(30,8))+ #设置五角星的大小    
  theme(panel.background = element_rect(fill = 'red')) +  
  ylim(0.5,5.65)+xlim(0.1,5)+#设置横纵轴阈值
  theme(legend.title = element_blank()) + #去除图例
  theme(panel.grid=element_blank())+ 
  theme(axis.ticks=element_blank(),  
        axis.text.x = element_blank(),    
        axis.text.y = element_blank(),   
        axis.title.x = element_blank(),  
        axis.title.y = element_blank(),  
        legend.position="none")#去除坐标等绘图元素

ggsave(filename = '祝祖国母亲生日快乐.pdf',width = 9,height = 6)

2.给微信头像加一面五星红旗

读取R包,设置位置,加载微信头像:

library(ggplot2)
library(ggimage)
position<-data.frame(x=0:2,y=0:2)
mypicture<-"C:\\Users\\123\\Pictures\\default.jpg"
p<-ggplot(position,aes(x,y))+geom_image(image=mypicture,size=Inf)

加上国旗:

flag<-"C:\\Users\\123\\Pictures\\祝祖国母亲生日快乐.jpg"
p1<-p+geom_image(image=flag,x=0.5,y=0.2,size=0.5)

小结

今晚值班,明天开始放假,闭关跑代码!!!