2

这是我的情节

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

在此处输入图像描述

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

在此处输入图像描述

4

2 回答 2

4

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

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的调用以在两个方向上获取最外层的刻度。

在此处输入图像描述

于 2012-05-03T15:25:11.107 回答
3

编辑:更新ggplot2版本 0.9.3.1 的代码。

使用最新版本的 ggplot2,这项任务要容易得多。以下代码完成了所有工作:

# Load required packages
library(ggplot2)

# Your data
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) )

# Get the  plot
p <- ggplot(dat) + 
   geom_segment(aes(x=start, y=pos, xend=end, yend=pos), 
         color="blue", size=2, lineend = "round") + 
   ylab("Fragments") +  xlab("Position") + 
   theme_bw() +
   geom_text(aes(label = pos, x = start, y = pos), hjust = 1.7) +
   scale_x_continuous(breaks = seq(0,14,2), labels = seq(0,14,2), expand = c(0,0)) +
   scale_y_continuous(limits = c(-1, 14), expand = c(0,0))  +
   geom_hline(yintercept = -1) +
   geom_segment(aes(x = 0, y = -1, xend = 0, yend = -0.9)) +
   geom_segment(aes(x = 14, y = -1, xend = 14, yend = -0.9)) +
   theme(panel.grid.major=element_blank(),
       panel.grid.minor=element_blank(),
       panel.border=element_blank(),
       axis.ticks.y = element_blank(), 
       axis.title.y = element_blank(),
       axis.text.y = element_blank())
p

原答案:

它可以完成ggplot2,有点摆弄。需要包中的函数grid()来删除 y 轴刻度线。

# Load required packages
library(ggplot2)
library(grid)

# Your data
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) )

# Get the base plot
p <- ggplot(dat) + 
   geom_segment(aes(x=start, y=pos, xend=end, yend=pos), 
   color="blue", size=2) + ylab("Fragments") +  xlab("Position") + theme_bw() +
   geom_text(aes(label = pos, x = start, y = pos), hjust = 1.7) +
   scale_x_continuous(breaks = seq(0,14,2), labels = seq(0,14,2), expand = c(0,0)) +
   scale_y_continuous(limits = c(-1, 14), expand = c(0,0))  +
   geom_hline(yintercept = -1) +
   geom_segment(aes(x = 0, y = -1, xend = 0, yend = -0.9)) +
   geom_segment(aes(x = 14, y = -1, xend = 14, yend = -0.9)) +
   opts(panel.grid.major=theme_blank(),
       panel.grid.minor=theme_blank(),
       panel.border=theme_blank(),
       axis.title.y = theme_blank(),
       axis.text.y = theme_blank())
p

# Remove the y-axis tick marks
g <- ggplotGrob(p)# Save plot as a grob
#grid.ls(g) 
grid.remove(grid.get("axis.ticks", grep=T, global = TRUE)[[1]]$name)

结果:

在此处输入图像描述

通过更多的摆弄,您可以在线段上获得圆形末端。它需要proto安装软件包。然后运行从这里获得的一些代码以启用一个新的几何图形geom_segment2,该几何图形采用“行结束”参数。

    # To create the new `geom_segment2`
library(proto)
GeomSegment2 <- proto(ggplot2:::GeomSegment, {
 objname <- "geom_segment2"
 draw <- function(., data, scales, coordinates, arrow=NULL, ...) {
   if (is.linear(coordinates)) {
     return(with(coord_transform(coordinates, data, scales),
       segmentsGrob(x, y, xend, yend, default.units="native",
       gp = gpar(col=alpha(colour, alpha), lwd=size * .pt,
         lty=linetype, lineend = "round"),
       arrow = arrow)
     ))
   }

}})

geom_segment2 <- function(mapping = NULL, data = NULL, stat =
"identity", position = "identity", arrow = NULL, ...)  {
  GeomSegment2$new(mapping = mapping, data = data, stat = stat,
        position = position, arrow = arrow, ...)
} 

    # The base plot
p <- ggplot(dat) + 
       geom_segment2(aes(x=start, y=pos, xend=end, yend=pos), 
       color="blue", size=2, lineend = "round") + ylab("Fragments") +  xlab("Position") + theme_bw() +
       geom_text(aes(label = pos, x = start, y = pos), hjust = 1.7) +
       scale_x_continuous(breaks = seq(0,14,2), labels = seq(0,14,2), expand = c(0,0)) +
       scale_y_continuous(limits = c(-1, 14), expand = c(0,0))  +
       geom_hline(yintercept = -1) +
       geom_segment(aes(x = 0, y = -1, xend = 0, yend = -0.9)) +
       geom_segment(aes(x = 14, y = -1, xend = 14, yend = -0.9)) +
       opts(panel.grid.major=theme_blank(),
           panel.grid.minor=theme_blank(),
           panel.border=theme_blank(),
           axis.title.y = theme_blank(),
           axis.text.y = theme_blank())
p

## Remove the y-axis tick marks
g <- ggplotGrob(p)
#grid.ls(g) 
grid.remove(grid.get("axis.ticks", grep=T, global = TRUE)[[1]]$name)

结果:

在此处输入图像描述

于 2012-05-04T22:30:01.687 回答