gpt4 book ai didi

r - 约会时如何在 X 轴上放大?

转载 作者:行者123 更新时间:2023-12-01 10:10:27 24 4
gpt4 key购买 nike

我正在绘制一个软件版本到它发布的日期。示例:

测试.cvs

Version,Date
0.302,23/2/2011
0.301,26/1/2011
0.215,28/4/2010
0.106,19/12/2008
0.069,21/3/2008

绘制我使用:

tbl <- read.csv("test.csv")
dates <-strptime(as.character(tbl$Date), "%d/%m/%Y")
plot(dates,tbl$Version,type="o",main="Releases", xlab="Date",ylab="Version")

虽然它按年绘制,但我更希望它按月/年绘制,并垂直打印标签。我怎么能做到这一点?我尝试设置 xaxt="n"并将 axis() 函数与 label=format(data,fmt) 一起使用,但我一直失败。

数据片段的输入:

structure(list(Version = c(0.302, 0.301, 0.215, 0.106, 0.069), 
Date = structure(c(3L, 4L, 5L, 1L, 2L), .Label = c("19/12/2008",
"21/3/2008", "23/2/2011", "26/1/2011", "28/4/2010"), class = "factor")), .Names = c("Version",
"Date"), class = "data.frame", row.names = c(NA, -5L))

最佳答案

这是一个基本的图形版本。首先,就地操作 Date 列比生成额外的 dates 对象更容易:

tbl <- within(tbl, Date <- as.Date(Date, "%d/%m/%Y"))

这就是情节。请注意,我们需要在底部留出更多的边距空间来容纳日期标签:

op <- par(mar = c(6,4,4,2) + 0.1) ## larger bottom margin
## plot data but suppress axes and annotation
plot(Version ~ Date, data = tbl, type = "o", axes = FALSE, ann = FALSE)
## Use Axis to plot the Date axis, in 1 month increments
## format the sequence of dates `ds` as abbreviated month name and Year
with(tbl, Axis(Date, at = (ds <- seq(min(Date), max(Date), by = "months")),
side = 1, labels = format(ds, format = "%b %Y"), las = 2))
## Add y-axis and plot frame
axis(2)
box()
## add on the axis labels
title(ylab = "Version", main = "Releases")
title(xlab = "Date", line = 5) ## pushing the x-axis label down a bit
par(op) ## reset the pars

这给了我们:

plot with custom Date axis

通过改变我们想要的日期顺序可以获得更大的灵 active ,在这里我们想要每 2 个月一次,我们用 2 位数的世纪标记它们:

with(tbl, Axis(Date, at = (ds <- seq(min(Date), max(Date), by = "2 months")),
side = 1, labels = format(ds, format = "%b %y"), las = 2))

要使用它,只需在上面的调用中替换现有的 with(....) 语句即可。

关于r - 约会时如何在 X 轴上放大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5733067/

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