在R中的ggplot2中将点添加到所有构面的最佳方法 [英] Best way to add points to ALL facets in ggplot2 in R

查看:46
本文介绍了在R中的ggplot2中将点添加到所有构面的最佳方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Can someone please tell me the best way to add the same point to every facet of a below plot?

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

For example, below, if I choose one of the points labelled as "unknown" I am able to call geom_point after making the plot facets but (as it is labelled "unknown") it is only highlighted on the 4th facet. I would like to add it to every facet.

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

这产生了这个

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

I would like this highlighted point (black circle) on every facet of the plot.

谢谢.

> 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 

推荐答案

为每个方面(要素级别)重复 xx 对象:

Repeat xx object for each facet (factor level):

xx <- plot_data[rep(ind[1], length(levels(plot_data$Species))), ]
xx$Species <- unique(plot_data$Species)
g + geom_point(data = xx, col = "black", pch = 21, lwd = 2)

这篇关于在R中的ggplot2中将点添加到所有构面的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆