在r中创建带有时间刻度和标签的线图

在r中创建带有时间刻度和标签的线图,r,plot,axis,R,Plot,Axis,我试图创建如下图(很多时候我都是手工绘制这样的图,但这次我想自己绘制) 这是我的数据和我的试验: 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", "B

我试图创建如下图(很多时候我都是手工绘制这样的图,但这次我想自己绘制)

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

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)

myd您可以尝试以下方法开始:

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以下是一些增强功能(我建议现在添加0只是为了更好地扩展规模):


myd要绘制三角形,请查看TeachingDemos软件包中的
my.symbols
ms.polygon
函数

在右上方的图表中,恐龙被向上移动,如果您希望这样做(移动标签,否则会太近或重叠),请查看TeachingDemos包中的
spread.labs
函数


其他一些可能有助于绘图的功能包括
text
mtext
grconvertX
grconvertY
,和

,用于“从头开始”构建新绘图,并最大限度地控制单个图形元素,网格图形系统很难击败:

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)
库(网格)
##设置具有合理x-y限制的绘图区域
##以及与数据规模相关的“本地”规模。

有关箭头绘画的内容,请参见Ananad Mahato的答案。@Josh O'Brien,谢谢,也可以用上一节中列出的线条呈现替代版本revisions@sharnil”“当然。我只是把它们以注释的形式放回去。(实际上我更喜欢他们更干净的外观,但把“指针”版本作为一个有趣的挑战)谢谢你的提问@谢谢,我有个小问题-我们如何改变网格中线条的厚度。lwd不起作用。@sharnil--您需要通过
gp=
(“图形参数”)参数传入
lwd
参数。(实际上,在我的
指针
函数定义中有一个示例。)有关以这种方式传递的参数的完整列表,请参见
?gpar
。这种风格需要一点时间来适应,但您很快就会意识到,所有图形原语都接受相同格式的相同参数集@sharnil——如果您有兴趣学习一点关于网格的知识,您应该了解它附带的有用的小插曲。尝试
browseVignettes(package=“grid”)
查看它们。名为“网格简介”的小插曲是一个很好的开始。
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)