r - 将选定的线保留在 x Axis 上,并在 Y Axis 上的特定部分标记

标签 r formatting ggplot2 axis

这是我的计划

dat <- data.frame(
  pos = c(1, 3, 5, 8, 10, 12),
  start = c(1,3, 6, 7, 10, 11),
  end = c(5, 6, 9, 9, 13, 12)
)

library(ggplot2)
p <- ggplot(dat) + geom_segment(aes(x=start, y=pos, xend=end, yend=pos), 
       color="blue", size=2) + ylab("Fragments)") +  xlab("Position")
    scale_y_reverse() + theme_bw()

p1 <- p + opts(legend.position="left",
        panel.background=theme_blank(),panel.border=theme_blank(),
        panel.grid.major=theme_blank(),
        panel.grid.minor=theme_blank(),plot.background=theme_blank())


p1

enter image description here

所需的位图版本是,在线段附近有轴线和标签。 [只是额外注意:请注意,位图将线更改为圆角(看看我们是否可以在 ggplot2 中这样做会很有趣)]

enter image description here

最佳答案

有趣的是,我实际上认为这在基础图形中更容易:

plot(c(0,13),c(1,12),type = "n",axes = FALSE,xlab = "Position",ylab = "")
segments(x0 = dat$start,
         y0 = dat$pos,
         x1 = dat$end,
         y1 = dat$pos,
         col = "blue",
         lwd = 6,
         lend = 2)
text(x = dat$start - 0.5,y = dat$pos,labels = dat$pos,font = 2)
axis(1)
axis(1,at = c(0,12),labels = FALSE,tcl = 0.5)

编辑 添加了额外的 axis 调用以在两个方向上获取最外层的刻度。

enter image description here

关于r - 将选定的线保留在 x Axis 上,并在 Y Axis 上的特定部分标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10434108/

相关文章:

api - 如何使用 Google Embed API 格式化数字 - 小数位限制 - 整数

r - ggplot : multiple lines starting from the same point

r - ggplot2:为 x 轴上的每个元素以及整个样本创建条形图

r - 转换后 R 管道运算符的行为

sql - MySQL 工作日/周末计数 - 第二部分

text - 复制和粘贴时撇号会发生变化

r - 使用 rollsum 并组合两个数据框

r - 使用 lapply 嵌套列表

r - 将 Tukey 的重要性字母添加到箱线图中

r - 如何摆脱ggplot geom_hex中的边界线