R 将选定的线保持在x轴上,并在Y轴上的特定线段上添加标签

R 将选定的线保持在x轴上,并在Y轴上的特定线段上添加标签,r,formatting,ggplot2,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) +

这是我的阴谋

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

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

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)
编辑添加了额外的
调用,以获取两个方向上最外侧的刻度


编辑:更新
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
结果是:

通过更多的摆弄,您可以在线段上获得圆形端点。它需要安装
proto
软件包。然后运行从中获得的一些代码,以启用一个新的geom
geom_段2
,该段采用“line end”参数

    # 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)
#创建新的“geom”分段2`
图书馆(原型)

GEOMSECTION2可以在
ggplot2
中完成,即使是圆线端。
    # 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)