15

我正在尝试创建如下图(很多时候我最终会手动绘制这样的图,但这次我想自己绘制)。

在此处输入图像描述

这是我的数据和我的试验:

myd <- data.frame (period = c("Triassic", "Jurasic", 
 "Cretaceous", "Cenzoic"), myears = c(245, 208, 145, 65), 
 label = c(226, 176,105, 32 ))
myd2 <- data.frame (event = c("Diansaurs_strt", "Birds", 
  "Diansaurs_ext", "Human"), myears = c(235, 200, 60, 0.5))
myd2$x <- -0.25
with (myd2, plot(x,myears,ylim=c(0,250),
xlim = c(0, 10), axes=F,xlab="",ylab="",type="p",pch=17))
with (myd2,text(x,myears,event,pos=4,xpd=T))
axis(side=2,at = myd$label, labels = myd$period)

在此处输入图像描述

我有问题特别是轴与文本和点的绘图和方向匹配。任何其他想法或改进帮助表示赞赏。

4

4 回答 4

16

对于“从头开始”构建新颖的情节,以及对单个图形元素的最大控制,网格图形系统很难被击败:

library(grid)

## Set up plotting area with reasonable x-y limits
## and a "native" scale related to the scale of the data.
x <- -1:1
y <-  extendrange(c(myd$myears, myd2$myears))
dvp <- dataViewport(x, y, name = "figure")

grid.newpage()
pushViewport(dvp)

## Plot the central timeline
grid.lines(unit(0, "native"), unit(c(0,245), "native"),
           gp = gpar(col="dodgerblue"))

## Annotate LHS
grid.segments(x0=0.5, x1=0.47,
              y0=unit(c(0, myd$myears), "native"),
              y1=unit(c(0, myd$myears), "native"),
              gp=gpar(col="dodgerblue"))
grid.text(label=c(0, myd$myears), x=0.44, y=unit(c(0, myd$myears), "native"))
grid.text(label=myd$period, x=0.3, y=unit(myd$label, "native"),
          just=0, gp=gpar(col="dodgerblue", fontface="italic"))

## Annotate RHS
## Create a function that plots a pointer to the specified coordinate
pointer <- function(x, y, width=1) {
    grid.polygon(x = x + unit(width*(c(0, .1, .1)), "npc"),
                 y = y + unit(width*(c(0, .03, -.03)), "npc"), 
                 gp = gpar(fill="dodgerblue", col="blue", lwd=2))
}
## Call it once for each milestone
for(y in myd2$myears) {
    pointer(unit(.5, "npc"), y=unit(y, "native"), width=0.3)
}
## Or, if you just want blue line segments instead of those gaudy pointers:
## grid.segments(x0=0.5, x1=0.53,
##           y0=unit(c(myd2$myears), "native"),
##           y1=unit(c(myd2$myears), "native"), gp=gpar(col="dodgerblue"))

grid.text(label=myd2$event, x=0.55, y=unit(myd2$myears, "native"),
          just=0)

在此处输入图像描述

于 2012-12-19T17:18:08.613 回答
5

您可以尝试这样的事情来帮助您入门:

myd <- data.frame(period = c("", "Triassic", "Jurasic", 
                             "Cretaceous", "Cenzoic", ""), 
                  myears = c(260, 245, 208, 145, 65, -5), 
                  label = c(260, 226, 176,105, 32, -5))
myd2 <- data.frame(event = c("Dinosaurs_strt", "Birds", 
                             "Dinosaurs_ext", "Human"), 
                   myears = c(235, 200, 60, 0.5))
myd2$x <- 1
with(myd2, plot(x, myears, ylim = c(-5, 250), xlim = c(0, 10), 
                axes = FALSE, xlab = "", ylab = "", type = "n"))
with(myd2, text(x, myears, event, pos = 4, xpd = TRUE))
axis(side = 2, at = myd$label, labels = myd$period, las = 2)
X0 <- rep(myd2$x, 4)
Y0 <- myd2$myears
X1 <- rep(-.25, 4)
Y1 <- Y0
arrows(X0, Y0, X1, Y1)

在此处输入图像描述

我在“myd”中数据的开头和结尾添加了一个额外的空元素来帮助轴。然后,pch我习惯于arrows将右手标签与轴匹配,而不是使用 。

一些调整可能会使它看起来更好。

于 2012-12-19T16:30:16.693 回答
3

以下是一些增强功能(我建议现在添加 0 以更好地扩展):

myd <- data.frame (period = c("Triassic", "Jurasic", 


 "Cretaceous", "Cenzoic", "now"), myears = c(245, 208, 145, 65, 0), 
    label = c(226, 176,105, 32, NA ))
    myd2 <- data.frame (event = c("Diansaurs_strt", "Birds", "Diansaurs_ext", "Human"),
    myears = c(235, 200, 60, 0.5))
    myd2$x <- -0.25
    with (myd2, plot(x,myears,ylim=c(0,250), xlim = c(0, 10), 
    axes=F,xlab="",ylab="",type="p",pch=17, col = "green"))
    with (myd2, plot(x,myears,ylim=c(0,250), 
    xlim = c(0, 10), axes=F,xlab="",ylab="",type="p",pch="-", col = "green"))
    with (myd2,text(x,myears,event,pos=4,xpd=T), col = "green")
    axis(side=2,at = myd$label, labels = myd$period, tick = FALSE, 
    las = 2, col = "green", )
    axis(side=2,at = myd$myears, labels = myd$myears,  las = 2, col = "green")

在此处输入图像描述

剩下的问题很少,您可能想要更改箭头的方向(我相信您可以以某种方式找到 <- 符号,但我不知道如何)。

于 2012-12-19T16:31:10.483 回答
0

要绘制三角形,请查看 TeachingDemos 包中的my.symbolsms.polygon函数。

在你上面的右图中,恐龙被向上移动,如果你想要这个(移动标签,否则会太靠近或重叠)然后查看spread.labsTeachingDemos 包中的函数。

其他一些可能有助于绘图的函数是textmtextgrconvertXgrconvertYsegmentsaxis

于 2012-12-19T16:56:22.227 回答