簡體   English   中英

合並兩個具有共同 x 軸的 coord_polar 圖並添加 x 軸比例尺

[英]Merge two coord_polar plots with common x axis and add x axis scale bar

我希望創建一個 plot ,其中一個圓形 coord_polar plot 在另一個內部,共享 x 軸但具有不同的 y 軸。 另外,我想在圓形 plot 的內部添加一個比例尺,表示 x 軸的值。 下面是一些測試數據:

library(dplyr)
library(ggplot2)

test_data <- data.frame(start = c(1, 200, 450, 600, 800),
                        end = c(150, 440, 570, 780, 1200),
                        gene_name = c("gene1", "gene2", "gene3", "gene4", "gene5"),
                        sample = 1,
                        gc = c(50.9, 49.8, 50.0, 51.0, 48.0)) %>%
  mutate(gene_length = end - start,
         pos = start + (gene_length/2)) %>%
  select(-gene_length)


p1 <- ggplot(test_data, aes(x = pos, y = gc)) +
  geom_col() +
  scale_y_continuous(limits = c(-60, 52)) +
  coord_polar()


p2 <- ggplot(test_data, aes(x = pos, fill = gene_name)) +
  geom_hline(aes(yintercept = sample + 0.4)) +
  geom_rect(aes(xmin = start, xmax = end, ymin = sample, ymax = sample + 0.8),
            color = "black",
            size = 0.01) +
  scale_y_continuous(limits = c(-10, 2)) +
  coord_polar()

p1

p1 看起來像這樣

p2

p2 看起來像這樣

我想要的是一個看起來有點像這樣的 plot (對不起,糟糕的油漆圖片): 在此處輸入圖像描述

主plot外面的黑色就是p1代表的barplot。 我希望每個條的寬度與下面 plot 中它們各自的元素的大小相對應。 我曾嘗試將它們與cowplot 或aplot 等工具合並,但似乎都無法將它們相互融合。 他們寧願將它們放在彼此之上或之下。

為什么不將兩個幾何圖形放在一個 plot 中,而不是嘗試合並兩個圖? 注意要強調gc的區別

我已經在 ymin = 47 處開始了灰色區域,但如果你願意,你可以將它們放回 0 並使內環處於負 y 值(因為它的 y 位置似乎是任意的)。

p1 <- ggplot(test_data, aes(x = pos, y = gc)) +
  geom_rect(aes(xmin = start, xmax = end, ymin = 47, ymax = gc)) +
  geom_hline(aes(yintercept = sample + 0.4)) +
  geom_rect(aes(fill = gene_name, xmin = start, xmax = end, 
                ymin = sample, ymax = sample + 1),
            color = "black",
            size = 0.01) +
  scale_y_continuous(limits = c(30, 52)) +
  scale_x_continuous(limits = c(0, 1250)) +
  coord_polar()

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM