使用r中的scale_x_datetime()函数自定义x轴

时间:2016-11-08 15:18:05

标签: r datetime ggplot2 axis

enter image description here 我想在每个栏的中心显示刻度线,但仅包括每个其他日期的标签。在过去,对于连续数据,我做过类似的事情:

ylabs = seq(0, 4000, 250)
ylabs[-(seq(1, 31, 4))] = ""

scale_y_continuous(breaks = seq(0, 4000, 250), labels = ylabs, limits = c(0,  4000))

这个方法很烦人,但我无法弄清楚如何使用ggplot2函数来解决这个问题。我最好的猜测是采用上面的方法如下:

labels = as.character(unique(Daily.Events$DateOnly))
labels[-seq(from = 1, to = length(labels), by = 2)] = ""

以及如何在scale_x_datetime()函数中插入自定义标签:

g = ggplot(Daily.Events, aes(DateOnly, Count))
g = g + geom_bar(stat = "identity", fill = "black")
g = g + scale_x_datetime(name = "Date", 
                         breaks = date_breaks("1 days"),
                         labels = date_format(format = "%b %d"),
                         expand = c(0,0))

g = g + scale_y_continuous(name = "Daily Events")
g = g + theme(axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1))
plot(g)

有人能提供更好的解决方案吗?我感谢任何帮助和批评!

0 个答案:

没有答案