服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - R语言 - R语言绘制Vonoroi图的完整代码

R语言绘制Vonoroi图的完整代码

2022-01-17 15:52作图帮 R语言

今天来给大家分享一篇教程关于R语言绘制Vonoroi图的完整代码,包括deldir包绘制Voronoi图,ggplot2绘制Voronoi图的实现代码,感兴趣的朋友跟随小编一起看看吧

deldir包绘制Voronoi图

#install.packages("deldir")
library(deldir)

# data
set.seed(1)
x <- runif(60)
y <- runif(60)

# Calculate Voronoi Tesselation and tiles
tesselation <- deldir(x, y)
tiles <- tile.list(tesselation)

plot(tiles, pch = 19,
   border = "black", #边界颜色
   showpoints = TRUE, #是否显示点
   fillcol = hcl.colors(60, "Sunset"))  #填充颜色

R语言绘制Vonoroi图的完整代码

#改变图形的形状
#install.packages("polyclip")
library(polyclip)
# Circle环状
s <- seq(0, 2 * pi, length.out = 3000)
circle <- list(x = 0.5 * (1 + cos(s)),
             y = 0.5 * (1 + sin(s)))
plot(tiles, pch = 19,
   col.pts = "white",
   border = "black",
   fillcol = hcl.colors(60, "Sunset""),
   clipp = circle)

R语言绘制Vonoroi图的完整代码
ggplot2绘制Voronoi图

library(ggvoronoi)
library(ggplot2)

set.seed(1)
x <- sample(1:600, size = 100)
y <- sample(1:600, size = 100)
dist <- sqrt((x - 200) ^ 2 + (y - 200) ^ 2)

df <- data.frame(x, y, dist = dist)

ggplot(df, aes(x, y)) +
stat_voronoi(geom = "path",
             color = 6,    
             lwd = 0.7,   
             linetype = 1) 
geom_point()

R语言绘制Vonoroi图的完整代码

#添加热图
ggplot(df, aes(x, y, fill = dist)) +
geom_voronoi() +
geom_point() +
scale_fill_gradient(low = "#20B2AA",
                    high = "#9370DB") #颜色

R语言绘制Vonoroi图的完整代码

#添加边界线
ggplot(df, aes(x, y, fill = dist)) +
geom_voronoi() +
stat_voronoi(geom = "path") +
geom_point()+
scale_fill_gradient(low = "#20B2AA",
                    high = "#9370DB")
#theme(legend.position = "none") # 去掉右侧图例标签

R语言绘制Vonoroi图的完整代码

以上就是R语言绘制Vonoroi图的详细内容,更多关于R语言绘制Vonoroi图的资料请关注服务器之家其它相关文章!

原文链接:https://blog.csdn.net/weifanbio/article/details/118017171

延伸 · 阅读

精彩推荐