一种可能不那么优雅的方式如下:
#random data
DF <- data.frame(range = c("10 - 20", "28 - 30", "3 - 8",
"100 - 180", "185 - 190", "200 - 350"),
type = rep(c("helix", "beta", "turn"), 2), stringsAsFactors = F)
#manipulate "from - to" numerically
newDF <- cbind(DF, do.call(rbind, strsplit(DF$range, " - ")), stringsAsFactors = F)
names(newDF) <- c(names(DF), "xleft", "xright") #name the columns
newDF$"xleft" <- as.numeric(newDF$"xleft") #make the values numeric
newDF$"xright" <- as.numeric(newDF$"xright") # -//-
#> newDF
# range type xleft xright
#1 10 - 20 helix 10 20
#2 28 - 30 beta 28 30
#3 3 - 8 turn 3 8
#4 100 - 180 helix 100 180
#5 185 - 190 beta 185 190
#6 200 - 350 turn 200 350
#color for each type
colors. <- c(rgb(0,1,0,1/3), rgb(0,0,1,1/3), rgb(1,0,0,1/3))[as.factor(newDF$type)]
#background plot
plot(NULL, xlim = c(0, max(newDF[c("xleft", "xright")])),
ylim = c(0,1), yaxt = "n", ann = F)
#legend
legend(x = max(newDF[c("xleft", "xright")]) * 0.5, y = 1.15, xpd = T, ncol = 3,
legend = c("beta", "helix", "turn"),
fill = c(rgb(0,1,0,1/3), rgb(0,0,1,1/3), rgb(1,0,0,1/3)))
#function that draws rectangles for each range
fun <- function(xl, yb, xr, yt, col.)
{
rect(xleft = xl, ybottom = yb, xright = xr, ytop = yt, col = col.)
}
#draw all rectangles
mapply(fun, xl = newDF$xleft, xr = newDF$xright, col. = colors.,
MoreArgs = list(yb = 0, yt = 1))
该图绘制成方形,但您可以通过拖动其框或将其提取到jpeg
具有所需尺寸的(或任何东西)中来更改其尺寸: