gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 15:03:16 24 4
gpt4 key购买 nike

我正在尝试创建如下所示的情节(很多时候我最终都会手工绘制这样的情节,但这次我想自己绘制)。

enter image description here

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

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)

enter image description here

我遇到了问题,特别是 Axis 与绘图以及文本和点的方向的匹配。任何其他想法或改进帮助表示赞赏。

最佳答案

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

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)

enter image description here

关于r - 在 r 中创建带有时间刻度和标签的线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13956023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com