在 R 中的 ggplot2 中向所有方面添加点的最佳方法

Lis*_*els 2 r ggplot2

有人可以告诉我将相同的点添加到下面的图的每个方面的最佳方法吗?

例如,在下面,如果我选择标记为“未知”的点之一,我可以geom_point在绘制绘图面后调用,但(因为它被标记为“未知”)它仅在第四个面上突出显示。我想将它添加到每个方面。

## load example iris data
data(iris)

## relabel some points in the data as "unknown"
set.seed(1)
plot_data <- iris
ind <- sample(seq(nrow(iris)), 20)
plot_data$Species <- factor(plot_data$Species, c(levels(plot_data$Species), "unknown"))
plot_data[ind, "Species"] <- "unknown"

## add ids for each point and specifiy colour scheme
plot_data$id <- seq(nrow(plot_data))
cols <- c("pink", "yellow", "blue", "grey")

## make facet plot
g <- ggplot(plot_data, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
  geom_point() +
  scale_color_manual(values = cols) +
  facet_wrap(~ Species) +
  theme(legend.position = "none") 
g

## highlight a point
xx <- plot_data[ind[1], ]
g + geom_point(data = xx, col = "black", pch = 21, lwd = 2)
Run Code Online (Sandbox Code Playgroud)

这会产生这个 在此输入图像描述

我希望在情节的每个方面都有这个突出显示的点(黑色圆圈)。

谢谢。

> sessionInfo()
R version 4.0.0 (2020-04-24)
Platform: x86_64-apple-darwin17.0 (64-bit)
Running under: macOS High Sierra 10.13.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/4.0/Resources/lib/libRlapack.dylib

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

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

other attached packages:
 [1] forcats_0.5.0        stringr_1.4.0        purrr_0.3.4          readr_1.3.1         
 [5] tidyr_1.1.0          tibble_3.0.1         tidyverse_1.3.0      plotly_4.9.2.1      
 [9] reshape2_1.4.4       ggplot2_3.3.1        circlize_0.4.9       shinyhelper_0.3.2   
[13] colorspace_1.4-1     colourpicker_1.0     shinythemes_1.1.2    DT_0.13             
[17] shiny_1.4.0.2        dplyr_1.0.0          pRoloc_1.29.0        BiocParallel_1.22.0 
[21] MLInterfaces_1.68.0  cluster_2.1.0        annotate_1.66.0      XML_3.99-0.3        
[25] AnnotationDbi_1.50.0 IRanges_2.22.2       MSnbase_2.14.2       ProtGenerics_1.20.0 
[29] S4Vectors_0.26.1     mzR_2.22.0           Rcpp_1.0.4.6         Biobase_2.48.0      
[33] BiocGenerics_0.34.0 
Run Code Online (Sandbox Code Playgroud)

teu*_*and 5

除了 zx8754 的答案之外,您可以删除导致分面的列:

g + geom_point(data = subset(xx, select = -Species), col = "black", pch = 21, lwd = 2)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述