今天端午节,但是我们不过节,我们要上课。。。 于是小郭老师提议给学员画个粽子,并甩来了用python画的链接。。。我最近写推文素材匮乏,借这个水一篇好了。链接里画的图长这样https://blog.csdn.net/qq_35164554/article/details/117817835

行嘛,也不难,画个多边形图而已。刚好这一期课程已经讲完了ggplot2,可以听懂。
ggplot2自带的多边形函数geom_polygon()画出来的不圆润呐。搜了一下ggforce包里的geom_shape可以画圆润的。
1.一开始的尝试,画了个残废粽子
dat = data.frame(x = c(0,2,1,2,1,3),
y = c(1,0,3,0,3,1),
id = factor(c(1,1,1,2,2,2)))
dat2 = data.frame(x = c(0.5,1.5,2),
y = c(2,1.5,2))
library(ggplot2)
library(ggforce)
p <- ggplot() +
geom_shape(data = dat,
aes(x = x, y = y,group = id),
fill = "#1f4b1a",
color = "black",
radius = unit(2, "mm"))+
geom_line(data = dat2,
mapping = aes(x = x,y = y),
color = "#57601d",size = 10)+
theme_classic()
p

2.这回正常点了吧
这是图:

这是数据:
dat = data.frame(x = c(0,2,3,1),
y = c(1,0,1,3))
dat
## x y
## 1 0 1
## 2 2 0
## 3 3 1
## 4 1 3
dat2 = data.frame(x = c(0.5,1.5,2),
y = c(2,1.5,2))
dat2
## x y
## 1 0.5 2.0
## 2 1.5 1.5
## 3 2.0 2.0
画图代码
p <- ggplot() +
geom_shape(data = dat,
aes(x = x, y = y),
fill = "#1f4b1a",
color = "black",
radius = unit(4, "mm"))+
geom_line(data = dat2,
mapping = aes(x = x,y = y),
color = "#57601d",size = 10)+
geom_line(aes(x = c(1.05,2),y = c(2.9,0.01)))+
theme_classic()+
coord_fixed()
p
网友评论