【问题标题】:How do I zoom in on X axis when it's a date?日期时如何放大 X 轴?
【发布时间】:2011-04-20 15:45:24
【问题描述】:

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

test.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" 并使用带有 label=format(data,fmt) 的 axis() 函数,但我一直失败。

数据sn-p的输入:

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))

【问题讨论】:

    标签: r


    【解决方案1】:

    这是一个基本的图形版本。首先,就地操作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
    

    这给了我们:

    通过改变我们想要的日期顺序可以获得更大的灵活性,这里我们想要每 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(....) 语句即可。

    【讨论】:

      【解决方案2】:

      为轴标签创建一系列日期。

      start <- as.Date("01/01/2008", "%d/%m/%Y")
      end <- as.Date("01/12/2011", "%d/%m/%Y")
      x_breaks <- seq(start, end, by = "month")
      

      dates 创建为Date 以匹配上述序列。

      dates <- as.Date(as.character(tbl$Date), "%d/%m/%Y")
      

      设置一些图形参数,las = 3 旋转你的 x 轴; mar 更改边距宽度。

      par(las = 3, mar = c(7, 5, 3, 1))
      

      现在绘制它,并按照您的建议手动添加 x 轴。

      plot(dates,tbl$Version,type="o",main="Releases", xlab="", ylab="Version", xaxt = "n")
      axis(side = 1, at = as.numeric(x_breaks), labels = strftime(x_breaks, "%b %Y"))
      title(xlab = "Date", line = 5)
      

      【讨论】:

        【解决方案3】:

        您可以使用ggplot2 轻松完成此操作。这是一些代码

        # generate data frame
        df = data.frame(
               Version = rnorm(20),
               Date    = seq(as.Date('2010-01-01'), by = '1 month', length = 20)
             )
        
        # create plot
        p0 = qplot(Date, Version, data = df) +
             scale_x_date(major = '1 month') +
             opts(axis.text.x = theme_text(angle = 90))
        

        这是输出

        【讨论】:

        • 我喜欢这个答案,因为它给出了网格并且很短,但我选择了另一个,因为它有很多我不知道的有用信息。不过还是谢谢
        猜你喜欢
        • 2021-04-08
        • 1970-01-01
        • 1970-01-01
        • 2012-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多