根据行数调整 ggplot2facet_wrap 绘图大小

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

我在 R 中使用 ggplot2 使用 facet_wrap() 来可视化数据,我想调整图形的大小,以便行数较多的图形(示例中的 MF 和 CC)显得更大,而行数较少的图形( BP)较小。但整个图表的大小并没有改变。这是数据示例:

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

r ggplot2 facet-wrap
1个回答
0
投票

facet_grid
提供了根据各个面的比例缩放面的能力。

ggplot(df, aes(Seq.ratio, feature, size = Seq.count, colour = adj..P.value)) +
  facet_grid(vars(Category), scales = "free_y", space = "free_y") +
  geom_point() +
  ylab(NULL) +
  scale_x_continuous(n.breaks = 5) +
  scale_size(name = "Seq count", limits = c(0, 15))

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