I am trying to plot the change in a time series for each calendar year using ggplot and I am having problems with the fine control of the x-axis. If I do not use scale="free_x"
then I end up with an x-axis that shows several years as well as the year in question, like this:
我正在尝试用ggplot绘制每个日历年的时间序列变化,我在x轴的精细控制上遇到了问题。如果我不使用scale="free_x",那么我就会得到一个x轴,这个x轴显示的是年份和年份,如下所示:
If I do use scale="free_x"
then as one would expect I end up with tick labels for each plot, and that in some cases vary by plot, which I do not want:
如果我确实使用scale="free_x",那么正如人们所期望的那样,我最终会为每个地块贴上勾号,在某些情况下,会因地块而异,这是我不希望看到的:
I have made various attempts to define the x-axis using scale_x_date etc but without any success. My question is therefore:
我尝试过使用scale_x_date等来定义x轴,但是没有成功。因此我的问题是:
Q. How can I control the x-axis breaks and labels on a ggplot facet grid so that the (time series) x-axis is identical for each facet, shows only at the bottom of the panel and is in the form of months formatted 1, 2, 3 etc or as 'Jan','Feb','Mar'?
问:我如何控制ggplot facet网格上的x轴断裂和标签,以便(时间序列)x轴对每个面都是相同的,只显示在面板的底部,以1、2、3等月的格式显示,或以“Jan”、“Feb”、“Mar”的格式显示?
Code follows:
代码如下:
require(lubridate)
require(ggplot2)
require(plyr)
# generate data
df <- data.frame(date=seq(as.Date("2009/1/1"), by="day", length.out=1115),price=runif(1115, min=100, max=200))
# remove weekend days
df <- df[!(weekdays(as.Date(df$date)) %in% c('Saturday','Sunday')),]
# add some columns for later
df$year <- as.numeric(format(as.Date(df$date), format="%Y"))
df$month <- as.numeric(format(as.Date(df$date), format="%m"))
df$day <- as.numeric(format(as.Date(df$date), format="%d"))
# calculate change in price since the start of the calendar year
df <- ddply(df, .(year), transform, pctchg = ((price/price[1])-1))
p <- ggplot(df, aes(date, pctchg)) +
geom_line( aes(group = 1, colour = pctchg),size=0.75) +
facet_wrap( ~ year, ncol = 2,scale="free_x") +
scale_y_continuous(formatter = "percent") +
opts(legend.position = "none")
print(p)
1 个解决方案
#1
10
here is an example:
这是一个例子:
df <- transform(df, doy = as.Date(paste(2000, month, day, sep="/")))
p <- ggplot(df, aes(doy, pctchg)) +
geom_line( aes(group = 1, colour = pctchg),size=0.75) +
facet_wrap( ~ year, ncol = 2) +
scale_x_date(format = "%b") +
scale_y_continuous(formatter = "percent") +
opts(legend.position = "none")
p
Do you want this one?
你要这个吗?
The trick is to generate day of year of a same dummy year.
诀窍是生成一年中的一天。
UPDATED
更新
here is an example for the dev version (i.e., ggplot2 0.9)
这里有一个开发版本的示例(例如。,ggplot2 0.9)
p <- ggplot(df, aes(doy, pctchg)) +
geom_line( aes(group = 1, colour = pctchg), size=0.75) +
facet_wrap( ~ year, ncol = 2) +
scale_x_date(label = date_format("%b"), breaks = seq(min(df$doy), max(df$doy), "month")) +
scale_y_continuous(label = percent_format()) +
opts(legend.position = "none")
p
#1
10
here is an example:
这是一个例子:
df <- transform(df, doy = as.Date(paste(2000, month, day, sep="/")))
p <- ggplot(df, aes(doy, pctchg)) +
geom_line( aes(group = 1, colour = pctchg),size=0.75) +
facet_wrap( ~ year, ncol = 2) +
scale_x_date(format = "%b") +
scale_y_continuous(formatter = "percent") +
opts(legend.position = "none")
p
Do you want this one?
你要这个吗?
The trick is to generate day of year of a same dummy year.
诀窍是生成一年中的一天。
UPDATED
更新
here is an example for the dev version (i.e., ggplot2 0.9)
这里有一个开发版本的示例(例如。,ggplot2 0.9)
p <- ggplot(df, aes(doy, pctchg)) +
geom_line( aes(group = 1, colour = pctchg), size=0.75) +
facet_wrap( ~ year, ncol = 2) +
scale_x_date(label = date_format("%b"), breaks = seq(min(df$doy), max(df$doy), "month")) +
scale_y_continuous(label = percent_format()) +
opts(legend.position = "none")
p