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

mwecs4sa  于 2023-03-20  发布在  其他
关注(0)|答案(1)|浏览(269)

下面是我的数据框。

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

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

# 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()
        )

这给出了下面的图:

现在,
1.我想调整坐标轴刻度标签(1 Kb,2Kb,... 10 Kb)的位置,使它们位于刻度圈内,这是为了防止与原始数据集中长度较长的基因名称重叠。
1.另外,我想知道是否可以将矩形的形状更改为箭头,如下图所示。

先谢谢你了!

qvtsj1bj

qvtsj1bj1#

将轴文本标签放置在圆内的一个选项是通过geom_text图层添加标签。此外,对于箭头形状,您可以切换到geom_segment

library(ggplot2)
library(ggrepel)
library(scales)

dat_x <- data.frame(
  x = scales::pretty_breaks(n = 10)(range(c(filld$start, filld$end)))
)

ggplot(filld) + #
  geom_segment(aes(x = start, xend = end - 100, y = 4.5, yend = 4.5, color = annotation),
    linewidth = 5, linejoin = "square",
    arrow = arrow(type = "closed", angle = 30),
    key_glyph = "rect"
  ) +
  scale_x_continuous(
    label = label_bytes("kB"), breaks = scales::pretty_breaks(n = 10),
    expand = c(0, 0)
  ) +
  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(
    data = dat_x,
    aes(x = x, y = 3.75, label = label_bytes("kB")(x)),
    vjust = 1,
    inherit.aes = FALSE
  ) +
  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
  ) +
  scale_y_continuous(limits = c(0, 5.5), expand = c(0, 0)) +
  coord_polar() +
  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(),
    legend.position = "right",
    panel.background = element_rect(fill = "white"),
    panel.border = element_blank(),
    panel.grid.minor = element_blank()
  )

相关问题