标签y轴在一周内递增ggplot2

时间:2014-10-28 16:18:02

标签: r plot ggplot2

我想生成一个图表,使用ggplot2每周从4月到6月标记y轴。

这是我的数据:

df <- structure(list(year = structure(1:11, .Label = c("2000", "2001", 
"2002", "2003", "2004", "2005", "2006", "2007", "2008", "2009", 
"2010"), class = "factor"), greenups_mean = c(107, 106, 124, 
107, 119, 112, 103, 113, 133, 127, 109), greenups.dtime = structure(c(11063, 
11428, 11811, 12159, 12536, 12895, 13251, 13626, 14011, 14371, 
14718), class = "Date"), gmd = c("04-16", "04-16", "05-04", "04-17", 
"04-28", "04-22", "04-13", "04-23", "05-12", "05-07", "04-19"
)), row.names = c(NA, -11L), class = "data.frame", .Names = c("year", 
"greenups_mean", "greenups.dtime", "gmd"))

我可以制作一个像样的线图。我在y轴上使用df$greenups_mean,它是一年中的某一天。

library (ggplot2)
p <- ggplot (df,aes(x=year,y=greenups_mean)) + geom_line(aes(group=1))
p <- p + ylab('Green-up') + xlab('Year')
p

enter image description here

但是,我想在4月1日到6月1日之间每周给它贴上标签。我想我必须将离散标签传递给情节?

谢谢

-cherrytree

1 个答案:

答案 0 :(得分:1)

要以这种方式更改yaxis,请使用scale_y_continuous

p <- ggplot (df,aes(x=year,y=greenups_mean)) + geom_line(aes(group=1))
p <- p + ylab('Green-up') + xlab('Year')
p + scale_y_continuous(breaks=seq(91,152,length.out=9),limit=c(90,152),labels=c('Apr 1','Apr 8','Apr 15','Apr 22','Apr 29','May 6', 'May 13', 'May 20','May 27'))

enter image description here

那些确切的日期和breaks的范围可能稍微偏离,但这是基本的想法。