用R画煎鸡蛋🍳

作者: caokai001 | 来源:发表于2020-03-05 11:19 被阅读0次

缘由

看着潜哥发的一条动态,画煎蛋

image.png

参考资料

【r<-ggplot2】cowplot添加注释
R语言数据可视化教程(ggplot2)_输入图形以展示
spot-matrix

小结:

  1. 一定要先画鸡蛋,后画外面的圈圈,不然就成了这样。
    鸡蛋铺满锅了
    2.cowplot 来完成拼图:
    创建一个标题grid,再和鸡蛋拼在一起
    3.svglite 包:ggsave() 可以调用它,保存为svg, 有时候显示更好
    image.png
    4.画多个图,用到了lapply 进行批量画图,你上一个简文更简洁。
    plot_grid(plotlist = lapply(radius, DrawFriedEgg , outer_r=1),ncol = 6)
    5.cowplot 包:如果参数是ncol =6,它会按照行画图;如果参数是nrow=4,它会按照列画图

实践

## 【r<-ggplot2】cowplot添加注释 :https://www.jianshu.com/p/0ec30fdffbd5
## R语言数据可视化教程(ggplot2)_输入图形以展示:https://blog.csdn.net/ARPOSPF/article/details/80544084

############################### 加载包

library(ggplot2)
library(cowplot) 
library(svglite)

############################### 画鸡蛋函数

DrawFriedEgg <- function(inner_r = 0.5, outer_r = 1,size=1.5){
  # inner_r : inner circle radius
  # outer_r : outer circle radius
  # size : circle line width
  
CreatCircle <- function(r = 1,center = c(0,0),npoints = 10000){

  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)
  
}

circle_df_inner <- CreatCircle(r =inner_r)
circle_df_outer <- CreatCircle(r = outer_r)

p <- ggplot()+
  geom_polygon(data=circle_df_inner,aes(x,y),colour="yellow",fill="yellow",size=size) +
  geom_path(data=circle_df_outer,aes(x,y),colour="black",size=size) +
  theme_void()
  
}

################# 画单个鸡蛋例子

title <- ggdraw() + draw_label("请大家吃鸡蛋", fontface='bold')
plot_grid(DrawFriedEgg(inner_r = 1, outer_r = 1))
plot_grid(title, p, ncol=1, rel_heights=c(0.1, 1)) # rel_heights values control title margins
ggsave("friedegg.svg",width = 8,height = 8,units = "cm")

################# 画多个鸡蛋例子

radius <- matrix(c(
  1,0.8,0.2,0.4,0.1,0.7,
  1,0.4,0.3,0.3,0.2,0.1,
  1,0.4,0.3,0.2,0.1,0,
  0.7,0.2,0.5,0.2,0.2,0.1),nrow = 4
)
radius <- as.vector(radius)


plot_grid(plotlist = lapply(radius, DrawFriedEgg , outer_r=1),ncol = 6) +
  draw_label("Do you want to eat eggs?",0.5,0.5,alpha = 0.8,size = 40,angle = 30 ,colour = "orange" )
ggsave("friedeggs.svg",width = 30,height = 20,units = "cm")



image.png





其他方法

####################################
# https://s0cran0r-project0org.icopy.site/web/packages/ggplot2/vignettes/extending-ggplot2.html
# https://rstudio-pubs-static.s3.amazonaws.com/294044_98c7dccab0ac4fdab2d3b0bb8eb97c5e.html
# Y 叔叔:https://mp.weixin.qq.com/s/_XH6u2hPv_JJi_MbGbeoHA
# https://weitinglin.com/2017/08/22/%E6%B7%B1%E5%85%A5r%E8%AA%9E%E8%A8%80object-oriented-programming%E4%BA%8C%EF%BC%9A%E9%96%8B%E7%99%BCggplot2%E5%BB%B6%E5%8D%87%E5%A5%97%E4%BB%B6%E6%89%80%E4%BD%BF%E7%94%A8%E7%9A%84ggproto%E7%B3%BB/


#https://community.rstudio.com/t/circle-in-ggplot2/8543

library(ggforce)
radius <- matrix(c(
  1,0.8,0.2,0.4,0.1,0.7,
  1,0.4,0.3,0.3,0.2,0.1,
  1,0.4,0.3,0.2,0.1,0,
  0.7,0.2,0.5,0.2,0.2,0.1),nrow = 4
)
radius <-as.data.frame(radius)
radius$Row <- paste0("r",1:4)
radius <- gather(radius,key = Col,value = r,-Row)

### Part 1 ,geom_point
ggplot(radius)+
  geom_point(aes(Row,Col,size=(20*r)),col="yellow")+
  geom_point(aes(Row,Col),size=20,pch=1)+
  theme_void()+
  theme(legend.position = "None")
 


 
### Part2 ,geom_circle
# https://github.com/thomasp85/ggforce/issues/109
radius2 <- radius
radius2$Row <- as.numeric(str_sub(radius2$Row,2,2))
radius2$Col <- as.numeric(str_sub(radius2$Col,2,2))
radius2$r <- radius2$r*50

ggplot(radius2)+
  geom_circle(aes(x0=Row,y0=Col,r=0.4),size=1,inherit.aes = FALSE)+
  geom_point(aes(Row,Col,size=r),col="yellow")+
  theme_void()+
  theme(legend.position = "None")


代码水平有限,如果有好的建议欢迎评论留言~

相关文章

  • 用R画煎鸡蛋🍳

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

  • 彩铅画,煎鸡蛋

    第一次画,近看真的不行。

  • 二宝画煎鸡蛋

    今天我和妈妈正在客厅里读课文,二宝喊妈妈进去,妈妈进去一看,大叫了一声,“哇”。我也赶紧进去一看,二宝在...

  • 重生高考前2

    问题:煎鸡蛋需要煎它的正、反面,如果煎熟一个鸡蛋的正反面各需要用去2分钟,那么用一次可容下2个鸡蛋的锅来煎3...

  • 煎鸡蛋

    每次妈妈都给我煎鸡蛋,今天我决定自己煎鸡蛋,我到了厨房,直接懵了,满屋的厨具,我回想妈妈是用那个厨具煎的,我找到了...

  • 煎鸡蛋

    食材鸡蛋,油,盐 过程上锅,倒油,油热打入鸡蛋,用锅铲翻动。煎至金黄。

  • 头疼的早餐

    今天早上弄了葱油饼和煎鸡蛋做早餐,葱油饼是那种半成品的,用煎 pan煎一下就好。另外再煎了三个鸡蛋。 还想煮些麦片...

  • 煎鸡蛋

    材料如下图: 1.拿出碗打一个或两个鸡蛋,如下图: 2.在切一些葱,在放到碗里,如下图: 3.在放上盐搅拌均匀,如...

  • 煎鸡蛋

    你们碰到过艰苦的任务吗,我就碰到过几次,其中让我印象最深刻的是煎鸡蛋的那次。 那天,早上起来我肚子就饿了,然后...

  • 煎鸡蛋

    晚上下班老爸来接我,车座上放着一张林老师发的假期优秀作业的喜报,老爸说是李皓然让他给我的,要求我回家表扬他...

网友评论

    本文标题:用R画煎鸡蛋🍳

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