編集: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
て、「行末」引数を取る新しい geomを使用できるようにします。
# 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)
結果: