R語言繪制Vonoroi圖的完整代碼
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")) #填充顏色
#改變圖形的形狀 #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)
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()
#添加熱圖 ggplot(df, aes(x, y, fill = dist)) + geom_voronoi() + geom_point() + scale_fill_gradient(low = "#20B2AA", high = "#9370DB") #顏色
#添加邊界線 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圖的資料請關註WalkonNet其它相關文章!
推薦閱讀:
- R語言學習ggplot2繪制統計圖形包全面詳解
- R語言繪制頻率直方圖的案例
- R語言使用ggplot繪制畫中畫細節放大的方法
- R語言可視化ggplot2繪制24小時動態血糖圖
- R包ggtreeExtra繪制進化樹