1

我想更改由geom_voronoi_tileggforce 包创建的 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   
4

1 回答 1

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

在此处输入图像描述

要更改多边形边缘的颜色,您可以设置aes(colour = Species)inside 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)

在此处输入图像描述

于 2019-05-02T08:39:09.410 回答