如何在ggplot2中仅显示每个类别图中具有值的行?

问题描述 投票:0回答:1

我正在 R 中使用 ggplot2 创建 GO 类别的散点图。但是,我希望每个图(BP、MF、CC)仅显示数据中具有值的行。目前,所有行都显示在每个图中,即使它们没有值。我该如何修改我的代码来实现这一目标?

我的数据的简化示例,我正在使用的代码在这里:

df <- data.frame(
  'Seq ratio' = runif(15, min = 0, max = 0.5),
  'feature' = stringi::stri_rand_strings(15, 1, pattern = "[A-F]"),
  'Seq count' = c(8, 10, 8, 8, 8, 4, 4, 4, 5, 4, 4, 7, 15, 15, 15),
  'adj. P value' = c(0.012609566, 0.012609566, 0.033278332, 0.021875357, 
  0.021875357,0.003216359, 0.003216359, 0.003216359, 0.030076544, 0.017188404, 
  0.003216359, 0.047018661, 0.002584020, 0.002584020, 0.002584020),
  'cluster' = c('Cluster 1', 'Cluster 1', 'Cluster 1', 'Cluster 1', 
  'Cluster 1', 'Cluster 2', 'Cluster 2', 'Cluster 2', 'Cluster 2', 
  'Cluster 3', 'Cluster 3', 'Cluster 4', 'Cluster 4', 'Cluster 4', 'Cluster 4'), 
  'Category' = c('MF', 'MF', 'CC', 'MF', 'CC', 'BP', 'BP', 'BP',
   'BP', 'MF', 'MF', 'BP', 'CC', 'CC', 'CC'))
library(ggplot2)
ggplot(df, aes(`Seq ratio`, feature, size =`Seq count`, colour=`adj. P value`))+
  facet_wrap(~ Category, ncol = 1, ) +
  geom_point() +
  ylab(NULL) +
  coord_cartesian(xlim = c(0, 0.25))+
  scale_x_continuous(n.breaks = 5)+
  scale_size(name = "Seq count", limits = c(0, 15))

例如,在图 BP 中,不显示字母 B,在图 CC 中不显示 E 和 C,在 MF 中不显示 F 和 B。

提前致谢!

r dataframe categories scatter-plot
1个回答
0
投票

scales = "free_y"

library(tidyverse)
library(stringi)
library(janitor)

set.seed(123)

df <- data.frame(
  'Seq ratio' = runif(15, min = 0, max = 0.5),
  'feature' = stri_rand_strings(15, 1, pattern = "[A-F]"),
  'Seq count' = c(8, 10, 8, 8, 8, 4, 4, 4, 5, 4, 4, 7, 15, 15, 15),
  'adj. P value' = c(0.012609566, 0.012609566, 0.033278332, 0.021875357, 
                     0.021875357,0.003216359, 0.003216359, 0.003216359, 0.030076544, 0.017188404, 
                     0.003216359, 0.047018661, 0.002584020, 0.002584020, 0.002584020),
  'cluster' = c('Cluster 1', 'Cluster 1', 'Cluster 1', 'Cluster 1', 
                'Cluster 1', 'Cluster 2', 'Cluster 2', 'Cluster 2', 'Cluster 2', 
                'Cluster 3', 'Cluster 3', 'Cluster 4', 'Cluster 4', 'Cluster 4', 'Cluster 4'), 
  'Category' = c('MF', 'MF', 'CC', 'MF', 'CC', 'BP', 'BP', 'BP',
                 'BP', 'MF', 'MF', 'BP', 'CC', 'CC', 'CC'))

df |> 
  clean_names() |> 
  ggplot(aes(seq_ratio, feature, size = seq_count, colour = adj_p_value)) +
  facet_wrap(~category, ncol = 1, scales = "free_y") +
  geom_point() +
  ylab(NULL) +
  scale_x_continuous(n.breaks = 5) +
  scale_size(name = "Seq count", limits = c(0, 15))

创建于 2024-03-11,使用 reprex v2.1.0

© www.soinside.com 2019 - 2024. All rights reserved.