用R画圈圈

缘由:

看到潜哥,发了一张图,于是想画一下~

image.png

参考:

杜雨大佬
画圈圈
卡片

image.png

思路:

用极坐标公式,计算出每个圆形每个位点坐标。用高度cutoff_yy来过滤一些点,进行颜色填充,最后再画出最外面的大黑圈就可以。

# https://qa.1r1g.com/sf/ask/480391971/
# geom_path will do open circles, geom_polygon will do filled circles

signalCircle <- function(cutoff_yy= 0,label="50%",center = c(0,0),diameter = 1, 
                         npoints = 10000,circle_color="black",cirlce_fill="yellow"){
  
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  
  circle_df <- data.frame(x = xx, y = yy)

  
# 画一个图
# 添加注释:https://ask.hellobi.com/blog/learn_R/14237

P1 <-ggplot(circle_df) +
  geom_polygon(data=subset(circle_df,y<=cutoff_yy),
               aes(x,y),fill="yellow")+
  geom_path(data=circle_df,aes(x,y),color="black",size=2)+
  # scale_color_manual(values = c("black"))+
  # guides(color=FALSE)+
  theme_void()+
  annotate("text",x=0,y=0,label=label,size=20) +
  # ggtitle("50%")+theme(plot.title =element_text(hjust = 0.5,vjust = 1))+
  coord_fixed()

return(P1)
}
#################################################
# signalCircle(cutoff_yy = -4 , label= "10%",center = c(0,0), diameter = 10,npoints = 50000)

library(cowplot)
P1 <- signalCircle(cutoff_yy = -4 , label= "10%",center = c(0,0), diameter = 10,npoints = 50000)
P2 <- signalCircle(cutoff_yy = -3 , label= "20%",center = c(0,0), diameter = 10,npoints = 50000)
P3 <- signalCircle(cutoff_yy = 0 , label= "50%",center = c(0,0), diameter = 10,npoints = 50000)
P4 <- signalCircle(cutoff_yy = 3 , label= "80%",center = c(0,0), diameter = 10,npoints = 50000)
P5 <- signalCircle(cutoff_yy = 4 , label= "90%",center = c(0,0), diameter = 10,npoints = 50000)
plot_grid(P1,P2,P3,P4,P5,nrow=1)

不知道如何循环产生glist 对象,所以就一个个输出P1,P2,P3,P4,P5

image.png

待解决问题:

已知半径R,和弓形面积占圆形面积比例T,及S=pi*R*R*T ,及垂线高度h?????
由于上面的h 和T的表达式不清楚,所以下面代码cutoff_yy 那句代码有点问题(第四行)

image.png

# https://qa.1r1g.com/sf/ask/480391971/
# geom_path will do open circles, geom_polygon will do filled circles

signalCircle <- function(full_percent= 1,center = c(0,0),diameter = 1, 
                         npoints = 10000,circle_color="black",cirlce_fill="yellow"){
  # full_percent : 填充比例 ,如:0.2,0.8
  cutoff_yy <- center[2] - r * cos( (2*pi*full_percent)/2 )
  #### Part1
  
  r = diameter / 2
  tt <- seq(0,2*pi,length.out = npoints)
  xx <- center[1] + r * cos(tt)
  yy <- center[2] + r * sin(tt)
  
  circle_df <- data.frame(x = xx, y = yy)
  #### Part2
   


# 画一个图
# 添加注释:https://ask.hellobi.com/blog/learn_R/14237

P1 <-ggplot(circle_df) +
  geom_polygon(data=subset(circle_df,y<=cutoff_yy),
               aes(x,y),fill="yellow")+
  geom_path(data=circle_df,aes(x,y),color="black",size=2)+
  # scale_color_manual(values = c("black"))+
  # guides(color=FALSE)+
  theme_void()+
  annotate("text",x=1,y=1,label=paste0(full_percent*100,"%"),size=20) +
  # ggtitle("50%")+theme(plot.title =element_text(hjust = 0.5,vjust = 1))+
  coord_fixed()+
  theme(
    plot.margin=unit(c(0,0,0,0), "cm"),
  )

return(P1)
}
#################################################


signalCircle(full_percent = 0.1 , center = c(1,1), diameter = 2,npoints = 50000)

image.png

代码写的太差劲,欢迎评论留言~~

你可能感兴趣的:(用R画圈圈)