【问题标题】:label y axis in one week increments ggplot2以一周为增量标记 y 轴 ggplot2
【发布时间】:2014-12-24 03:36:37
【问题描述】:

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

这是我的数据:

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

但是,我不想用一年中的某天标记 y 轴,而是想从 4 月 1 日到 6 月 1 日每周标记一次。我假设我必须将离散标签传递给情节?

谢谢

-樱桃树

【问题讨论】:

    标签: r plot ggplot2


    【解决方案1】:

    要以这种方式更改y轴,请使用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'))
    

    breaks 的确切日期和范围可能有点偏差,但这是基本概念。

    【讨论】:

    • 实际上,我想要 y 轴上的每周刻度线,而不是 x 轴,并且只有两个月,.因此,4 月和 5 月的 8 周只有约 8 个刻度线。
    • “绿色”是指一年中的哪一天吗?
    • 是的,绿色是指一年中的某一天。
    • 这是完美的@keegan。我想知道是否必须对 y 轴标签进行硬编码。谢谢。
    猜你喜欢
    • 2020-06-20
    • 2017-09-23
    • 1970-01-01
    • 2022-01-18
    • 2020-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多