如何在ggforce软件包的geom_voronoi_tile函数中更改区域的颜色

时间:2019-05-02 02:16:58

标签: r ggplot2 voronoi ggforce

我想从包ggforce中更改geom_voronoi_tile创建的voronoi多边形的颜色,但是无法这样做。我尝试使用以下代码:

library(tidyverse)
library(ggforce)
library(RColorBrewer)

col1 <- c("#d53e4f", "#f46d43", "#3288bd")
Species <- c("setosa", "versicolor", "virginica")

Tabla1 <- data.frame(Species, col1)
iris1 <- iris %>%
  left_join(Tabla1, by = "Species")


ggplot(iris1, aes(Sepal.Length, Sepal.Width) ) + 
  geom_voronoi_tile(aes(fill = Species, group = -1L, color = col1)) + 
  geom_voronoi_segment() +
  geom_point()

这是我的会话信息

R version 3.4.4 (2018-03-15)
Platform: x86_64-apple-darwin15.6.0 (64-bit)
Running under: macOS Sierra 10.12.6

Matrix products: default
BLAS: /System/Library/Frameworks/Accelerate.framework/Versions/A/Frameworks/vecLib.framework/Versions/A/libBLAS.dylib
LAPACK: /Library/Frameworks/R.framework/Versions/3.4/Resources/lib/libRlapack.dylib

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods  
[7] base     

other attached packages:
 [1] RColorBrewer_1.1-2 ggforce_0.2.0.9000 agricolae_1.2-8   
 [4] mxmaps_0.1         forcats_0.4.0      stringr_1.4.0     
 [7] dplyr_0.8.0.1      purrr_0.3.2        readr_1.1.1       
[10] tidyr_0.8.2        tibble_2.0.1       ggplot2_3.1.0     
[13] tidyverse_1.2.1   

1 个答案:

答案 0 :(得分:0)

要更改多边形填充的值,可以使用scale_fill_manual()将颜色设置为Species。请注意,我放下了color = col参数,因为它会将多边形的边界设置为一种颜色,在geom_voronoi_*()中该颜色由geom_voronoi_segment()处理。

ggplot(iris1, aes(Sepal.Length, Sepal.Width) ) + 
  geom_voronoi_tile(aes(fill = Species, group = -1L)) + 
  geom_voronoi_segment() +
  scale_fill_manual(values = col1, breaks = Species) +
  geom_point()

enter image description here

要更改多边形边缘的颜色,可以在aes(colour = Species)内设置geom_voronoi_segment()

ggplot(iris1, aes(Sepal.Length, Sepal.Width) ) + 
  geom_voronoi_tile(aes(fill = Species, group = -1L)) + 
  geom_voronoi_segment(aes(colour = Species, group = -1L)) +
  geom_point() +
  scale_colour_manual(values = col1, breaks = Species)

enter image description here