调整 ggplot 中轴刻度标签的位置

Pra*_*ama 2 r ggplot2

下面是我的数据框。

structure(list(start = c(1, 899, 1648, 2024, 4101, 6001, 7124, 
8982), end = c(898, 1647, 2023, 4100, 6000, 7123, 8981, 10000
), gene = c("A", "B", "C", "X", "P", "N", "O", "F"), annotation = c("Replicase", 
"transcription", "Metabolic Pathways", "Metabolic Pathways", 
"transcription", "transcription", "Antibiotic Resistance", "transcription"
), direction = c(1, 1, 1, -1, 1, -1, 1, 1), p = c(449.5, 1273, 
1835.5, 3062, 5050.5, 6562, 8052.5, 9491)), row.names = c(NA, 
-8L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)

这是我到目前为止编写的代码。

# Mean
filld$p <- rowMeans(subset(filld, select = c(start, end)))

# Plot
ggplot(filld, aes(xmin = start, xmax = end, ymin = 4, ymax = 5, fill = annotation)) + # 
  geom_rect() +
  scale_x_continuous(label = label_bytes("kB"), breaks = scales::pretty_breaks(n = 10)) +
  geom_segment(data = subset(filld, gene %in% gene[duplicated(gene)]),
               aes(x = p, y = 0, xend = p, yend = 4, colour = annotation),
               size = 2) +
  geom_text_repel(aes(x = p, y = 4.5,
                     label = gene, 
                     family = 'Roboto Condensed',
                  ),  nudge_x = .15,
                  box.padding = 0.5,
                  nudge_y = 1,
                  segment.curvature = -0.1,
                  segment.ncp = 3,
                  segment.angle = 20,
                  size=3.5, min.segment.length = 0, max.overlaps = Inf) +
  coord_polar() + 
  scale_y_continuous(limits = c(0, 5.5)) +
  theme_classic() +
  theme(axis.line = element_blank(), 
        #axis.text.x = element_blank(), 
        axis.text.y = element_blank(),
        axis.ticks = element_blank(), 
        axis.title.x = element_blank(), 
        axis.title.y = element_blank(), 
        #axis.ticks.margin = unit(c(0,0,0,0), "lines"), 
        legend.position = "right", 
        panel.background = element_rect(fill = "white"), 
        panel.border = element_blank(), 
        #panel.grid.major = element_blank(), 
        panel.grid.minor = element_blank()
        )
Run Code Online (Sandbox Code Playgroud)

这给了我以下情节:

在此输入图像描述

现在,

  1. 我想调整轴刻度标签(1Kb、2Kb、..10Kb)的位置,使它们位于带有刻度的圆圈内。这是为了防止与原始数据集中长度可能较长的基因名称发生任何重叠。

  2. 另外,我想知道是否可以将矩形的形状更改为箭头,如下图所示。

在此输入图像描述

预先非常感谢您!

All*_*ron 5

我可能会在数据整形和轴伪造方面投入一些工作。使用geom_textpath,我们可以通过直接标记获得一些相当专业的结果,而不需要大量代码:

library(tidyverse)
library(geomtextpath)

filld %>%
  group_by(gene, annotation) %>%
  summarise(x = c(start, end - c(200, 200, 20, 200, 200), start),
            y = c(17, 17, 16, 18, 20, 19, 19)/4) %>%
  ggplot(aes(x, y)) + 
  geom_polygon(aes(fill = annotation, group = gene), 
               color = "black", linewidth = 0.3) +
  geom_textpath(aes(label = paste("gene", gene), x = p - 100, y = 5.2), 
                data = filld) +
  geom_hline(yintercept = 3.9, color = "gray60") +
  geom_line(aes(group = group), 
            data = data.frame(x = rep(0:9 * 1e3, each = 2),
                              y = rep(c(3.7, 3.9), 10),
                              group = rep(1:10, each = 2))) +
  geom_textpath(aes(label = paste(x, "kB")),
                data = data.frame(x = seq(0, 9000, 1000), y = 3.5)) +
  theme_void(base_size = 16) +
  scale_fill_manual(values = c("#c00000", "#fdc000", "#308298", "#d8d8d8")) +
  ylim(0, 5.5) +
  xlim(0, 10000) +
  coord_polar()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

  • @tjebo 这只是 R 在 Windows 中使用的标准衬线字体。该设备只是“破布”。在本例中,它是直接从 RStudio 绘图查看器窗口复制的。 (2认同)
  • @tjebo 我愿意,而且我强烈推荐它:) (2认同)