开发者

How do I zoom in on X axis when it's a date?

I'm plotting a software version to the date it was released. Example:

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

To plot I use:

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

It plots it by year though, I rather want it to plot by month/year, and have the labels printed vertically. How could I accomplish this? I tried setting xaxt="n" and using the axis() function with label=format(data,fmt) but I keep failing.

dput of data snippet:

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


Here is a base graphics version. First, it is easier to manipulate the Date column in-place rather than produce an extra dates object:

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

This then does the plot. Note we need a bit more margin space on the bottom to accommodate the date labels:

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

This gives us:

How do I zoom in on X axis when it's a date?

More flexibility can be gained by altering the sequence of dates we want, here we want every 2 months, and we label them with 2-digit century:

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

To use this just swap in the above call in place of the existing with(....) statement.


Create a sequence of dates for your axis labels.

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

Create dates as a Date to match the above sequence.

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

Set some graphical params, las = 3 rotates your x axis; mar changes margin widths.

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

Now draw it, and manually add the x axis, as you suggested.

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)


You can easily do this using ggplot2. Here is some code

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

Here is the output

How do I zoom in on X axis when it's a date?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜