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