【问题标题】:X-axis major and minor tick date labels and breaks (epicurve)X 轴主要和次要刻度日期标签和中断(epicurve)
【发布时间】:2014-09-30 10:52:10
【问题描述】:

这是我目前的经验曲线:

以及用于生成它的代码:

# generate random sample of dates and sex
df = data.frame(dates = Sys.Date() + sample(-10:25, 100, replace = T), 
            sex = sample(c("M", "F"), 25, replace = T)) 

require(ggplot2); require(scales)

# set x-axis limits 
xlmts = c(Sys.Date() - 22, Sys.Date() + 30)

p = ggplot(df, aes(x = dates, fill  = sex))

p = p + geom_bar(stat = "bin", colour = "black")

p = p + scale_x_date(breaks = "1 day", labels=date_format("%a \n %d \n %m"),
                 limits = xlmts)

p

这大致就是我希望我的 Epicruve 的样子:

我的 x 轴有两个问题:

问题 1 - 中断

  1. 尽管我在 'scale_x_date' 中设置了 'breaks = 1day',但已将多天组合在一起

问题 2 - 主要和次要刻度标签

2.1 有什么办法可以得到90度角的缩写日名称,并保持0度的日数和月数。

2.2 有没有办法只显示一个月的数字(最好在月中居中),这样它就不会像上面的例子一样在每个主要刻度标签上重复。

【问题讨论】:

    标签: r date ggplot2


    【解决方案1】:

    这会让你走得更远(我没有添加行),但这是相当手动的工作。

    在主线剧情中,我:

    • 使用随机种子使其可重现 :-)
    • 预先计算每天的 M/F 计数
    • 旋转 X 轴标签(仅使用日期短名称)
    • 扩展绘图边距以容纳额外的 X 轴注释

    然后我:

    • 通过annotation_custom 生成并添加非轮换的月内日期#
    • 通过annotation_custom 生成并添加未轮换的月份数字
    • 构建最终的绘图网格结构
    • 关闭面板剪辑矩形
    • grid.draw绘制图形

    您可能会想稍微调整一下间距。

    这应该足以让您继续前进(并且使用lineGrob 添加行应该是非常可行的)。

    library(dplyr)
    library(ggplot2)
    library(scales)
    library(grid)
    library(gridExtra)
    
    set.seed(1492) # reproducible
    
    df = data.frame(dates = Sys.Date() + sample(-10:25, 100, replace = T), 
                    sex = sample(c("M", "F"), 25, replace = T)) 
    
    # pre-compute the M/F counts per date
    
    df.2 <- df %>% group_by(dates, sex) %>% summarise(count=n())
    
    gg <- ggplot(df.2, aes(x=dates, y=count, fill=sex))
    gg <- gg + geom_bar(color="black", position="stack", stat="identity")
    gg <- gg + scale_x_date(breaks="1 day", 
                            labels=date_format("%a"),
                            limits = xlmts)
    gg <- gg + labs(x=NULL)
    gg <- gg + theme_bw()
    gg <- gg + theme(axis.text.x=element_text(angle = 90, hjust = 1))
    gg <- gg + theme(plot.margin=unit(c(1,1,5,1), "line"))
    
    gg1 <- gg # keep base plot
    
    # make & plot the day #'s
    
    for (d in seq(from=as.Date("2014-09-06"), to=as.Date("2014-11-01"), by="1 day")) {
      d1 <- format(as.Date(d, origin="1970-01-01"), "%d")
      gg1 <- gg1 + annotation_custom(grob=textGrob(d1, hjust=0, gp=gpar(cex=0.7)), ymin=-0.8, ymax=-0.8, 
                                     xmin=as.numeric(d), xmax=as.numeric(d))  
    }
    
    # add month numbners
    
    gg1 <- gg1 + annotation_custom(grob=textGrob("9", gp=gpar(cex=0.8)), ymin=-1.0, ymax=-1.0, 
                                xmin=as.numeric(as.Date("2014-09-17")), xmax=as.numeric(as.Date("2014-09-17")))
    gg1 <- gg1 + annotation_custom(grob=textGrob("10", gp=gpar(cex=0.8)), ymin=-1.0, ymax=-1.0, 
                                xmin=as.numeric(as.Date("2014-10-12")), xmax=as.numeric(as.Date("2014-10-12")))
    
    # build the plot
    
    gg2 <- ggplot_gtable(ggplot_build(gg1))
    
    # no panel clipping
    gg2$layout$clip[gg2$layout$name=="panel"] <- "off"
    
    # draw the plot 
    
    grid.draw(gg2)
    

    【讨论】:

    • 啊。看来我这一年没有做annotation_custom,但根据示例中的其他位,应该很容易添加
    猜你喜欢
    • 2012-10-08
    • 1970-01-01
    • 1970-01-01
    • 2020-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-15
    • 2012-03-14
    相关资源
    最近更新 更多