将离散的y轴高度调整为刻面中的段数

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

我试图绘制生物体所面对的不同蛋白质序列中有趣片段的概述。

每个小关节/生物体可能含有不同数量的蛋白质 - 长黑色片段。每种蛋白质都有一个相同长度的较短片段的彩色覆盖层,可以重叠 - 颜色代表患者组。

我首先遇到了不同小平面上y轴上蛋白质/区段之间的间距差异的问题。我设法使用ggplot:coord_fixed函数使用指定的比率来解决这个问题。然而,每个小平面中的y轴的高度仍然不适合分段的数量。此外,coord_fixed在尝试facet_wrap(scales = "free_y")时会抛出错误,因为它不允许自由轴。

如何移除y轴上的额外间距/控制每个面内y轴的高度?

以下是一些示例代码:

library(ggplot2)
library(dplyr)
d_list <- lapply(paste("protein", seq(1,100,1)), function(protein){
  #The full length the protein
  prot_length <- sample(seq(100,500,1), size = 1)

  #The organism the protein belongs to
  org_name <- sample(paste("organism", seq(1,5,1), sep = "_"), 1)

  #The start and end of the segments of interest - 15 amino acids long
  start <- sample(seq(1,prot_length-14,1),sample(1:20,1))
  end <- start + 14

  #The patient/group the segments of interest originate from
  group <- sample(paste("patient", seq(1,3,1), sep = "_"), length(start), T)

  data.frame(protein_name = rep(protein,length(start)),
             protein_length = rep(prot_length, length(start)),
             start = start,
             end = end,
             organism_name = rep(org_name,length(start)),
             group = group)
})

d <- do.call("rbind", sample(d_list, 20))

d %>%
  arrange(., organism_name, desc(protein_length)) %>%
  mutate(., protein_name = factor(protein_name, levels = unique(protein_name))) %>%
  ggplot(., aes(x = 1, xend = protein_length, y = protein_name, yend = protein_name)) +
  geom_segment(color = rgb(0,0,0), size = 1) +
  geom_segment(aes(x = start, xend = end, y = protein_name, yend = protein_name, color = as.factor(group)),
               size = 0.7) +
  scale_x_continuous(breaks = seq(0,500,100), labels = seq(0,500,100)) +
  scale_y_discrete(label = NULL, drop = T) +
  scale_color_manual(values = c("firebrick1", "dodgerblue1", "darkgoldenrod1")) +
  facet_wrap(~organism_name, ncol = 1, drop = T) +
  theme_minimal() + 
  labs(color = "Group", y = "Proteins", x = "Amino Acid Position") +
  theme(axis.title.x = element_text(size = 15, face = "bold", vjust = 0.5), 
        axis.text.x = element_text(size = 12), 
        panel.grid.minor.x = element_blank(),
        axis.title.y = element_text(size = 15, face = "bold", vjust = 0.5), 
        panel.grid.major.y = element_blank(), 
        panel.grid.minor.y = element_blank(),
        legend.title = element_text(size = 15, face = "bold"), 
        legend.text = element_text(size = 12)) +
  coord_fixed(ratio = 2)
r ggplot2 facet-wrap
1个回答
1
投票

编辑将facet_wrap的条带位置与facet_grid的免费面板尺寸相结合

(注意:我增加了分段大小,因为它们很难看到...)

# data d was created with set.seed(123)

# generate plot without either facet option
p <- d %>%
  arrange(organism_name, desc(protein_length)) %>%
  mutate(protein_name = factor(protein_name, 
                               levels = unique(protein_name))) %>%      
  ggplot(aes(x = 1, xend = protein_length, 
             y = protein_name, yend = protein_name)) +
  geom_segment(color = rgb(0, 0, 0), size = 4) +
  geom_segment(aes(x = start, xend = end, y = protein_name, 
                   yend = protein_name, color = as.factor(group)),
               size = 3) +
  scale_x_continuous(breaks = seq(0,500,100), labels = seq(0,500,100)) +
  scale_y_discrete(label = NULL, drop = T) +
  scale_color_manual(values = c("firebrick1", "dodgerblue1", "darkgoldenrod1")) +
  theme_minimal() + 
  labs(color = "Group", y = "Proteins", x = "Amino Acid Position") +
  theme(axis.title.x = element_text(size = 15, face = "bold", vjust = 0.5), 
        axis.text.x = element_text(size = 12),
        panel.grid.minor.x = element_blank(),
        axis.title.y = element_text(size = 15, face = "bold", vjust = 0.5), 
        panel.grid.major.y = element_blank(), 
        panel.grid.minor.y = element_blank(),
        legend.title = element_text(size = 15, face = "bold"), 
        legend.text = element_text(size = 12))

# create two versions of the plot using facet_grid / facet_wrap, 
# with scales set to "free_y" for both, but also space = "free_y" for facet_grid
# (facet_wrap doesn't have this option)
p.grid <- p + facet_grid(organism_name ~ ., scales = "free_y", space = "free_y")
p.wrap <- p + facet_wrap(~ organism_name, ncol = 1, scales = "free_y")

# convert both into grob objects
gp.grid <- ggplotGrob(p.grid)
gp.wrap <- ggplotGrob(p.wrap)

# apply the panel heights of the facet_grid version to the facet_wrap one
gp.wrap$heights[gp.wrap$layout[grep("panel", gp.wrap$layout$name), "t"]] <- 
  gp.grid$heights[gp.grid$layout[grep("panel", gp.grid$layout$name), "t"]]

# plot the facet_wrap version
grid::grid.draw(gp.wrap)

2nd plot

原始答案

听起来你可能正在寻找facet_grid而不是facet_wrap。如果你将scalesspace都设置为"free_y",它允许轴断裂和刻面高度变化:

d %>%
  arrange(organism_name, desc(protein_length)) %>%
  mutate(protein_name = factor(protein_name, 
                               levels = unique(protein_name))) %>%      
  ggplot(aes(x = 1, xend = protein_length, 
             y = protein_name, yend = protein_name)) +
  geom_segment(color = rgb(0, 0, 0), size = 1) +
  geom_segment(aes(x = start, xend = end, y = protein_name, 
                   yend = protein_name, color = as.factor(group)),
               size = 0.7) +
  scale_x_continuous(breaks = seq(0,500,100), labels = seq(0,500,100)) +
  scale_y_discrete(label = NULL, drop = T) +
  scale_color_manual(values = c("firebrick1", "dodgerblue1", "darkgoldenrod1")) +
  facet_grid(organism_name ~ ., drop = T,
             scales = "free_y", space = "free_y") +
  theme_minimal() + 
  labs(color = "Group", y = "Proteins", x = "Amino Acid Position") +
  theme(axis.title.x = element_text(size = 15, face = "bold", vjust = 0.5), 
        axis.text.x = element_text(size = 12),
        panel.grid.minor.x = element_blank(),
        axis.title.y = element_text(size = 15, face = "bold", vjust = 0.5), 
        panel.grid.major.y = element_blank(), 
        panel.grid.minor.y = element_blank(),
        legend.title = element_text(size = 15, face = "bold"), 
        legend.text = element_text(size = 12))

plot

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