R ggplot和facet grid:如何控制x轴中断 [英] R ggplot and facet grid: how to control x-axis breaks

查看:345
本文介绍了R ggplot和facet grid:如何控制x轴中断的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用ggplot来绘制每个日历年的时间序列中的变化,并且我对x轴的精细控制存在问题。如果我不使用 scale =free_x,那么我最终会得到一个x轴,它显示了几年以及所讨论的年份,如下所示:





如果我确实使用了 scale =free_x,那么正如人们所期望的那样,我最终会得到每个plot的tick标签,在某些情况下,根据不同的情节会有所不同:



我做了各种尝试,使用scale_x_date等来定义x轴,但没有取得任何成功。我的问题是:

如何控制ggplot facet网格上的x轴中断和标签,以便(时间序列)x轴对于每个方面都是相同的,仅在面板的底部显示,并以格式为1的月份形式显示, 2,3等或'1月','2月','3月'?

代码如下:

 require(lubridate)
require(ggplot2)
require(plyr)

#生成数据
df < - 数据.frame(date = seq(as.Date(2009/1/1),by =day,length.out = 1115),price = runif(1115,min = 100,max = 200))
#删除周末天数
df < - df [!(weekdays(as.Date(df $ date))%in%c('Saturday','Sunday')),]
#为后面的
df $ year < - as.numeric(格式(as.Date(df $ date),format =%Y))
df $ month < - as添加一些列。数字(格式(as.Date(df $ date),format =%m))
df $ day< - as.numeric(format(as.Date(df $ date),format =% d))

#计算自日历年开始以来的价格变化
df < - ddply(df,。(year),transform,pctc hg =((price / price [1]) - 1))

p < - ggplot(df,aes(date,pctchg))+
geom_line(aes(group = 1, color = pctchg),size = 0.75)+
facet_wrap(〜year,ncol = 2,scale =free_x)+
scale_y_continuous(formatter =percent)+
opts .position =none)

print(p)


解决方案

这里是一个例子:

  df<  -  transform(df,doy = as.Date (paste(2000,month,day,sep =/)))

p < - ggplot(df,aes(doy,pctchg))+
geom_line(aes(group = 1,color = pctchg),size = 0.75)+
facet_wrap(〜year,ncol = 2)+
scale_x_date(format =%b)+
scale_y_continuous(formatter =percent )+
opts(legend.position =none)
p



你想要这个?



诀窍是生成年的一天 ame dummy year。

已更新

这里是dev版本的一个例子(即ggplot2 0.9)

  p < -  ggplot(df,aes(doy,pctchg))+ 
geom_line(aes(group = 1,color = pctchg),size = 0.75)+
facet_wrap(〜year,ncol = 2)+
scale_x_date(label = date_format(%b), seq(min(df $ doy),max(df $ doy),month))+
scale_y_continuous(label = percent_format())+
opts(legend.position =none)
p


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:

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:

I have made various attempts to define the x-axis using scale_x_date etc but without any success. My question is therefore:

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'?

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)

解决方案

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)

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

这篇关于R ggplot和facet grid:如何控制x轴中断的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆