用R画圈圈

作者: caokai001 | 来源:发表于2020-02-29 22:57 被阅读0次

缘由:

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

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画圈圈

    缘由: 看到潜哥,发了一张图,于是想画一下~ 参考: 杜雨大佬画圈圈卡片 思路: 用极坐标公式,计算出每个圆形每个...

  • 画个圈圈

    画个圈圈,将喜欢的你圈在里面, 画个圈圈,将家人包住, 画个圈圈,将记忆锁住, 画个圈圈,将幸运留住, …… 喜欢...

  • 起点

    画个圈圈

  • 用R画煎鸡蛋🍳

    缘由 看着潜哥发的一条动态,画煎蛋 参考资料 【r<-ggplot2】cowplot添加注释R语言数据可视化教程(...

  • 儿歌两首

    画圈圈 手拉手,围圈圈。 围着圈圈坐桌边。 左手画个圆, 变成泡泡飞上天。 右手画个圆, 变成糕点香又甜。 左...

  • 画个圈圈

  • 画个圈圈

    蹲在地上的不一定是看蚂蚁的小孩,有可能是画圈圈的大人。我真希望这个圈圈能有神奇的魔力,让那些画大饼的人得到应有的惩...

  • 画个圈圈

    你信命吗?不管你信不信,我信!因为在我把自己关起来不言不语躺了一周后,终于决定远走高飞!可是呢?当我收拾好行囊,就...

  • 画个圈圈

    玩圆规,画了一个圈圈。不如在圈圈里面写点字吧,能背的古诗文也不多,就来写一篇耳熟能详的陋室铭吧。 嘿嘿,别说,圈圈...

  • 【R>>ggforce】给Cluster画个小圈圈

    用ggplot2画图时,常常需要给不同的cluster加上圆圈或椭圆,这时候ggforce就派上用上了。 核心函数...

网友评论

    本文标题:用R画圈圈

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